.net/c#连接sqlserver
Webconfig代码
<configuration>
<appSettings>
<add key="myconnect" value="server=.;UID=sa;password='1';database=test" />
</appSettings>
</configuration>
数据库类代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
///DB_class 的摘要说明
/// </summary>
public class DB_class
{
SqlConnection myconn;
SqlCommand mycmd;
DataSet ds; //数据集
SqlDataAdapter adapt;
public DB_class()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection GetConnection()
{
string mystr = ConfigurationManager.AppSettings["myconnect"].ToString();
SqlConnection myconn = new SqlConnection(mystr);
return myconn;
}
public int ExecNonQuery(string strSql)
{
try
{
myconn = GetConnection();//与数据库连接
mycmd = new SqlCommand();//初始化SqlCommand类对象
mycmd.Connection = myconn;
mycmd.CommandText = strSql;
if (mycmd.Connection.State != ConnectionState.Open)
{
mycmd.Connection.Open();//打开与数据库的连接
}
int a=mycmd.ExecuteNonQuery();//执行Sql操作,并返回受影响的行数
return a;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
if (mycmd.Connection.State == ConnectionState.Open)
{//断开连接,释放资源
mycmd.Connection.Close();
myconn.Dispose();
mycmd.Dispose();
}
}
}
public string ExecScalar(string strSql)
{
try
{
myconn = GetConnection();//与数据库连接
mycmd = new SqlCommand();//初始化SqlCommand类对象
mycmd.Connection = myconn;
mycmd.CommandText = strSql;
if (mycmd.Connection.State != ConnectionState.Open)
{
mycmd.Connection.Open();//打开与数据库的连接
}
//使用SqlCommand对象的ExecuteScalar方法返回第一行第一列的值
//strSql = Convert.ToString(mycmd.ExecuteScalar());
string another = Convert.ToString(mycmd.ExecuteScalar());
return another;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
if (mycmd.Connection.State == ConnectionState.Open)
{//断开连接,释放资源
myconn.Dispose();
mycmd.Connection.Close();
mycmd.Dispose();
}
}
}
public DataTable GetDataSet(string strSql, string TableName)
{
ds = new DataSet();
try
{
myconn = GetConnection();//与数据库连接
adapt = new SqlDataAdapter(strSql, myconn); //实例化SqlDataAdapter类对象
adapt.Fill(ds, TableName);//填充数据集
return ds.Tables[TableName];//返回数据集DataSet的表的集合
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{//断开连接,释放资源
myconn.Close();
adapt.Dispose();
ds.Dispose();
myconn.Dispose();
}
}
}
.net/c#连接sqlserver的更多相关文章
- php以pdo方式连接sqlserver,无法开启sqlsrv扩展
首先,PHP连接sqlserver代码如下: $host="aaaaa"; $dbname="aaaaa"; $user="aaaaa"; ...
- 用Window Authentication的方式去连接SQLServer
用Window Authentication的方式去连接SQLServer Connection String: jdbc:sqlserver://${serverName};databaseName ...
- Android通过webservice连接SQLServer 详细教程(数据库+服务器+客户端)
http://blog.csdn.net/zhyl8157121/article/details/8169172 目录(?)[-] 项目说明 开发环境的部署 数据库设计 服务器端程序设计Webserv ...
- ORACLE连接SQLSERVER
一.实验(实验成功) 1.实验目标:ORACLE连接SQLSERVER以及查询数据 2.搭建的环境: oracle 9i 9.0.2.0.1 地址:192.168.40.139 sql2000 的数据 ...
- Jfinal连接SQLSERVER相关配置说明
driver=net.sourceforge.jtds.jdbc.Driver jdbcUrl = jdbc:sqlserver://localhost:1433;DatabaseName=test ...
- jsp连接sqlServer数据库教程
一.首先讲下我用的工具版本以供参考: jar包:jtds1.3.1.jar 下载地址:点击进入 数据库:SQL Server2012 服务器:Tomcat8.0 下载地址:点击进入 开发IDE工 ...
- navicat 连接sqlserver提示要安装 sql server native client
navicat 连接sqlserver提示要安装 sql server native client 解决方法:其实navicat自带sqlncli_x64.msi,就在安装目录下,安装后问题解决!
- 使用thinkphp连接sqlserver数据库时提示“系统不支持:sqlsrv”
习惯了使用php跟mysql组合,现在接到项目需要调用客户线下的系统软件的数据,具了解,这个软件的数据库是用sqlserver数据库也就是常说的mssql数据库了. 那么我现在需要用PHP连接sqls ...
- PHP:PHP5.4连接SQLSERVER
在PHP5.4的环境下要连接SQLSERVER(2005/2008)数据库,需要以下步骤: 1.下载PHP5.4连接SQLSERVER的驱动(两个动态连接库)http://www.microsoft. ...
- PHP连接sqlserver的两种方法,向sqlserver2000中写入数据,中文乱码
项目环境是php5.3.28 项目用的ThinkPHP3.2.3 已经mysql5.5数据库,要和另一个项目对接,需要连接sqlsever2000数据库进行一些操作. 第一种用php自带扩展连接数据 ...
随机推荐
- Backbone Backbone-localStorage demo
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- GitHub的使用(上)—— 创建和更新
推荐一个属于自己的代码控制工具(或者是叫代码托管工具)——GitHub. 提起代码控制工具,很容易想到的就是CVS,SVN.这也是开发团队常用的.但如果想管理只属于自己的代码呢?那它们就不太合适了—— ...
- Python代码编辑器
PyCharm Community 说到PyCharm Community,我们就会想到它是一款免费的开源Python代码编辑器,不过这也是事实啦.PyCharm Community为我们提供了轻量级 ...
- Newtonsoft.Json序列化和反序列之javascriptConvert.SerializeObject,DeserializeObject,JsonWriter,JsonReader
这里下载:http://www.newtonsoft.com/products/json/安装: 1.解压下载文件,得到Newtonsoft.Json.dll 2.在项目中添加引用.. jav ...
- Model Browser
http://www.entityframeworktutorial.net/model-browser-in-entity-framework.aspx We have created our fi ...
- git(icode)分支及发布管理方式
如果git(icode)不加管理,可能出现枝节蔓生.四处开放的版本库.到处都是分支,完全看不出主干发展的脉络,造成下图的局面: 为了降低合并和版本管理的成本,团队引入一种值得借鉴的管理方式(link) ...
- config windows virtual machine on mac
1.download virtualbox and related extension pack from http://www.oracle.com/technetwork/server-stor ...
- HDU 1525 (博弈) Euclid's Game
感觉这道题用PN大法好像不顶用了,可耻地看了题解. 考虑一下简单的必胜状态,某一个数是另一个数的倍数的时候是必胜状态. 从这个角度考虑一下:游戏进行了奇数步还是偶数步决定了哪一方赢. 如果b > ...
- Xdebug的安装与使用
php下搭配使用xdebug是十分好的组合,用于php的调试工作,下面分别介绍之: 1 WINDOWS下的安装 下载站点http://www.xdebug.org/ 要确定使用一个跟你的PHP配合 ...
- mysql在update时,从其他select结果集更新表
需要使用join,例如 select g.id,g.res_count,count(gr.r_id) cnt from mb_game_res gr left join mb_game g on gr ...