2010-11-30 11 views
1

を与え、この機能app.configファイルには、私はそれをさせるのです場合は正常に動作している接続私はデスクトップアプリケーションの午前正しく

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

この機能により、ローカルサーバー 上のデータベースのリストを取得されていません

ど...ハードコードされたが、私は

として
<?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> 

をapp.configファイルで宣言された接続文字列を使用しようとしたとき、そして、それが機能していません私はする必要がありますか?

+0

設定から接続文字列を取得する方法のコードを表示できますか? –

+0

はいそれはこのようです – Srivastava

+0

SqlConnection sConnection =新しいSqlConnection(ConfigurationSettings.appsettings ["ConnectionString"]); – Srivastava

答えて

2

System.Configuration名前空間とライブラリのAppSettingsReaderクラスから、アプリケーション設定に簡単にアクセスできます。

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

しかし、接続文字列のために、Microsoftはあなたが同様にアプリの設定]セクションに、あなたのapp.configファイルであることを利用ConnectionStringSection

を提供しています

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

と、このようにアクセス:

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

しかし、MrEyesが言うとおり、あなたのapp.configファイルがメインアプリケーションのプロジェクトに含まれていることを確認してください。それ以外の場合は、アプリケーションのデフォルト設定として自動的には使用されません。

2

上記のコードは、メインの実行ファイルとは別のクラスライブラリを使用していますか?

appsettingを追加したapp.configは、クラスライブラリプロジェクトの一部ですか?

両方の質問に「はい」と答えた場合、ConfigurationManagerはあなたが期待している場所を探していません。フレームワークは、実行可能ファイル(コンパイル後のyourexesname.exe.config)のapp.configファイルを探します。だからあなたがそこにアプリケーションの設定を移動する場合は、コードが動作するはずです。

0

Rはい、これはあなたが残っているという事実を行うには何もイマイチ何の問題

0

があってはならない場合uが

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

      } 

以下のようなsoemthingをしよう。 [セミコロン]は文字列の最後にあるのですか?

+0

いいえ。接続文字列をセミコロンで終わらせる必要はありません。 –

関連する問題