A_A*_*_AR 11 c# sql-server asp.net connection-string web-config
我正在学习通过单击按钮从文本框写入数据库.我已经在我的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();
}
Run Code Online (Sandbox Code Playgroud)
我得到一个工具提示错误
SqlConnection sql = new SqlConnection(constring);
Run Code Online (Sandbox Code Playgroud)
如
System.data.SqlClient.Sqlconnection.Sqlconnection(string)有一些无效的参数.
我想从负载连接字符串web.config中constring
Pra*_*iar 10
你可以简单地给Name你的ConnectionStringin web.config文件,并执行以下操作:
web.config中:
<add name="ConnectionStringName" connectionString=YourServer"; Initial Catalog=YourDB; Integrated Security=True"/>
Run Code Online (Sandbox Code Playgroud)
代码背后:
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ToString());
Run Code Online (Sandbox Code Playgroud)
那是因为ConnectionStrings集合是ConnectionStringSettings对象的集合,但SqlConnection构造函数需要一个string参数.所以你不能单独传入constring.
试试这个.
SqlConnection sql = new SqlConnection(constring.ConnectionString);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38228 次 |
| 最近记录: |