繁体   English   中英

App.Config不起作用

[英]App.Config is not working

当我的程序硬编码与数据库连接,它的工作正常,但当我去aap.config文件...我没有连接到数据库。

在我的app.config文件中

<add key="ConnectionString" 
     value="Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp"/>

当我对我的代码进行硬编码时,我就像这样连接了

public void BindDBDropDown()
{
   SqlConnection sConnection = new SqlConnection("Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp");

   //To Open the connection.
   sConnection.Open();

   string selectDatabase = @"SELECT NAME FROM master..sysdatabases";

   SqlCommand sCommand = new SqlCommand(selectDatabase, sConnection);

   try
   {
       DataSet dsListOfDatabases = new DataSet("master..sysdatabases");
       SqlDataAdapter da = new SqlDataAdapter(selectDatabase, sConnection);
       da.TableMappings.Add("Table", "master..sysdatabases");
       da.Fill(dsListOfDatabases);

       DataViewManager dsv = dsListOfDatabases.DefaultViewManager;
       cmbDatabases.DataSource = dsListOfDatabases.Tables["master..sysdatabases"];
       cmbDatabases.DisplayMember = "NAME";
       cmbDatabases.ValueMember = ("");
    }
    catch (Exception ex)
    {
       //All the exceptions are handled and written in the EventLog.
       EventLog log = new EventLog("Application");
       log.Source = "MFDBAnalyser";
       log.WriteEntry(ex.Message);
    }
    finally
    {
       if (sConnection.State != ConnectionState.Closed) 
       {
          sConnection.Close();
       }
    }
 }

但是当我使用的时候

public static DataSet GetPrimaryKeyTables()
{
    SqlConnection sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    string selectPrimaryKeys;
    //Opens the connection
    sConnection.Open();
    selectPrimaryKeys = @"SELECT [TABLE_NAME]
                          FROM [INFORMATION_SCHEMA.TABLE_CONSTRAINTS]
                          WHERE [CONSTRAINT_TYPE = 'PRIMARY KEY']
                          ORDER BY  [TABLE_NAME]";

    SqlCommand sCommand = new SqlCommand(selectPrimaryKeys, sConnection);

    try
    {
        DataSet dtPrimaryKeysTables = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
        SqlDataAdapter da = new SqlDataAdapter(selectPrimaryKeys, sConnection);
        da.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
        da.Fill(dtPrimaryKeysTables);

        DataViewManager dsva = dtPrimaryKeysTables.DefaultViewManager;
        return dtPrimaryKeysTables;
     }
     catch (Exception ex)
     {
         //All the exceptions are handled and written in the EventLog.
         EventLog log = new EventLog("Application");
         log.Source = "MFDBAnalyser";
         log.WriteEntry(ex.Message);
         return null;
      }
      finally
      {
         //To close the connection
         if (sConnection != null)
         {
            sConnection.Close();
         }
      }
  }

我没有连接到数据库。

你们可以解决这个问题......我已经尝试了一百次

请在app.config文件中编写以下代码。

<Configuration>
    <connectionStrings>
        <add name="AppName" connectionString="Connectionstring Name" />
    </connectionStrings>
</Configuration>

请在.cs文件中写下以下代码。 这个类文件对所有人都是通用的。

public string connection()
        {
          return System.Configuration.ConfigurationManager.ConnectionStrings["AppName"].ToString(); 
        }

添加连接字符串的方法似乎是错误的。 你可以用以下方式做到这一点:

  <connectionStrings>
    <add name="MyConnection" connectionString="Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp" providerName="System.Data...."/>
  </connectionStrings>

您必须使用ConfigurationManager.AppSettings[""]而不是ConfigurationSettings.AppSettings["ConnectionString"])

如今不建议使用ConfigurationSettings。

首先,您的app.config应该如下所示:

<connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=192.168.10.3;Initial    Catalog=GoalPlanNew;User Id=gp;Password=gp;" providerName="System.Data.SqlClient"/>
</connectionStrings>

然后添加对System.Configuration命名空间的引用。

然后修改你的代码:

public static DataSet GetPrimaryKeyTables()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        SqlConnection sConnection = new SqlConnection(connectionString);

        string selectPrimaryKeys;

        //Opens the connection      
        sConnection.Open();
        selectPrimaryKeys = @"SELECT [TABLE_NAME]
        FROM      [INFORMATION_SCHEMA.TABLE_CONSTRAINTS]
        WHERE     [CONSTRAINT_TYPE = 'PRIMARY KEY']
        ORDER BY  [TABLE_NAME]";

        SqlCommand sCommand = new SqlCommand(selectPrimaryKeys, sConnection);

        try
        {
            DataSet dtPrimaryKeysTables = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
            SqlDataAdapter da = new SqlDataAdapter(selectPrimaryKeys, sConnection);
            da.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
            da.Fill(dtPrimaryKeysTables);

            DataViewManager dsva = dtPrimaryKeysTables.DefaultViewManager;
            return dtPrimaryKeysTables;
        }
        catch (Exception ex)
        {
            //All the exceptions are handled and written in the EventLog.
            EventLog log = new EventLog("Application");
            log.Source = "MFDBAnalyser";
            log.WriteEntry(ex.Message);
            return null;
        }
        finally
        {
            //To close the connection
            if (sConnection != null)
            {
                sConnection.Close();
            }
        }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM