標籤:
1.在<connectionStrings> 標籤裡添加串連
<connectionStrings>
<add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;Database=DatabaseName;UserID=sa;Password=abc123"
providerName="System.Data.SqlClient" />
</connectionStrings>
或者
<connectionStrings>
<add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
在程式碼中擷取<connectionStrings> 標籤裡的連接字串:
引用命名空間:
- Using System.Configuration ;
- string connStr = ConfigurationManager.ConnectionStrings["ConnectionName"].ToString();
2.在<appSettings>標籤裡添加串連
<appSettings>
<add key="ConnectionName" value="Server=.\SQLEXPRESS;Database=DatabaseName;User ID=sa;Password=abc123" />
</appSettings>
或者
<appSettings>
<add key="ConnectionName" value="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=True" />
</appSettings>
在程式碼中擷取<appSettings> 標籤裡的連接字串:
引用命名空間:
- Using System.Configuration ;
- string connStr = ConfigurationManager.AppSettings["ConnectionName"].ToString();
在ASP.NET項目中的web.config檔案裡設定資料庫串連並在程式碼中擷取連接字串