C#連接數據庫的新方法(通過web.config配置文件)
方法一、 1、web.config中<configuration>下加入以下連接代碼 <connectionStrings> <add name="Test" connectionString="server=.;uid=sa;pwd=;Database=tcedu;Connect Timeout=90" providerName="System.Data.SqlClient"/> </connectionStrings> 2、在後臺代碼中加入以下代碼,其中Test爲web.config中數據連接的名稱(name) SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString); 方法二、 1、web.config中<configuration>下加入以下連接代碼 <appSettings> <add key="Test" value="Data Source=.;Initial Catalog=tcedu;User ID=sa;password="/> </appSettings> 2、在後臺代碼中加入以下代碼,其中Test爲web.config中數據連接的名稱(name) SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Test"]);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.