下面是一个简单的 ADO.NET 应用程序,它从数据源中返回结果并将输出写至控制台或命令提示符窗口。通过 ADO.NET 访问数据中提供的示例代码的大部分都可以放入利用此示例创建的模板,以查看特定 ADO.NET 功能的工作示例。
该示例显示包含在 ADO.NET 应用程序中的典型命名空间。OLE DB 客户端的命名空间不同于 SQL Server 客户端的命名空间。对于 SQL Server .NET 数据提供程序 (System.Data.SqlClient) 和 OLE DB .NET 数据提供程序 (System.Data.OleDb) 都显示了相应的示例。如果需要,可以在单个应用程序中同时使用 SQL Server .NET 数据提供程序和 OLE DB .NET 数据提供程序。
以下示例连接到 Microsoft SQL Server 2000 上的 Northwind 数据库,并使用 DataReader(有关 DataReader 更多信息,请参阅使用 DataReader 检索数据)返回一个“类别”(Categories) 列表。
编译和运行 ADO.NET 示例应用程序
cd\SampleCode\ADONETSample
vbc.exe sample.vb /r:System.dll /r:System.Data.dll /r:System.Xml.dll
csc.exe sample.cs /r:System.dll /r:System.Data.dll /r:System.Xml.dll
[Visual Basic]
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic
Public Class Sample
Public Shared Sub Main()
Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;" & _
"Integrated Security=SSPI;Initial Catalog=northwind")
Dim catCMD As SqlCommand = nwindConn.CreateCommand()
catCMD.CommandText = "SELECT CategoryID, CategoryName FROM Categories"
nwindConn.Open()
Dim myReader As SqlDataReader = catCMD.ExecuteReader()
Do While myReader.Read()
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}", myReader.GetInt32(0), myReader.GetString(1))
Loop
myReader.Close()
nwindConn.Close()
End Sub
End Class
[C#]
using System;
using System.Data;
using System.Data.SqlClient;
class Sample
{
public static void Main()
{
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
SqlCommand catCMD = nwindConn.CreateCommand();
catCMD.CommandText = "SELECT CategoryID, CategoryName FROM Categories";
nwindConn.Open();
SqlDataReader myReader = catCMD.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
}
myReader.Close();
nwindConn.Close();
}
}
[Visual Basic]
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic
Public Class Sample
Public Shared Sub Main()
Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" & _
"Integrated Security=SSPI;Initial Catalog=northwind")
Dim catCMD As OleDbCommand = nwindConn.CreateCommand()
catCMD.CommandText = "SELECT CategoryID, CategoryName FROM Categories"
nwindConn.Open()
Dim myReader As OleDbDataReader = catCMD.ExecuteReader()
Do While myReader.Read()
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}", myReader.GetInt32(0), myReader.GetString(1))
Loop
myReader.Close()
nwindConn.Close()
End Sub
End Class
[C#]
using System;
using System.Data;
using System.Data.OleDb;
class Sample
{
public static void Main()
{
OleDbConnection nwindConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
OleDbCommand catCMD = nwindConn.CreateCommand();
catCMD.CommandText = "SELECT CategoryID, CategoryName FROM Categories";
nwindConn.Open();
OleDbDataReader myReader = catCMD.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
}
myReader.Close();
nwindConn.Close();
}
}