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

时间:2010-11-30 11:20:29

标签: c# visual-studio winforms

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

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

然后它无法正常工作......

我需要做什么?

4 个答案:

答案 0 :(得分:2)

您上面发布的代码是否使用单独的类库作为主可执行文件?

app.config,你在哪里添加了appsetting,是类库项目的一部分吗?

如果对这两个问题都是肯定的,则ConfigurationManager不会查找您期望的位置。该框架将查看app.config文件中的可执行文件(编译后的yourexesname.exe.config)。因此,如果您将应用设置移到那里,代码应该可以正常工作。

答案 1 :(得分:2)

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

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

但是对于连接字符串,Microsoft提供了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文件位于主应用程序的项目中,否则它不会自动用作应用程序的默认配置。

答案 2 :(得分:0)

你尝试下面的事情

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

            }

如果是,那么应该没有任何问题

答案 3 :(得分:0)

这与你离开的事实无关;字符串末尾的[分号]是吗?

相关问题