c#数据库连接字符串集合
(一)常用连接:
1.使用SqlConnection对象:
a. Sql 验证
public void SqlConnectionOpen()
{
SqlConnection conn= new SqlConnection();
conn.ConnectionString = "user id=sa;password=;initial catalog=northwind;datasource=localhost;connect Timeout=20";
conn.Open();
}
b. Windows 身份验证
public void SqlConnectionOpen()
{
SqlConnection conn= new SqlConnection();
conn.ConnectionString = "Integrated Security=SSPI;initial catalog=northwind;datasource=localhost;connect Timeout=20";
conn.Open();
}
举例:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="ConnString" connectionString="Data Source=./sqlexpress;Initial Catalog=MetalZoom;User Id=sa;Password=XXXX"/>
<add name="ConnString1" connectionString="Data Source=OPTIPLEX755/SQLEXPRESS;database=MetalZoom;Integrated Security=true"/>
<add name="ConnString2" connectionString="Data Source=OPTIPLEX755/SQLEXPRESS;database=MetalZoom;Integrated Security=SSIP"/>
<add name="ConnString3" connectionString="Data Source=./sqlexpress;database=MetalZoom;Integrated Security=true"/>
<add name="ConnString4" connectionString="Data Source=./sqlexpress;database=metalZoom;integrated Security=true"/>
<add name="ConnString5" connectionString="Data Source=192.168.0.88;Initial Catalog=master;Persist Security Info=True;User ID=sa"/>
</connectionStrings>
</configuration>
2.使用OleDbConnection对象:
public void OleDBConnectionOpen()
{
OleDBConnection conn = new OleDbconnection();
conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:/Customer.mdb";
conn.Open();
}
(二)其它:
1.ODBC连接Access本地数据库
conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=C:/a.mdb;"+
"Uid=Admin;"+"Pwd=;");
2.ODBC连接Access系统数据库
conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=C:/a.mdb;"+
"SystemDB=Admin;"+"Pwd=;");
3.ODBC连接Access系统数据库
conGoodDay.Open("Driver={Microsoft Access Driver(*.mdb)};"+"Dbq=//server/share/a.mdb;");
4.ODBC连接Excel系统数据库
conGoodDay.Open("Driver={Microsoft Access Driver(*.xls)};"+"DriverId=790;"+
"Dbq=C:/a.xls;"+"DefaultDir=c:/somepath;");
5.ODBC连接Oracle系统数据库
conGoodDay.Open("Driver={Microsoft ODBC for oracle};"+"Server=OracleServer.world;"+
"Uid=Admin;"+"Pwd=password;");
6.ODBC连接Sql Servr
conGoodDay.Open("Driver={Sql Server};"+"Server=myServer;"+"Database=myDatabaseName;"
"Uid=Admin;"+"Pwd=password;");
7.ODBC连接Visual FoxPro
conGoodDay.Open("Driver={Microsoft Visual FoxPro Driver};"+
"SourceType=DBC;"+"SourceDB=c:a.dbc;"+"Exclusive=No;");
Windows 身份验证
建议使用 Windows 身份验证(有时也称为“集成安全性”)连接到支持其的数据源。连接字符串中使用的语法根据提供程序的不同而不同。下表演示用于 .NET Framework 数据提供程序的 Windows 身份验证语法。
提供程序
语法
SqlClient
Integrated Security=true;
-- or --
Integrated Security=SSPI;
OleDb
Integrated Security=SSPI;
Odbc
Trusted_Connection=yes;
OracleClient
Integrated Security=yes;
Integrated Security=true 用于 OleDb 提供程序时会引发异常。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/farawayplace613/archive/2009/01/24/3852104.aspx