我正在学习通过单击按钮从文本框写入数据库。我在我的web.config
文件中指定了连接字符串给我的NorthWind数据库。但是,我无法访问后面的代码中的连接字符串。从web.config文件获取sql连接字符串
这是我试过的。
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(字符串)有一些无效的参数。
我想加载从web.config
在连接字符串中constring
这是不相关的问题,而是从文本框原因SQL注入写入DB。 – Popeye 2013-03-21 06:30:38
@Popeye是吗?你有什么建议?将用户输入值存储在数据库中的哪种方法更好? – 2013-03-21 06:34:01
只要搜索谷歌,你也可以参考这个http://www.mikesdotnetting.com/Article/113/Preventing-SQL-Injection-in-ASP.NET – Popeye 2013-03-21 06:36:59