目前,ADO.NET提供了两种数据库访问类库:一种用于SQL Server 7.0 或更高版本,另一种用于其他所有您可能已经安装的OLE DB提供程序。在这两种情况下,您分别使用不同的类,但遵循相似的命名规则。除前缀,名称都是相同的。前一种情况前缀为Sql,后一种情况则是OleDb。
同时,.NET框架还提供了ODBC .NET的数据访问模式。ODBC .NET Data Provider是 .NET 框架的增强组件,它可以访问原始的 ODBC 驱动程序,就像 OLE DB .NET Data Provider 可以访问原始的 OLE DB providers 一样。目前它仅在下列驱动程序中测试过:Microsoft SQL ODBC Driver,Microsoft ODBC Driver for Oracle,Microsoft Jet ODBC Driver。
下面这段示例代码来自与MSDN,提供了两种数据库访问模式:
SqlClient
[Visual Basic]
Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")
Dim selectCMD As SqlCommand = New SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn)
selectCMD.CommandTimeout = 30
Dim custDA As SqlDataAdapter = New SqlDataAdapter
custDA.SelectCommand = selectCMD
nwindConn.Open()
Dim custDS As DataSet = New DataSet
custDA.Fill(custDS, "Customers")
nwindConn.Close()
OleDb
[Visual Basic]
Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" & _
"Integrated Security=SSPI;Initial Catalog=northwind")
Dim selectCMD As OleDbCommand = New OleDbCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn)
selectCMD.CommandTimeout = 30
Dim custDA As OleDbDataAdapter = New OleDbDataAdapter
custDA.SelectCommand = selectCMD
Dim custDS As DataSet = New DataSet
custDA.Fill(custDS, "Customers")
<%
set adocon=Server.Createobject("adodb.connection")
adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _
Server.MapPath("数据库所在路径")
%>
2.Access OLE DB连接方法:
<%
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _
"Data Source=" & Server.MapPath("数据库所在路径")
%>
3.SQL server连接方法:
<%
set adocon=server.createobject("adodb.recordset")
adocon.Open"Driver={SQL Server};Server=(Local);UID=***;PWD=***;"& _
"database=数据库名;"
%>
4.SQL server OLE DB连接方法:
<%
set adocon=Server.Createobject("adodb.connection")
adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _
"user ID=***;Password=***;"& _
"inital Catalog=数据库名"
%>
5.Oracle 连接方法:
<%
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"
%>
6.Oracle OLE DB 连接方法:
<%
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"
%>
7.dBase 连接方法:
<%
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"
%>
8.mySQL 连接方法:
<%
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={mysql};database=yourdatabase;
uid=username;pwd=yourpassword;option=16386;"
%>
9.Visual Foxpro 连接方法:
<%
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"
%>
10.MS text 连接方法:
<%
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_
"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"
%>
11.MS text OLE DB 连接方法:
<%
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_
"Extended Properties'text;FMT=Delimited'"
%>
%>
<%
starttime=timer()
StrSQL="DBQ="+server.mappath("admin/data/news30000.mdb")+";DRIVER={Microsoft Access Driver (*.mdb)};"
conn.open StrSQL
r
%>