然后在Webform_ aspx cs里面获取连接字符串 要添加如下引用
复制代码 代码如下: using System Configurationusing System Datausing System Data SqlClient 复制代码 代码如下: SqlConnection conprotected void Page_Load(object sender EventArgs e) { ConnectDB()} private void ConnectDB() { string ConString = ConfigurationManager ConnectionStrings["ConnectionString"] ConnectionStringcon = new SqlConnection(ConString)con Open()SqlCommand = new SqlCommand()SqlDataReader sdrstring sqlstr = "select * from item"CommandText = sqlstrConnection = consdr = ExecuteReader()while (sdr Read()) { Response Write(sdr["字段名"] ToString()+"</br>")} sdr Close()sdr = null} lishixinzhi/Article/program/net/201311/14106在WEB.config配置下面的节点,connectionstring写的是连接数据库的字符串datasource是填写的数据库,initialcatalog是连接的数据库名称,user id是连接数据的账户名,password是账户的密码
<connectionStrings>
<add name="connstring" connectionString="Data Source=.\HHInitial Catalog=stuUser ID=saPassword=123"/>
</connectionStrings>
--在web应用层获取这段数据库连接字符串的语句是
ConfigurationManager.ConnectionStrings["connstring"].ToString()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)