Reputation: 22063
Just thinking about this, is it possible to create a connection string outside the ASP.NET's web.config?
Upvotes: 1
Views: 3421
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
Reputation: 11
You can create a connection string through the usage of .udl file.
UDL File Creation :
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
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
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