您的位置:首页 > 数据库

C#中的数据库的连接方式分类说明(转载)

2013-05-31 20:24 281 查看
一: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: