熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2013-11-13 10:36:34  來源: .NET編程 

  在WebConfig中配置數據庫連接字符串代碼如下

復制代碼 代碼如下:
<connectionStrings>
<add name="ConnectionString" connectionString="user id=用戶名;password=密碼;initial catalog=數據庫名稱;data source=服務器名稱"/>
</connectionStrings>

  
然後在Webform_aspxcs裡面獲取連接字符串要添加如下引用

復制代碼 代碼如下:
using SystemConfiguration;
using SystemData;
using SystemDataSqlClient;

  
代碼

復制代碼 代碼如下:
SqlConnection con;
protected void Page_Load(object sender EventArgs e)
{
ConnectDB();
}
private void ConnectDB()
{
string ConString = ConfigurationManagerConnectionStrings["ConnectionString"]ConnectionString;
con = new SqlConnection(ConString);
conOpen();
SqlCommand com = new SqlCommand();
SqlDataReader sdr;
string sqlstr = "select * from item";
comCommandText = sqlstr;
comConnection = con;
sdr = comExecuteReader();
while (sdrRead())
{
ResponseWrite(sdr["字段名"]ToString()+"</br>");
}
sdrClose();
sdr = null;
}
From:http://tw.wingwit.com/Article/program/net/201311/14106.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.