【问题标题】:how to access connection string Class.cs file?如何访问连接字符串 Class.cs 文件?
【发布时间】:2012-08-27 01:31:14
【问题描述】:

我对 Windows 编程完全陌生,所以我不知道如何访问 Class.cs 文件中的连接字符串,因为我们可以在 Web 应用程序中访问如下:

 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 的帮助。请帮帮我..

【问题讨论】:

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


    【解决方案1】:

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

    using System.Configuration;
    

    注意:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        如果你想使用 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";
        

        【讨论】:

          猜你喜欢
          • 2011-07-10
          • 1970-01-01
          • 1970-01-01
          • 2020-07-28
          • 1970-01-01
          • 2020-03-16
          • 1970-01-01
          • 1970-01-01
          • 2011-05-11
          相关资源
          最近更新 更多