2010-11-24 19 views
0

當我使我的程序硬編碼連接數據庫,其工作正常,但當我去aap.config文件...我沒有連接到數據庫。App.Config不工作

在我的app.config文件我有

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

當我做我的代碼硬編碼,然後我連這樣的

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

,但是當我使用

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

我沒有連接到數據庫。

你們可以PLZ解決這個問題。我已經嘗試了百倍

+0

你錯過格式化所有的代碼 - 你可以糾正嗎? – 2010-11-24 05:18:33

回答

2

請寫在app.config文件中下面的代碼。

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

請將以下代碼寫入.cs文件。這個類文件對所有人都是通用的。

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

添加連接字符串的方式似乎是錯誤的。你shouyld做以下列方式:

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

你必須使用ConfigurationManager.AppSettings[""],而不是時下棄用ConfigurationSettings.AppSettings["ConnectionString"])

ConfigurationSettings。

0

首先你的app.config應該是這樣的:

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

然後添加到System.Configuration命名空間的引用。

然後修改代碼:

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

    }