這是一個已經移植到 .NET 的 ADO 應用的例子。也演示了單向、只讀、快速 DataReader 的使用。它演示如何使用 DataView 類從 DataSet 獲取一個 Table 和 操做一個相似於舊的 ADO 記錄集模型。請記得,ADO 記錄集僅僅包含一個 Table 的數據,可是 ADO.NET DataSet 能夠包含多個 Tables 而且很是靈活。
' Open the database. cn.Open("server=(local)\SQLExpress;Integrated Security=SSPI;database=northwind;provider=SQLNCLI") ' Open the Recordset. Set rs = New ADODB.Recordset rs.Open "select * from Employees", cn, adOpenKeyset, adLockPessimistic ' Move to the first record and display the data. rs.MoveFirst FillDataFields
SqlConnection mySqlConnection = new SqlConnection("server=(local)\\SQLExpress;Integrated Security=SSPI;database=northwind"); SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("select * from employees", mySqlConnection); DataSet myDataSet = new DataSet(); mySqlDataAdapter.Fill(myDataSet,"Employees");
If rs.EOF = False Then If rs.BOF = True Then rs.MoveFirst End If rs.MoveNext End If If rs.EOF = False Then FillDataFields End If
For Each fld In Flds FieldSize = fld.ActualSize If FieldSize > 0 Then Select Case fld.Name Case "EmployeeID" txtEID.Text = Str(fld.Value) Case "LastName" txtLastName.Text = fld.Value Case "FirstName" txtFirstName.Text = fld.Value Case "Title" txtTitle.Text = fld.Value ... End Select End If Next
// Create a new dataview instance on the Employees table that was just created DataView myDataView = new DataView(myDataSet.Tables["Employees"]); // Sort the view based on the first column name. myDataView.Sort = "EmployeeID"; int iReportsTo; for (int i = 0; i < myDataView.Count; i++) { Console.Write("\n************************ Employee number " + (i+1).ToString() + " ************************\n"); Console.Write("EmployeeID:\t" + myDataView[i]["EmployeeID"].ToString() + "\n" + "FirstName:\t" + myDataView[i]["FirstName"].ToString() + "\n" + "LastName:\t" + myDataView[i]["LastName"].ToString() + "\n" + "Title:\t\t" + myDataView[i]["Title"].ToString() + "\n" + "TitleOfCourtesy:" + myDataView[i]["TitleOfCourtesy"].ToString() + "\n" + ... }