简体   繁体   中英

App.Config is not working

When I make my program hard coded for connection with Database, its working fine but when I go for aap.config file...I am not getting connected to the database.

In my app.config file I have

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

When I am making my code hard coded then I am connected like this

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();
       }
    }
 }

but when I am using

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();
         }
      }
  }

I am not connected to the database.

Can you guys plz solve the problem...I have tried a hundred times

Please write the below code in app.config file.

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

Please write the below code in .cs file. This class file is common to all.

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

The way of adding connection string seems to be wrong. You shouyld do it in the following manner:

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

You have to use ConfigurationManager.AppSettings[""] instead of ConfigurationSettings.AppSettings["ConnectionString"])

ConfigurationSettings deprecated nowadays.

Firstly your app.config should look like this:

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

Then add a reference to the System.Configuration namespace.

Then modify your code:

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();
            }
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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