Connection String in the .net for Ms Access and MSSQL Server

Connection String in the .net for Ms Access

using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = "Dsn=DsnName";
conn.Open();

------------------------------------------

using System.Data.Odbc;

OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"Driver={Microsoft Access Driver (*.mdb)};" +
"Dbq=c:\myPath\myDb.mdb;" +
"Uid=Admin;Pwd=;";

conn.Open();

 

Connection String in the .net for MSSQL Server

The best practice to define your connection string in the connection.config file otherwise you can directly do the code in web.config.

Connection string in .NET 2.0

<appSettings>
<add key="myConnectionString" value="server=localhost or websitename or serverIP ;database=myDb;uid=myUser;password=myPass;" />
</appSettings>

To read the connection string from code, use the ConfigurationSettings class.
string connStr = ConfigurationSettings.AppSettings("myConnectionString");

Connection string in .NET 3.5 and above connection

<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost or websitename or serverIP;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

To read the connection string into your code, use the ConfigurationSettings class.
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

  • 54 Users Found This Useful
Was this answer helpful?

Related Articles

Sample Email Code In .net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendMail.aspx.cs"...

Sample Code For Connection String In Asp to MsAccess Database and Mssql Server

Sample Code For Connection String In Asp to MsAccess DatabaseExample of a DSN connection<%DIM...

Powered by WHMCompleteSolution