开发者

C# Sql Connection String Across Pages

If I have several pages what would be the correct procedure in creating a connection string variable and sharing it among all of my pages. I would prefer not to type the connection stri开发者_StackOverflowng 100 times for each page and would just rather call it. Can I create it in my namespace or whats the best approach?


Put the connection string in the web.config file. See the following on MSDN: How to: Read Connection Strings from the Web.config File

Example of connection string in config:

<connectionStrings>
    <add name="Movies2"
         connectionString="Data Source=(local);Initial Catalog=Movies;User ID=wt3movies;Password=lalalalala;Integrated Security=SSPI"
         providerName="System.Data.SqlClient" />
</connectionStrings>

Using the string:

string connStr = ConfigurationManager.ConnectionStrings["Movies2"].ConnectionString;


It's typically in your configuration file (web.config)


can't you use the <connectionStrings/> configuration ? http://msdn.microsoft.com/en-us/library/ms178411.aspx


Well i would have included Setting file and placed it over there.It may be not the best bet but works for me.


There are many options. It depends on what data access methodology you are using. I would suggest creating a class to handle loading the connection string from the web.config file and exposing it as a public property.


There are a variety of ways to approach this which would delve in to architecture/SOC/IoC/Repository/ etc, but to answer your question in its simplest possible sense, you could create a Database class that had a single method that fetched your connection string from configuration.

internal class DataAccess
{
   static string GetDatabaseConnection()
    {
      return ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
      // where AppDb is defined in your web.config/app.config.
    }
}

Your pages could just use:

string connection = DataAccess.GetDatabaseConnection();


connection strings are usually stored in configuration files such as the web config. Here is a simple example add something like this to the config

<connectionStrings>
  <add 
    name="NorthwindConnectionString" 
    connectionString="Data Source=serverName;Initial 
    Catalog=Northwind;Persist Security Info=True;User 
    ID=userName;Password=password"
    providerName="System.Data.SqlClient"
  />
</connectionStrings>

and then retrive it as

System.Configuration.Configuration rootWebConfig =
            S

ystem.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
            System.Configuration.ConnectionStringSettings connString;
            if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
            {
                connString =
                    rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
                if (connString != null)
                    Console.WriteLine("Northwind connection string = \"{0}\"",
                        connString.ConnectionString);
                else
                    Console.WriteLine("No Northwind connection string");
            }

full article is here http://msdn.microsoft.com/en-us/library/ms178411.aspx


You should keep it in you config file. For winforms that will be app.config, and for webforms it's web.config. Here is the section you need to have (for winforms).

<connectionStrings> <add name="MyNameSpace.Properties.Settings.ConnectionString1" connectionString="Data Source=MYSQLSERVER;Initial Catalog=DATABASENAME;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

Then you can access the connection string like this (depending on your .NET version - this is for 2.0)

string connectionString = ((string)(configurationAppSettings.GetValue(ConnectionString1"", typeof(string))));


Have the connection string (CS) in (App/Web).Config file and have the CS returned from a static method GetConncectionString().It means, this particular static method would be used in all the pages where CS is required.


You can also make a file called connectionStrings.config,or a name you choose, with this content:

    <connectionStrings>
  <add name="MyConnection" connectionString="server=MyServer; database=MyDataBase; user id=myUser; pwd=MyPwd;"/>
</connectionStrings>

And then, in your Web.Config Insert this tag under node

<connectionStrings configSource="connectionStrings.config"/>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜