这是一个已经移植到 .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" + ... }