Tips for Database ConnectionStrings of Various Provider
Microsoft SQL Server
// ODBC DSN
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Dsn=DsnName;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
// ODBC — Trusted Connection
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Driver={SQL Server};” + “Server=ServerName;” + “DataBase=DataBaseName;” + “Uid=;” + “Pwd=;”;
conn.Open();
// OleDb — Trusted Connection
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = “Driver=SQLOLEDB;” + “Data Source=ServerName;” + “Initial Catalog=DataBaseName;” + “Integrated Security=SSPI;”;
conn.Open();
// OleDb — via IP Address
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = “Driver=SQLOLEDB;” + “Network Library=DBMSSOCN;” + “Data Source=xxx.xxx.xxx.xxx,1433;” + “Initial Catalog=DataBaseName;” + “User id=UserName;” + “Password=Secret;”;
conn.Open();
// .NET DataProvider — Trusted Connection
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = “Data Source=ServerName;” + “Initial Catalog=DataBaseName;” + “Integrated Security=SSPI;”;
conn.Open();
// .NET DataProvider — Standard Connection
using System.Data.SqlClient;
SqlConnection conn = new SqlDbConnection();
conn.ConnectionString = “Data Source=ServerName;” + “Initial Catalog=DataBaseName;” + “User id=UserName;” + “Password=Secret;”;
conn.Open();
Microsoft Sql Express
// .NET Data Provider — Default Relative Path
// Standard Connection
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = “Data Source=.\SQLExpress;” + “User Instance=true;” + “User Id=UserName;” + “Password=Secret;” + “AttachDbFilename=|DataDirectory|DataBaseName.mdf;”
conn.Open();
// .NET Data Provider — Default Relative Path
// Trusted Connection
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = “Data Source=.\SQLExpress;” + “User Instance=true;” + “Integrated Security=true;” + “AttachDbFilename=|DataDirectory|DataBaseName.mdf;”
conn.Open();
// .NET Data Provider — Absolute Path
// Trusted Connection
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = “Data Source=.\SQLExpress;” + “User Instance=true;” + “Integrated Security=true;” + “AttachDbFilename=C:\MyPath\DataBaseName.mdf;”
conn.Open();
Microsoft Access
// ODBC DSN
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Dsn=DsnName”;
conn.Open();
// ODBC — Standard Security
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();
// ODBC — Workgroup (System Database)
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Driver={Microsoft Access Driver (*.mdb)};” + “Dbq=c:\myPath\myDb.mdb;” + “SystemDb=c:\myPath\myDb.mdw;”;
conn.Open();
// OleDb with MS Jet — Standard Security
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;” + “Data Source=c:\mypath\myDb.mdb;” + “User id=admin;” + “Password=”;
conn.Open();
Oracle
// ODBC DSN
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Dsn=DsnName;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
// ODBC — New Microsoft Driver
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Driver={Microsoft ODBC for Oracle};” + “Server=OracleServer.world;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
// OleDb — Oracle Driver — Standard Connection
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = “Driver=OraOLEDB.Oracle;” + “Data Source=ServerName;” + “User id=UserName;” + “Password=Secret;”;
conn.Open();
// .NET DataProvider from Oracle
// — Standard Connection
using Oracle.DataAccess.Client;
OracleConnection conn = new OracleConnection();
conn.ConnectionString = “Data Source=ServerName;” + “User id=UserName;”; “Password=Secret;”;
conn.Open();
IBM DB2
// ODBC DSN
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Dsn=DsnName;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
// ODBC without DSN
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Driver={IBM DB2 ODBC DRIVER};” + “DataBase=DataBaseName;” + “HostName=ServerName;” + “Protocol=TCPIP;” + “Port=PortNumber;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
// .NET DataProvider from IBM
using IBM.Data.DB2;
Db2Connection conn = new Db2Connection();
conn.ConnectionString = “DataBase=DataBaseName;” + “Uid=UserName;” + “Pwd=Secret;” +
conn.Open();
MySql
// .NET DataProvider from CoreLab
using CoreLab.MySql;
MySqlConnection conn = new MySqlConnection();
conn.ConnectionString = “Host=ServerName;” + “DataBase=DataBaseName;” + “Protocol=TCP;” + “Port=3306;” + “Direct=true;” + “Compress=false;” + “Pooling=true;” + “Min Pool Size=0;” + “Max Pool Size=100;” + “Connection Lifetime=0;” + “User id=UserName;” + “Password=Secret;” +
conn.Open();
// OleDb
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = “Provider=MySqlProv;” + “Data Source=ServerName;” + “User id=UserName;” + “Password=Secret;”
conn.Open();
Sybase
// ODBC DSN
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Dsn=DsnName;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
// ODBC — SQL Anywhere
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “ODBC;” + “Driver={Sybase SQL Anywhere 5.0};” + “DefaultDir=c:\myfolder\;” + “Dbf=c:\mypath\dbname.db;” + “Uid=UserName;” + “Pwd=Secret;” + “Dsn=””””;”; // Must be included!
conn.Open();
// OleDb — Sybase Adaptive Server Enterprise (ASE)
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = “Driver=Sybase.ASEOLEDBProvider;” + “Server Name=ServerName,5000;” + “Initial Catalog=DataBaseName;” + “User id=UserName;” + “Password=Secret;”;
conn.Open();
// .NET DataProvider from Sybase
using Sybase.Data.AseClient;
AseConnection conn = new AseConnection();
conn.ConnectionString = “Data Source=ServerName;” + “Initial Catalog=DataBaseName;” + “User id=UserName;” + “Password=Secret;”;
conn.Open();
Interbase
// ODBC DSN
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Dsn=DsnName;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
// ODBC — EasySoft ODBC Driver — remote machine
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Driver={Easysoft IB6 ODBC};” + “Server=ServerName;” + “DataBase=ServerName:C:\MyPath\DbName.gdb;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
Informix
// ODBC DSN — INFORMIX 3.30 ODBC Driver
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Dsn=DsnName;” + “Host=HostName;” + “Server=ServerName;” + “Service=ServerName;” + “Protocol=olsoctcp;” + “Database=DataBaseName;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
// ODBC without DSN — INFORMIX 3.30 ODBC Driver
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Dsn=””;” + “Driver={INFORMIX 3.30 32 BIT};” + “Host=HostName;” + “Server=ServerName;” + “Service=ServerName;” + “Protocol=olsoctcp;” + “Database=DataBaseName;” + “Uid=UserName;” + “Pwd=Secret;”;
conn.Open();
dBase Dbf
// ODBC without DSN
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = “Driver={Microsoft dBASE Driver (*.dbf)};” + “Driverid=277;” + “Dbq=C:\MyPath\”;
conn.Open();
// Use: sql = “Select * From MyDb.dbf”
Visual FoxPro
// OleDb — Database container (dbc)
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = “Driver=VFPOLEDB;” + “Data Source=C:\MyPath\MyDb.dbc;” + “Collating Sequence=machine;” + “Password=Secret;”;
conn.Open();
Discover more from mycodetips
Subscribe to get the latest posts sent to your email.