Skip to main content
 首页 » 编程设计

c#之app.config 文件未正确提供连接

2024年02月23日29freeliver54

我在桌面应用程序上检索本地服务器上的数据库列表 通过这个函数

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

如果我让它被硬编码但当我尝试使用 app.config 文件中声明的连接字符串作为

<?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> 

然后就不行了...

我需要做什么??

请您参考如下方法:

您可以从 AppSettingsReader 轻松访问应用设置System.Configuration 命名空间和库中的类。

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

但是对于连接字符串,微软提供了ConnectionStringSection

您在 app.config 文件中使用它,类似于应用程序设置部分:

<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 文件位于主应用程序的项目中,否则它不会自动用作应用程序的默认配置。