我正在学习通过点击按钮从文本框写入数据库.我已经在我的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);
如
System.data.sqlClient.sqlconnection.sqlconnection(string) has some invalid arguments.
我想加载连接字符串从web.config在约束
解决方法
这是因为
ConnectionStrings
集合是
ConnectionStringSettings
对象的集合,但是
SqlConnection
构造函数需要一个字符串参数.所以你不能自己通过约束.
试试这个.
sqlConnection sql = new sqlConnection(constring.ConnectionString);
原文地址:https://www.jb51.cc/csharp/91821.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。