无法通过ConnectionString访问本地数据库

时间:2013-09-05 09:09:11

标签: web-config connection-string configurationmanager

我的connectionstring中有一个web.config,我正在尝试从class文件中调用它。

为什么到达数据库时遇到问题。数据库位于项目内的本地计算机上。

班级

public static SqlConnection createConnection()
{
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Database"]);
    con.Open();
    return con;
}

和web.config

<connectionStrings>
    <add connectionString="server=.\SQLEXPRESS;uid=The_Kettle_LibraryU;pwd=-5$G)dO:}B7X;Database=The_Kettle_Library" name="Database" providerName="System.Data.SqlClient" />
</connectionStrings>

发布错误。

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1502: The best overloaded method match for 'System.Data.SqlClient.SqlConnection.SqlConnection(string)' has some invalid arguments

Source Error:
Line 11:     public static SqlConnection createConnection()
Line 12:     {
Line 13:         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"]);
Line 14:         con.Open();
Line 15:         return con;

1 个答案:

答案 0 :(得分:1)

我认为你应该使用:

ConfigurationManager.ConnectionStrings["Database"].ConnectionString