欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)

 更新時(shí)間:2013年06月27日 17:32:12   作者:  
要想在ASP.NET中操作SQL數(shù)據(jù)庫(kù)首先需要在WebConfig中配置數(shù)據(jù)庫(kù)連接字符串,之后在.cs文件中獲取連接字符串,具體的配置及獲取方法如下,感興趣的朋友可以參考下哈
在WebConfig中配置數(shù)據(jù)庫(kù)連接字符串,代碼如下:
復(fù)制代碼 代碼如下:

<connectionStrings>
<add name="ConnectionString" connectionString="user id=用戶名;password=密碼;initial catalog=數(shù)據(jù)庫(kù)名稱;data source=服務(wù)器名稱"/>
</connectionStrings>

然后在Webform_1.aspx.cs里面獲取連接字符串,要添加如下引用;
復(fù)制代碼 代碼如下:

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

代碼:
復(fù)制代碼 代碼如下:

SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
ConnectDB();
}
private void ConnectDB()
{
string ConString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
con = new SqlConnection(ConString);
con.Open();
SqlCommand com = new SqlCommand();
SqlDataReader sdr;
string sqlstr = "select * from item";
com.CommandText = sqlstr;
com.Connection = con;
sdr = com.ExecuteReader();
while (sdr.Read())
{
Response.Write(sdr["字段名"].ToString()+"</br>");
}
sdr.Close();
sdr = null;
}

這樣就可以了,非常簡(jiǎn)單,初學(xué)asp.net ,希望朋友們多指教,感激不盡!

相關(guān)文章

最新評(píng)論