从web获取sql连接字符串。配置文件

时间:2022-09-05 20:20:01

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中指定到我的wind northdatabase的连接字符串。配置文件。但是,我不能在后面的代码中访问连接字符串。

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加载连接字符串。配置在若干

4 个解决方案

#1


8  

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构造函数需要一个字符串参数。所以不能只通过constring传递。

Try this instead.

试试这个。

SqlConnection sql = new SqlConnection(constring.ConnectionString);

#2


9  

You can simply give a Name to your ConnectionString in web.config file and do this:

您可以简单地在web中为ConnectionString命名。配置文件并这样做:

web.config:

. config:

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

Code Behind:

背后的代码:

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

#3


4  

try this

试试这个

readonly SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwindconnect"].ToString());

#4


0  

I will suggests you to create a function rather than directly access the web.config file as follow

我建议您创建一个函数,而不是直接访问web。配置文件如下图

    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

在应用程序中使用这个函数

#1


8  

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构造函数需要一个字符串参数。所以不能只通过constring传递。

Try this instead.

试试这个。

SqlConnection sql = new SqlConnection(constring.ConnectionString);

#2


9  

You can simply give a Name to your ConnectionString in web.config file and do this:

您可以简单地在web中为ConnectionString命名。配置文件并这样做:

web.config:

. config:

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

Code Behind:

背后的代码:

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

#3


4  

try this

试试这个

readonly SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwindconnect"].ToString());

#4


0  

I will suggests you to create a function rather than directly access the web.config file as follow

我建议您创建一个函数,而不是直接访问web。配置文件如下图

    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

在应用程序中使用这个函数