2010-11-24 5 views
0

Quand je fais mon programme codé en dur pour la connexion avec la base de données, son beau travail, mais quand je vais pour le fichier aap.config ... Je ne suis pas se connecter à la base de données.App.Config ne fonctionne pas

Dans mon fichier app.config je

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

Quand je fais mon code codé en dur alors je suis connecté comme ce

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

mais quand je me sers

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

Je ne suis pas connecté à la base de données.

Pouvez-vous les gars plz résoudre le problème ... J'ai essayé cent fois

+0

Vous avez manqué le formatage tout le code - pouvez-vous corriger? –

+0

En outre, comme une meilleure pratique, vous devez mettre votre SqlConnection dans 'l'aide (SqlConnection sConnexion = new SqlConnection (...) {.... // votre code ici}' et vous n'avez pas à vous soucier de la enfin des blocs et des trucs en désordre comme ça, même chose pour 'SqlCommand' –

Répondre

2

S'il vous plaît écrire le code ci-dessous dans le fichier app.config.

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

Veuillez écrire le code ci-dessous dans le fichier .cs. Ce fichier de classe est commun à tous.

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

La méthode d'ajout de chaîne de connexion semble incorrecte. Vous shouyld le faire de la manière suivante:

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

Vous devez utiliser ConfigurationManager.AppSettings[""] au lieu de ConfigurationSettings.AppSettings["ConnectionString"])

ConfigurationSettings dépréciés de nos jours.

0

Tout d'abord votre app.config devrait ressembler à ceci:

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

Puis ajouter une référence à l'espace de noms System.Configuration.

Ensuite, modifiez votre 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(); 
      } 
     } 

    }