日期:2013-10-04  浏览次数:20426 次

The following example shows what a simple ADO.NET application that connects to the Northwind database and returns a list of Categories would look like. The example writes the output to the console, or command prompt.<br>
<br>
The following example shows what a simple ADO.NET application that connects to the Northwind database and returns a list of Categories. The example writes the output to the console, or command prompt.<br>
<br>
SqlClient<br>
[Visual Basic]<br>
Imports System<br>
Imports System.Data<br>
Imports System.Data.SqlClient<br>
Imports Microsoft.VisualBasic<br>
<br>
Public Class Sample<br>
<br>
  Public Shared Sub Main() <br>
    Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;" & _<br>
                                                       "User Id=sa;Password=pwd;Initial Catalog=northwind")<br>
<br>
    Dim catCMD As SqlCommand = nwindConn.CreateCommand()<br>
    catCMD.CommandText = "SELECT CategoryID, CategoryName FROM Categories"<br>
<br>
    nwindConn.Open()<br>
<br>
    Dim myReader As SqlDataReader = catCMD.ExecuteReader()<br>
<br>
    Do While myReader.Read()<br>
      Console.WriteLine(vbTab & "{0}" & vbTab & "{1}", myReader.GetInt32(0), myReader.GetString(1))<br>
    Loop<br>
<br>
    myReader.Close()<br>
    nwindConn.Close()<br>
  End Sub<br>
End Class<br>
[C#]<br>
using System;<br>
using System.Data;<br>
using System.Data.SqlClient;<br>
<br>
class Sample<br>
{<br>
  public static void Main() <br>
  {<br>
    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;User Id=sa;Password=pwd;Initial Catalog=northwind");<br>
<br>
    SqlCommand catCMD = nwindConn.CreateCommand();<br>
    catCMD.CommandText = "SELECT CategoryID, CategoryName FROM Categories";<br>
<br>
    nwindConn.Open();<br>
<br>
    SqlDataReader myReader = catCMD.ExecuteReader();<br>
<br>
    while (myReader.Read())<br>
    {<br>
      Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));<br>
    }<br>
<br>
    myReader.Close();<br>
    nwindConn.Close();<br>
  }<br>
}<br>
OleDb<br>
[Visual Basic]<br>
Imports System<br>
Imports System.Data<br>
Imports System.Data.OleDb<br>
Imports Microsoft.VisualBasic<br>
<br>
Public Class Sample<br>
<br>
  Public Shared Sub Main() <br>
    Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" & _<br>