KG Sosa
KG Sosa

Reputation: 22063

How to create a connection string outside web.config

Just thinking about this, is it possible to create a connection string outside the ASP.NET's web.config?

Upvotes: 1

Views: 3421

Answers (5)

Shyam sundar shah
Shyam sundar shah

Reputation: 2523

You can use following in case of MSSQL server

string connectionString = "Your Connection string"  

using (SqlConnection con = new SqlConnection(connectionString))
{
    //
    // Open the SqlConnection.
    //
    con.Open();
    //
    // The following code uses an SqlCommand based on the SqlConnection.
    //
    using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
    using (SqlDataReader reader = command.ExecuteReader())
    {
    while (reader.Read())
    {
        Console.WriteLine("{0} {1} {2}",
        reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
    }
    }
}

Upvotes: 0

You can create a connection string through the usage of .udl file.

UDL File Creation :

  1. Right-click on the desktop, or in the folder where you want to create the file.
  2. Select New, then Text Document.
  3. Give the Text Document any name with a .udl extension ("Show file extensions" must be enabled in folder options).
  4. A window will pop up warning that "If you change a filename extension, the file may become unusable. Are you sure you want to change it?" Select Yes.
  5. You have now successfully created a UDL file.

Now you need to implement the settings inside the .udl file according to your requirements. A video tutorial has been provided to explain you the whole procedure of using .udl file to create a connection string for MS SQL Server.

http://visiontechno.net/studymats/udlcreation.html

Upvotes: 1

JBrooks
JBrooks

Reputation: 10013

You can have it in another .config file that gets pulled in by your web.config like this:

<appSettings file="../Support/config/WebEnvironment.config">
</appSettings>

You can then use it in your code like this:

System.Configuration.ConfigurationManager.AppSettings["DefaultConnection"]

We have it such that this file isn't physically under our site, but it is virtually under it. That is the "Support" directory above is a virtual directory. Details can be found HERE.

Upvotes: 0

cgreeno
cgreeno

Reputation: 32411

Yes you can store it anywhere it is just text.... The web.config is just a XML document that stores configuration settings about your application. You could just as easily create another XML file or a text file and read it in from there. You just wouldnt be able to use:

ConfigurationManager.ConnectionStrings[].ConnectionString 

Upvotes: 2

annakata
annakata

Reputation: 75872

Possibly you're looking for configSource?

Upvotes: 3

Related Questions