3

I am working on a Windows application. I have a form with labels like

HOST:
UserName:
Password:

How I can declare the connection string in the app.config file so that it takes the initial catalog, userID and password as variables that I can use in further to check the user whether which database the user wants to get connected with the entered userID and password.

I am using SQL Server 2008 and Visual C# 2008 Express Edition.

Community
  • 1
  • 1
preety
  • 956
  • 3
  • 11
  • 18

6 Answers6

6

As I'm reading your question, you want to have a generic connection string that you want to inject username/password variables into. To do that you would need to have a key with this format:

<add name="myDBKey" connectionString="Data Source=myDB;Initial Catalog={0};Persist Security Info=True;User ID={1};Password={2}" providerName="System.Data.SqlClient"/>

Then in your code, you would need to have these variables declared and assigned, and then use String.Format to complete it.

string dbCatalog = "myCatalog";
string dbUser = "myUser";
string dbPW = "myPW";

string myDBConnectionString = String.Format(
    ConfigurationManager.ConnectionStrings["myDBKey"].ConnectionString,
    dbCatalog, dbUser, dbPW);

This will inject your variables into the string.

Joel Etherton
  • 36,043
  • 10
  • 81
  • 99
  • +1 This was also my interpretation of the question - perhaps the OP can confirm. – CJM Nov 04 '10 at 12:01
4

There is a <connectionString> section to the app.config file.

<connectionStrings>
    <add name="MyDatabase" connectionString="Data Source=sqlserver,1433;Network Library=DBMSSOCN;Initial Catalog=MyDatabase;User ID=xxx;Password=xxxx;" />
  </connectionStrings>

For your Host, User ID and Password, you can define these in the <appSettings> section.

Neil Knight
  • 44,112
  • 23
  • 121
  • 184
1

You can do some thing like this,

<appSettings>
    <add key="SettingName" value="SettingValue" />
</appSettings>

or go to "Variables within app.config/web.config".

Community
  • 1
  • 1
Singleton
  • 3,565
  • 3
  • 22
  • 36
1

Try this

<connectionStrings>
    <add name="ConString" connectionString="Server=Servernae;Database=DBName;User Id=username;password=yourpassword"/>
    </connectionStrings>

For more information try Connection Strings

Mohammad Nadeem
  • 8,364
  • 11
  • 51
  • 79
1

Start by declaring the variables by going to your project's property tab, then going to the Settings tab (on the left), declaring your variables by mentioning the name, default value, and scope (which will be Application).

In your code, to fetch the values:

using System.Configuration;
//....
ConfigurationSettings.AppSettings["ConnectionString"].ToString();
// or
ConfigurationSettings.AppSettings.ConnectionString;

Please note that you can't change the value in code for an Application setting.

EDIT:

Alternately, there is also the connectionStrings node which can be set (but it must be done in the app.config file itself. See MSDN for documentation.

Example of XML:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="ConnStr1" connectionString="LocalSqlServer: data source=127.0.0.1 Integrated Security=SSPI;Initial Catalog=aspnetdb"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

In C#, you will get a System.Coonfiguration.ConnectionStrings, which is a collection of ConnectionStringSettings.

Example of usage in C# code: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx

MPelletier
  • 15,130
  • 14
  • 79
  • 128
0

My library Expansive is designed with this as a primary use-case.

Moderate Example (using AppSettings as default source for token expansion)

In app.config:

<configuration>
    <appSettings>
        <add key="Domain" value="mycompany.com"/>
        <add key="ServerName" value="db01.{Domain}"/>
    </appSettings>
    <connectionStrings>
        <add name="Default" connectionString="server={ServerName};uid=uid;pwd=pwd;Initial Catalog=master;" provider="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Use the .Expand() extension method on the string to be expanded:

var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
connectionString.Expand() // returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"

or

Use the Dynamic ConfigurationManager wrapper "Config" as follows (Explicit call to Expand() not necessary):

var serverName = Config.AppSettings.ServerName;
// returns "db01.mycompany.com"

var connectionString = Config.ConnectionStrings.Default;
// returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"

Advanced Example 1 (using AppSettings as default source for token expansion)

In app.config:

<configuration>
    <appSettings>
        <add key="Environment" value="dev"/>
        <add key="Domain" value="mycompany.com"/>
        <add key="UserId" value="uid"/>
        <add key="Password" value="pwd"/>
        <add key="ServerName" value="db01-{Environment}.{Domain}"/>
        <add key="ReportPath" value="\\{ServerName}\SomeFileShare"/>
    </appSettings>
    <connectionStrings>
        <add name="Default" connectionString="server={ServerName};uid={UserId};pwd={Password};Initial Catalog=master;" provider="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Use the .Expand() extension method on the string to be expanded:

var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
connectionString.Expand() // returns "server=db01-dev.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"
anderly
  • 627
  • 7
  • 15