2010-12-02 15 views
0

ユーザーIDとパスワードを取得し、ローカルserver.Nowで使用可能なデータベースのリストを表示するフォームを開発しました。このapp.configファイルにハードコードされた値を削除する

public void BindDBDropDown() 
{ 
    //Create the connection object 
    SqlConnection sConnection = new SqlConnection(
     ConfigurationSettings.AppSettings["ConnectionString"]); 

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

/// <summary> 
///This function binds the names of all the tables with primary 
///keys in a dropdown cmbResults. 
/// </summary> 
public void GetPrimaryKeyTable() 
{ 
    //An instance of the connection string is created to manage 
    //the contents of the connection string. 
    var sqlConnection = new SqlConnectionStringBuilder(); 
    sqlConnection.DataSource = "192.168.10.3"; 
    sqlConnection.UserID = "gp"; 
    sqlConnection.Password = "gp"; 
    sqlConnection.InitialCatalog = 
     Convert.ToString(cmbDatabases.SelectedValue); 
    string connectionString = sqlConnection.ConnectionString; 

    SqlConnection sConnection = new SqlConnection(connectionString); 

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

    //Query to select the table_names that have PRIMARY_KEYS. 
    string selectPrimaryKeys = @" 
     SELECT TABLE_NAME 
     FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
     WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' 
     AND  TABLE_NAME <> 'dtProperties' 
     ORDER BY TABLE_NAME"; 

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

    try 
    { 
     //Create the dataset 
     DataSet dsListOfPrimaryKeys = 
      new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS"); 

     //Create the dataadapter object 
     SqlDataAdapter sDataAdapter = 
      new SqlDataAdapter(selectPrimaryKeys, sConnection); 

     //Provides the master mapping between the sourcr table 
     //and system.data.datatable 
     sDataAdapter.TableMappings.Add("Table", 
      "INFORMATION_SCHEMA.TABLE_CONSTRAINTS"); 

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

     //Bind the result combobox with primary key tables 
     DataViewManager dvmListOfPrimaryKeys = 
      dsListOfPrimaryKeys.DefaultViewManager; 
     dgResultView.DataSource = dsListOfPrimaryKeys 
      .Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"]; 
    } 
    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 connection is not closed then close the connection 
     if(sConnection.State != ConnectionState.Closed) 
     { 
      sConnection.Dispose(); 
     } 
    } 
} 

.like誰もがこれらのハードコードされたものを削除し、直接app.configファイルからローカルサーバーのアドレス、ユーザーIDとパスワードを取るに私を助けることができます?

+0

これは助けをhttpで次のようになります。 //social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/9a8c9f5a-092e-4c4a-87bb-9f35d8f55da1 – Egalitarian

+1

これは、 'ConfigurationSettings.AppSettings [" ConnectionString "]'行ですでに実行しています。他のハードコーディングされたもののためにそれを行う際の問題は何ですか? – Steven

答えて

5

まず、app.configファイルまたはweb.configファイルを開きます。それが存在しない場合、それは次のセクション

<appSettings> 
    <add key="...." value="....." /> 
    .... 
</appSettings> 

ため

ルックを追加する必要があります。

今...

<add key="myServer" value="192.168.10.3" /> 
<add key="myUserId" value="gp" /> 
<add key="myPassword" value="gp" /> 

を以下のキー/値を追加し、これはapp.configをが今...

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
     <add key="myServer" value="192.168.10.3" /> 
     <add key="myUserId" value="gp" /> 
     <add key="myPassword" value="gp" /> 
    </appSettings> 
</configuration> 

素晴らしい!のように見えることができるものの一例です。これでコードを更新できます。アプリで(..

sqlConnection.DataSource = ConfiguationManager.AppSettings["myServer"]; 
sqlConnection.UserID = ConfiguationManager.AppSettings["myUserId"]; 
sqlConnection.Password = ConfiguationManager.AppSettings["myPassword"]; 

今、あなたはそれらのキー/値の値を変更することができますし...

sqlConnection.DataSource = "192.168.10.3"; 
sqlConnection.UserID = "gp"; 
sqlConnection.Password = "gp"; 

変更 - :トリックはConfigurationManager.AppSettingsクラスを使用することですコードをコンパイルすることなく.configファイルまたはweb.configファイル):)

HTH

+0

System.Configurationを先に使用してください。最後のセクションを簡単にするには、次のように読んでみてください。 'sqlConnection.DataSource = ConfigurationSettings.AppSettings [" myServer "];' – nawfal

関連する問題