1. 程式人生 > >C# 連線SQLSERVER資料庫 連線字串

C# 連線SQLSERVER資料庫 連線字串

一: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