C# 从 web.config 文件中获取 sql 连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15540806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting sql connection string from web.config file
提问by A_AR
I am learning to write into a database from a textbox with the click of a button. I have specified the connection string to my NorthWind database in my web.config
file. However I am not able to access the connection string in my code behind.
我正在学习通过单击按钮从文本框写入数据库。我已经在我的web.config
文件中指定了到我的 NorthWind 数据库的连接字符串。但是,我无法在后面的代码中访问连接字符串。
This is what I have tried.
这是我尝试过的。
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();
}
I get a tooltip error for
我收到一个工具提示错误
SqlConnection sql = new SqlConnection(constring);
as
作为
System.data.SqlClient.Sqlconnection.Sqlconnection(string) has some invalid arguments.
System.data.SqlClient.Sqlconnection.Sqlconnection(string) 有一些无效的参数。
I want to load the connection string from the web.config
in constring
我想从web.config
in加载连接字符串constring
采纳答案by p.s.w.g
That's because the ConnectionStrings
collection is a collection of ConnectionStringSettings
objects, but the SqlConnection
constructor expects a string
parameter. So you can't just pass in constring
by itself.
那是因为ConnectionStrings
集合是ConnectionStringSettings
对象的集合,但SqlConnection
构造函数需要一个string
参数。所以你不能仅仅通过constring
它自己。
Try this instead.
试试这个。
SqlConnection sql = new SqlConnection(constring.ConnectionString);
回答by Rajeev Kumar
try this
尝试这个
readonly SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwindconnect"].ToString());
回答by ????
I will suggests you to create a function rather than directly access the web.config file as follow
我会建议你创建一个函数而不是直接访问 web.config 文件如下
public static string GetConfigurationValue(string pstrKey)
{
var configurationValue = ConfigurationManager.AppSettings[pstrKey];
if (!string.IsNullOrWhiteSpace(configurationValue))
return configurationValue;
throw (new ApplicationException(
"Configuration Tag is missing web.config. It should contain <add key=\"" + pstrKey + "\" value=\"?\"/>"));
}
And use this function in you application
并在您的应用程序中使用此功能
回答by Praveen Nambiar
You can simply give a Name
to your ConnectionString
in web.config
file and do this:
您可以简单地给Name
您的ConnectionString
inweb.config
文件并执行以下操作:
web.config:
网络配置:
<add name="ConnectionStringName" connectionString=YourServer"; Initial Catalog=YourDB; Integrated Security=True"/>
Code Behind:
背后的代码:
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ToString());