2010-11-24 6 views
0

Wenn ich mein Programm für die Verbindung mit der Datenbank fest codiert, funktioniert es gut, aber wenn ich für aap.config Datei gehe ... Ich werde nicht mit der Datenbank verbunden.App.Config funktioniert nicht

In meiner app.config-Datei habe ich

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

Wenn ich meinen Code schwer mache codiert dann bin ich wie dieses

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

aber wenn ich bin mit

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

Ich bin nicht mit der Datenbank verbunden.

Können Sie Kerle plz das Problem zu lösen ... Ich habe hundertmal versucht

+0

Sie verfehlten den gesamten Code Formatierung - können Sie es richtig? –

+0

Außerdem sollten Sie Ihre SqlConnection in 'using (SqlConnection sConnection = new SqlConnection (...) {.... // Ihr Code hier}} setzen und dann müssen Sie sich keine Gedanken über die endlich blockiert und unordentliches Zeug wie das. Das gleiche gilt für 'SqlCommand' –

Antwort

2

Bitte schreiben Sie den Code unten in app.config-Datei.

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

Bitte schreiben Sie den folgenden Code in .cs-Datei. Diese Klassendatei ist allen gemeinsam.

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

Der Weg zum Hinzufügen der Verbindungszeichenfolge scheint falsch zu sein. Sie shouyld es in der folgenden Art und Weise tun:

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

Sie haben ConfigurationManager.AppSettings[""] zu verwenden, anstatt ConfigurationSettings.AppSettings["ConnectionString"])

Configuration heute veraltet.

0

erstens Ihre app.config sollte wie folgt aussehen:

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

Dann einen Verweis auf die Namespace System.Configuration hinzuzufügen.

ändern Sie dann Ihren 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(); 
      } 
     } 

    }