如何访问连接字符串Class.cs文件?

时间:2012-09-01 14:52:44

标签: asp.net winforms connection-string app-config

我是Windows编程的新手,所以我不知道如何在Web应用程序中访问Class.cs文件中的连接字符串,如下所示:

 public DataTable Client_Login_Check(string Email, string Password)
{
    DataTable dt = new DataTable();
    try
    {
        object[] objParam = new object[2];
        objParam[0] = Email;
        objParam[1] = Password;
        return dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["GL"], "GreenLight_Users_LoginCheck", objParam).Tables[0];
    }

    catch (Exception ex)
    {
        OutputMessage = ex.Message;
    }
    return dt;
}

我添加了“ System.Configuration ”命名空间...但我仍然没有在帮助中获得 ConfigurationManager 。请帮帮我..

3 个答案:

答案 0 :(得分:1)

除了在项目中添加对System.Configuration.dll的引用外,您还需要在文件中为命名空间添加using声明:

using System.Configuration;

注意:

您应该使用ConfigurationManager.ConnectionStrings["GL"].ConnectionString并在connectionStrings configuration section中使用连接字符串,而不是appSettings部分。

答案 1 :(得分:1)

添加对System.Configuration.dll的引用,您应该能够使用System.Configuration.ConfigurationManager

答案 2 :(得分:1)

如果你想使用App.config

在标记

中添加这些行
<configuration>
  <connectionStrings>
    <add name="mystring" connectionString="Data Source=.\SQLEXPRESS;Initial     Catalog=dbName;Integrated Security=True" providerName="System.Data.SqlClient"/>
   </connectionStrings>

   <appSettings>
     <add key="Server" value=".\SQLEXPRESS"/>
     <add key="Database" value="dbAIAS"/>
     <add key="ClientSettingsProvider.ServiceUri" value=""/>
  </appSettings>

 <configuration>
您可以通过

访问Name.cs文件中的

string strconn = @"Data Source= " +   ConfigurationSettings.AppSettings["Server"].ToString() + "; Initial Catalog= " +         ConfigurationSettings.AppSettings["Database"].ToString() + ";Integrated Security=True";