opening a connection to a database

Results 1 to 2 of 2

Thread: opening a connection to a database

  1. #1
    Join Date
    Dec 1969

    Default opening a connection to a database

    I have read an article that explains how to open a db connection in C# as:<BR><BR>cont string strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\DataGrocerToGo.mdb";<BR><BR>OleDbConnect ion objConn = new OleDbConnection(strConnString);<BR><BR>objConn.Ope n();<BR><BR><BR>// Now, my question is, Can I store the objConn information in a globally accessible location (either in a class or in the global.asax) so that anywhere in my application, I can open a connection to the database simply by using:<BR><BR>objConn.Open();

  2. #2
    Join Date
    Dec 1969

    Default RE: opening a connection to a database

    Hi,<BR>you can do this in the web.config file, which I think you would put in the application directory(check this) can specify it like you would an application or session variable in there. Here is an example I have used...(not written by me<BR><BR>&#060;configuration&#062;<BR><BR> &#060;!-- store the database connection info here --&#062;<BR><BR> &#060;appSettings&#062;<BR> &#060;add key="DSN"value="server=localhost;uid=sa;pwd=;datab ase=Northwind" /&#062;<BR> &#060;/appSettings&#062;<BR><BR>&#060;/configuration&#062;<BR><BR>**********<BR><BR>you can then call the value like this in your ASP.NET page(this is using VB....C# may vary)<BR><BR>DSN = ConfigurationSettings.AppSettings("DSN")<BR><BR>go od luck!<BR><BR>Neil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts