您的位置:首页 > 其它

ADO.NET 快速入门(十五):ADO 应用转换为 ADO.NET

2017-04-26 22:03 429 查看
这是一个已经移植到 .NET 的 ADO 应用的例子。也演示了单向、只读、快速 DataReader 的使用。它演示如何使用 DataView 类从 DataSet 获取一个 Table 和 操作一个类似于旧的 ADO 记录集模型。请记得,ADO 记录集仅仅包含一个 Table 的数据,但是 ADO.NET DataSet 可以包含多个
Tables 并且非常灵活。

 

原始的 ADO 示例使用 SQL Server 2000 下的 Employee 示例。原始的示例使用 ADO Recordset 对象管理从 SQL 查询返回的结果数据。新的示例演示了如何使用 SqlDataAdapter 填充 DataSet(类似于 ADO Recordset 的模型)。还有,原始的示例使用弹窗(.cpp)和表单(.frm)显示 Northwind 数据库 Employ Table 的输出。然而,在示例中 .NET 示例仅仅使用控制台窗口输出非图形数据。

 

下面示例的要点:

1、连接到数据库

2、使用轻量级、只读、单向的读取器

3、执行 SQL 查询并且生成 ADO Recordset 或者 ADO.NET DataSet

4、使用 ADO Recordset 或者 ADO.NET DataSet 访问指定记录集

 

通常在一个 ADO 应用程序中,包括到数据库的连接和执行 SQL 的查询并且生成 ADO Recordset。

 

在原始的
4000
代码中使用 Visual Basic 版本的 Employee,使用 ADO 连接对象和连接串“server=(local)\SQLExpression;Integrated Security=SSPI;database=northwind;provider=SQLNCLI”。然后记录集通过使用 ADO Recordset.Open 方法打开 SQL‘SELECT’查询。

 

 

' 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


 

   

在 .NET 中的实现非常类似。使用 SqlConnection 对象和连接串“server=(local)\SQLExpress;Integrated Security=SSPI;database=northwind”。读取器通常用于循环执行查询返回的数据。这个读取器(只读、单向的数据读取器)性能优于原生的 ADO 读取器。SqlDataAdapter 通过使用 SQL ‘SELECT’查询和 Fill 方法填充 DataSet。

 

 

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");


 

 

在 ADO 应用程序中,有些函数在 Recordset 中从每个记录和字段组合获取数据。这常常是通过调用 Recordset 的 MoveFirst、MoveLast、MovePrevious 和 MoveNext 方法完成。例如:

 

 

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


 

接着使用 Field 访问器从 Recordset 的当前记录提取数据。例如:

 

 

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


 

   

下例中,DataSet 的 “Employee”Table 分配给 DataView,生成的 DataView 通过迭代提取数据值。通过使用 DataView,你能够把 DataSet 中的任一 Table 转化成 一个功能类似于传统 ADO Recordset 的对象。

 

// 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" +
...
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: