日期:2014-05-18  浏览次数:20795 次

如何用C#连接 SQL SEVER 数据库
请教各位高手 如何用C#连接到SSQLSEVER 数据库 给出详细代码 谢谢

------解决方案--------------------
参考如下资料:

一:C# 连接SQL数据库

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=True;
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;

1:Integrated Security参数
当设置Integrated Security为 True 的时候,连接语句前面的 UserID, PW 是不起作用的,即采用windows身份验证模式。
只有设置为 False 或省略该项的时候,才按照 UserID, PW 来连接。
Integrated Security 还可以设置为:sspi ,相当于 True,建议用这个代替 True。
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=true;
Data Source=myServerAddress;Initial Catalog=myDataBase;;User ID=myUsername;Password=myPasswordIntegrated Security=false;

2:参数Trusted_Connection
Trusted_Connection=true,将使用当前的 Windows 帐户凭据进行身份验证
Trusted_Connection=false;将不采用信任连接方式(也即不采用Windows验证方式),而改由SQL Server 2000验证方式
Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=false;
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

3:Initial Catalog是你要连接的数据库的名字

4:WINCE连接
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;User ID=myDomain\myUsername;Password=myPassword;

二:可以利用SqlConnectionStringBuilder,这样不必去记住名称。
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
scsb.DataSource = @"(local)\SQLExpress";
scsb.IntegratedSecurity = true;
scsb.InitialCatalog = "Northwind";
SqlConnection myConnection = new SqlConnection(scsb.ConnectionString);

三:可以利用属性中的Setting来自动设置连接字符串
1:在type中选择 (connection string),
2:在DataSouce中选择数据源,然后再Server中输入服务器名,本地用(local)\SQLExpress
3:选择登陆验证方式,本次选Windows验证(即信任连接Integrated Security=True)
4:选择数据库名,确认即可
Data Source=(local)\SQLExpress;Initial Catalog=Northwind;Integrated Security=True
server = .\sqlexpress;integrated security = true;database = northwind

四:SQL2005远程服务器连接方法

如何打开sql server 2005 的1433端口:
配置工具->Sql Server Configuration Manager->MSSQLSERVER的协议看看TCP/IP协议是否启动,如果启动,右键菜单点"属性" ,在分页菜单中选"IP地址",把"IP1"和"IP2"中"TCP端口"为1433,"已启用"改为"是"
配置工具->Sql Server Configuration Manager->SQL Native Client 配置->客户端协议->TCP/IP选择TCP/IP右键菜单中"属性",确认"默认端口"是1433,"已启用"为"是"。

SQL Server 2005 远程连接配置TCP/IP属性:
Surface Area Configuration --> Database Engine --> Remote Connections --->Using TCP/IT SQL Server 外围应用配置器?服务和连接外围配置?database englie?远程连接?启用(远程连接的TCP/IP和named pipes) 
SQL Server Configuration Manager?SQL2005网络配置?启用TCP/IP和named pipes

其他说明见下: sqlserver2005(Express版),为了便于管理,你还需要去下一个manage管理器:
安装好manage管理器后,在程序中连接sqlserver2005,下面几点是要注意的。 
1. 开启sql2005远程连接功能,开启办法如下, 配置工具->sql server外围应用配置器->服务和连接的外围应用配置器->打开MSSQLSERVER节点下的Database Engine 节点,先择"远程连接",接下建议选择"同时使用TCP/IP和named pipes",确定后,重启数据库服务就可以了. 
2.登陆设置改为,Sql server and windows Authentication方式同时选中,具体设置如下: manage管理器->windows Authentication(第一次用windows方式进去),->对象资源管理器中选择你的数据服务器--右键>属性>security>Sql server and windows Authentication方式同时选中. 
3:设置一个Sql server方式的用户名和密码,具体设置如下: manage管理器->