c# – 从web.config文件获取sql连接字符串
发布时间:2020-12-15 04:23:48 所属栏目:百科 来源:网络整理
导读:我正在学习通过点击按钮从文本框写入数据库.我已经在我的web.config文件中指定了我的NorthWind数据库的连接字符串.但是我无法访问我的代码中的连接字符串. 这是我试过的 protected void buttontb_click(object sender,EventArgs e){ System.Configuration.Co
我正在学习通过点击按钮从文本框写入数据库.我已经在我的web.config文件中指定了我的NorthWind数据库的连接字符串.但是我无法访问我的代码中的连接字符串.
这是我试过的 protected void buttontb_click(object sender,EventArgs e) { System.Configuration.Configuration rootwebconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Mohtisham"); System.Configuration.ConnectionStringSettings constring; constring = rootwebconfig.ConnectionStrings.ConnectionStrings["northwindconnect"]; SqlConnection sql = new SqlConnection(constring); sql.Open(); SqlCommand comm = new SqlCommand("Insert into categories (categoryName) values ('" + tb_database.Text + "')",sql); comm.ExecuteNonQuery(); sql.Close(); } 我得到一个工具提示错误 SqlConnection sql = new SqlConnection(constring); 如
我想加载连接字符串从web.config在约束 解决方法
这是因为
ConnectionStrings 集合是
ConnectionStringSettings 对象的集合,但是
SqlConnection 构造函数需要一个字符串参数.所以你不能自己通过约束.
试试这个. SqlConnection sql = new SqlConnection(constring.ConnectionString); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |