1.使用SqlConnection对象: a. Sql 验证
public void SqlConnectionOpen()
{
SqlConnection conn= new SqlConnection();
conn.ConnectionString = "user id=sa;password=;
initial catalog=northwind;
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();
}
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 提供程序时会引发异常。
大神们的总结:
http://blog.csdn.net/skh429170331/article/details/43062591
http://blog.csdn.net/xijiaoda_liuhao/article/details/8312945
http://www.cnblogs.com/52yixin/articles/1822057.html
转载至:
http://www.cnblogs.com/52yixin/articles/1822057.html