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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:07:04  来源:igfitidea点击:

Getting sql connection string from web.config file

c#asp.netsql-serverweb-configconnection-string

提问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.configfile. 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.configin constring

我想从web.configin加载连接字符串constring

采纳答案by p.s.w.g

That's because the ConnectionStringscollection is a collection of ConnectionStringSettingsobjects, but the SqlConnectionconstructor expects a stringparameter. So you can't just pass in constringby 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 Nameto your ConnectionStringin web.configfile and do this:

您可以简单地给Name您的ConnectionStringinweb.config文件并执行以下操作:

web.config:

网络配置:

<add name="ConnectionStringName"  connectionString=YourServer"; Initial Catalog=YourDB; Integrated Security=True"/>

Code Behind:

背后的代码:

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ToString());