2010-11-30 5 views
1

ich auf einer Desktop-Anwendung bin und die Liste der Datenbanken auf einem lokalen Server durch diese Funktion Abrufenapp.config-Datei gibt eine Verbindung nicht richtig

/// <summary> 
/// This function populates the databases list in the drop down after the user is connected. 
/// </summary> 
public void BindDBDropDown() 

     { 

     //Create the connection object 
     SqlConnection sConnection = new SqlConnection("Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp"); 

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

     //Query to select the list of databases. 
     string selectDatabaseNames = @"SELECT 
              NAME 
             FROM 
              MASTER..SYSDATABASES"; 

     //Create the command object 
     SqlCommand sCommand = new SqlCommand(selectDatabaseNames, sConnection); 

     try 
      { 
      //Create the data set 
      DataSet sDataset = new DataSet("master..sysdatabases"); 

      //Create the dataadapter object 
      SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectDatabaseNames, sConnection); 
      sDataAdapter.TableMappings.Add("Table", "master..sysdatabases"); 

      //Fill the dataset 
      sDataAdapter.Fill(sDataset); 

      //Bind the database names in combobox 
      DataViewManager dsv = sDataset.DefaultViewManager; 

      //Provides the master mapping between the sourcr table and system.data.datatable 
      cmbDatabases.DataSource = sDataset.Tables["master..sysdatabases"]; 
      cmbDatabases.DisplayMember = "NAME"; 
      cmbDatabases.ValueMember = ("NAME"); 
      } 
     catch(Exception ex) 
      { 
      //All the exceptions are handled and written in the EventLog. 
      EventLog logException = new EventLog("Application"); 
      logException.Source = "MFDBAnalyser"; 
      logException.WriteEntry(ex.Message); 
      } 
     finally 
      { 
      //If connection is not closed then close the connection 
      if(sConnection.State != ConnectionState.Closed) 
       { 
       sConnection.Close(); 
       } 
      } 
     } 

Diese Funktion funktioniert prima, wenn ich im Stich gelassen habe es werden fest einprogrammiert, aber als ich versuchte, in der app.config-Datei als

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key="ConnectionString" value="Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp"/> 
    </appSettings> 
</configuration> 

erklärt die Verbindungszeichenfolge zu verwenden, dann ist es nicht funktioniert ...

Wha t muss ich tun ??

+0

Können Sie uns den Code zeigen, wie Sie die Verbindungszeichenfolge von der Config bekommen? –

+0

ja ist es so – Srivastava

+0

SqlConnection sConnection = neue SqlConnection (ConfigurationSettings.appsettings ["ConnectionString"]); – Srivastava

Antwort

2

Sie können einfachen Zugriff auf App-Einstellungen von der Klasse AppSettingsReader im System.Configuration-Namespace und der Bibliothek erhalten.

AppSettingsReader asr = new AppSettingsReader(); 
SqlConnection sConnection = new SqlConnection(asr.GetValue("ConnectionString", typeof(string)).ToString()); 

Aber für eine Verbindungszeichenfolge haben Microsoft die ConnectionStringSection

Sie, dass in Ihrem app.config-Datei verwenden, zur Verfügung gestellt, ähnlich wie die App-Einstellungen Abschnitt:

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

und greifen Sie wie folgt aus:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString) 

Aber, wie MrEyes sagt, Sie brauchen Stellen Sie sicher, dass sich Ihre app.config Datei im Projekt Ihrer Hauptanwendung befindet, andernfalls wird sie nicht automatisch als Standardkonfiguration für Ihre Anwendung verwendet.

2

Ist der Code, den Sie oben veröffentlicht haben, eine separate Klassenbibliothek für Ihre Hauptprogrammdatei?

Ist die app.config, wo Sie den appsetting, Teil des Klassenbibliotheksprojekts hinzugefügt?

Wenn diese beiden Fragen mit "Ja" beantwortet werden, sucht der ConfigurationManager nicht, wo Sie es erwarten. Das Framework sucht nach der Datei app.config für Ihre ausführbare Datei (yourexesname.exe.config nach der Kompilierung). Wenn Sie also die App-Einstellungen dorthin verschieben, sollte der Code funktionieren.

0

r u soemthing versuchen, wie unten

if (!String.IsNullOrEmpty(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString)) 
      { 

      } 

wenn ja, dann sollte es kein Problem sein

0

Dieses ist nicht etwas mit der Tatsache zu tun, die Sie verlassen haben; [Semikolon] vom Ende der Schnur ist es?

+0

Nein. Die Verbindungszeichenfolge muss nicht mit einem Semikolon abgeschlossen werden. –

Verwandte Themen