Net-informations.com
SiteMap  | About    

Connection String

A connection string provides the information that a provider needs to communicate with a particular database. The Connection String includes parameters such as the name of the driver, Server name and Database name , as well as security information such as user name and password.

An ADO.NET Data Provider is a class that can communicate with a specific type of database or data store. Usually Data Providers use a connection string containing a collection of parameters to establish the connection with the database through applications. The .NET Framework provides mainly three data providers, they are

.NET DataProvider(SQL Server); OLEDB ODBC

From the following section you can find different types of providers and its connection strings in detail.

Microsoft SQL Server

.NET DataProvider - Standard Connection

using System.Data.SqlClient; ConnectionString = "Data Source=servername;" + "Initial Catalog=databasename;User id=uasername;Password=password;"; SqlConnection connection ; connection = new SqlConnection(ConnectionString); connection.Open();

.NET DataProvider - Trusted Connection

using System.Data.SqlClient; ConnectionString = "Data Source=servername;" + "Initial Catalog=databasebame;"Integrated Security=SSPI;"; SqlConnection connection ; connection = new SqlConnection(ConnectionString); connection.Open();

.NET DataProvider - using IP Address

using System.Data.SqlClient; ConnectionString = "Network Library=DBMSSOCN;" + "Data Source=your IP Address,1433;Initial Catalog=datadasename;" + "User Id=username;Password=password;"; SqlConnection connection ; connection = new SqlConnection(ConnectionString); connection.Open();

OleDb - Standard Connection

using System.Data.OleDb; ConnectionString = "Driver=SQLOLEDB;Data Source=servername;" + "Initial Catalog=databasename;User id=username;Password=password;"; OleDbConnection connection ; connection = new OleDbConnection(ConnectionString); connection.Open();

OleDb - Trusted Connection

using System.Data.OleDb; ConnectionString = "Driver=SQLOLEDB;Data Source=servername;" + "Initial Catalog=databasename;Integrated Security=SSPI;"; OleDbConnection connection ; connection = new OleDbConnection(ConnectionString); connection.Open();

ODBC DSN

using System.Data.Odbc; ConnectionString = "Dsn=yourdsnname;Uid=username;Pwd=password;"; OdbcConnection connection ; connection = new OdbcConnection(ConnectionString); connection.Open();

ODBC - Standard Connection

using System.Data.Odbc; ConnectionString = "Driver={SQL Server};" + Server=servername;DataBase=databasename;" + "Uid=username;Pwd=password;"; OdbcConnection connection ; connection = new OdbcConnection(ConnectionString); connection.Open();

ODBC - Trusted Connection

using System.Data.Odbc; ConnectionString = "Driver={SQL Server};Server=ServerName;" + "DataBase=DataBaseName;Trusted_Connection=Yes;"; OdbcConnection connection ; connection = new OdbcConnection(ConnectionString); connection.Open();

MySql

.NET DataProvider - MySQL Connector/Net

using MySql.Data.MySqlClient; ConnectionString = "server=localhost;database=testDB;" + "uid=root;pwd=abc123;"; MySqlConnection connection ; connection = new MySqlConnection(ConnectionString); connection.Open();

Specifying TCP port

using MySql.Data.MySqlClient; ConnectionString = "Server=IP Address;Port=1234;" + Database=myDataBase;Uid=myUsername;Pwd=myPassword;" MySqlConnection connection ; connection = new MySqlConnection(ConnectionString); connection.Open();

OleDb

using System.Data.OleDb; ConnectionString = "Provider=MySqlProvider;Data Source=servername;" + "User id=UserName;Password=Secret;" OleDbConnection connection ; connection = new OleDbConnection(ConnectionString); connection.Open();

ODBC DSN

using System.Data.Odbc; ConnectionString = "Dsn=DSNName;Uid=username;Pwd=password;"; OdbcConnection connection ; connection = new OdbcConnection(ConnectionString); connection.Open();

ODBC Local(MyODBC Driver )

using System.Data.Odbc; ConnectionString = "Driver={MySql};Server=localhost;" + "Option=16834;DataBase=databasename;" OdbcConnection connection ; connection = new OdbcConnection(ConnectionString); connection.Open();

ODBC 3.51 Driver

using System.Data.Odbc; ConnectionString = "DRIVER={MySql ODBC 3.51 Driver};SERVER=servername;" + "DATABASE=databasename;USER=username;PASSWORD=password;" OdbcConnection connection ; connection = new OdbcConnection(ConnectionString); connection.Open();

Oracle

.NET DataProvider from Microsoft

using System.Data.OracleClient; ConnectionString = "Data Source=ServerName;User id=UserName;" + "Password=password;"; OracleConnection connection ; connection = new OracleConnection(ConnectionString); connection.Open();

OleDb - Oracle Driver

using System.Data.OleDb; ConnectionString = "Driver=OraOLEDB.Oracle;Data Source=servername;" + "User id=username;Password=passowrd;"; OleDbConnection connection ; connection = new OleDbConnection(ConnectionString); connection.Open();

OleDb - Microsoft Driver

using System.Data.OleDb; ConnectionString = "Driver=MSDAORA;Data Source=servername;" + "User id=username;Password=password;"; OleDbConnection connection ; connection = new OleDbConnection(ConnectionString); connection.Open();

Microsoft Access

OleDb with MS Jet

using System.Data.OleDb; ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\mypath\myDb.mdb;Database Password=password;" OleDbConnection connection ; connection = new OleDbConnection(ConnectionString); connection.Open();

ODBC DSN

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

ODBC - Exclusive Use

using System.Data.Odbc; ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" + "Dbq=c:\path\pub.mdb;Exclusive=1;Uid=Admin;Pwd=password;"; OdbcConnection connection ; connection = new OdbcConnection(ConnectionString); connection.Open();

Web.config and ConnectionString

You can store connection strings in the Web.config file and reference the configuration entries in data source controls. You can create connectionStrings element within the Element element, by create a child element named and place your connection strings there.

  

<configuration>
 <connectionStrings>
   <add name="SQLDbConnection"
		connectionString="Server=SQlServerName; Database=YouDatabaseName; User Id=userid; password= password"
		providerName="System.Data.SqlClient" />
 </connectionStrings>
</configuration>

You can access the connectionstring value at run time in your ASP.NET application as shown in the following example.

vb.net

  Dim conn As String = ConfigurationManager.ConnectionStrings("SQLDbConnection")

C#

  string conn = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString();

From the following links you can see how each dataprovider makes connection to the database from ASP.NET application.

ASP.NET SQL Server Connection

ASP.NET OLEDB Connection

ASP.NET ODBC Connection






net-informations.com (C) 2015    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.