开发者

app.config file is not giving connection properly

I am on a desktop application and retrieving the list of databases on a local server by this function

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

This function is working fine if I am letting it to be hardcoded but when I tried to use the connection string declared in the app.config file as

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

Then it is not working...

What I need to do开发者_JAVA百科??


You can get easy access to app settings from the AppSettingsReader class in the System.Configuration namespace and library.

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

But for a connection string, Microsoft have provided the ConnectionStringSection

You use that in your app.config file, similarly to the app settings section:

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

and access it like this:

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

But, as MrEyes says, you need to make sure your app.config file is in your main application's project, otherwise it's not automatically used as the default configuration for your application.


Is the code you posted above using a separate class library to your main executable?

Is the app.config, where you added the appsetting, part of the class library project?

If yes to both those questions then the ConfigurationManager is not looking where you are expecting it to. The framework will look to the app.config file for your executable (yourexesname.exe.config after compilation). So if you move the app settings into there the code should work.


r u trying soemthing like below

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

            }

if yes then there should not be any problem


This isnt anything to do with the fact you have left ; [semi-colon] off the end of the string is it?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜