Enterprise Library 4.1数据访问应用程序块快速入门【7】在XML中进行多行检索
2010-01-13 12:28
537 查看
本演练演示如何从SQL Server数据库中检索XML数据。
1、配置数据库。必要的步骤,请参阅数据访问快速入门。
2、创建Database(当你不使用统一的集成方法)。下面的代码使用工厂创建一个默认的配置的SqlDatabase对象。
3、使用下面代码创建一个命令。它使用SQL字符串创建了一个DbCommand。
4、添加下面的代码调用ExecuteXmlReader并处理结果
5、使用下面的代码关闭XmlReader和Connection。
文章由唐勇(http://www.tangyong.net/)翻译自微软Enterprise Library系列英文文档,翻译得辛苦,转载请保留,谢谢!
1、配置数据库。必要的步骤,请参阅数据访问快速入门。
2、创建Database(当你不使用统一的集成方法)。下面的代码使用工厂创建一个默认的配置的SqlDatabase对象。
SqlDatabase dbSQL = DatabaseFactory.CreateDatabase() as SqlDatabase;
Dim dbSQL As SqlDatabase = DirectCast(DatabaseFactory.CreateDatabase(), SqlDatabase)
3、使用下面代码创建一个命令。它使用SQL字符串创建了一个DbCommand。
string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, " + "LastUpdate From Products FOR XML AUTO"; DbCommand dbCommand = dbSQL.GetSqlStringCommand(sqlCommand);
Dim sqlCommand As String = "Select ProductID, ProductName, CategoryID, " _ & " UnitPrice, LastUpdate From Products FOR XML AUTO" Dim dbCommand As DbCommand = dbSQL.GetSqlStringCommand(sqlCommand)
4、添加下面的代码调用ExecuteXmlReader并处理结果
XmlReader productsReader = null; StringBuilder productList = new StringBuilder(); try { productsReader = dbSQL.ExecuteXmlReader(dbCommand); while (!productsReader.EOF) { if (productsReader.IsStartElement()) { productList.Append(productsReader.ReadOuterXml()); productList.Append(Environment.NewLine); } } }
Dim productsReader As XmlReader = Nothing Dim productList As StringBuilder = New StringBuilder() Try productsReader = dbSQL.ExecuteXmlReader(dbCommand) While (Not productsReader.EOF) If (productsReader.IsStartElement()) Then productList.Append(productsReader.ReadOuterXml()) productList.Append(Environment.NewLine) End If End While
5、使用下面的代码关闭XmlReader和Connection。
finally { // Close the Reader. if (productsReader != null) { productsReader.Close(); } // Explicitly close the connection. The connection is not closed // when the XmlReader is closed. if (dbComman.Connection != null) { dbCommand.Connection.Close(); } }
Finally ' Close the Reader. If (Not productsReader Is Nothing) Then productsReader.Close() End If ' Explicitly close the connection. The connection is not closed ' when the XmlReader is closed. If (Not dbCommand.Connection Is Nothing) Then dbCommand.Connection.Close() End If End Try
文章由唐勇(http://www.tangyong.net/)翻译自微软Enterprise Library系列英文文档,翻译得辛苦,转载请保留,谢谢!
相关文章推荐
- Enterprise Library 4.1数据访问应用程序块快速入门【1】使用DbDataReader检索多行
- Enterprise Library 4.1数据访问应用程序块快速入门【2】使用DataSet检索多行
- Enterprise Library 4.1数据访问应用程序块快速入门【5】在事务内执行多个更新程序
- Enterprise Library 4.1数据访问应用程序块快速入门【6】使用DataSet更新数据库
- Enterprise Library 4.1数据访问应用程序块快速入门【3】执行命令和访问输出参数
- Enterprise Library 4.1数据访问应用程序块快速入门【4】执行命令和访问一单项目结果
- Flex 3快速入门: 处理数据 访问 XML 数据
- Enterprise Library 数据访问应用程序块快速入门
- java线程基础巩固---线程间通信快速入门,使用wait和notify进行线程间的数据通信
- Android入门:通过XML数据与服务器进行通信
- ADO.NET 快速入门(十二):从 SQL Server 生成 XML 数据
- ADO.NET 快速入门(十四):使用 SQL Server 检索数据
- ADO.NET 快速入门(十二):从 SQL Server 生成 XML 数据
- PHP入门之局域网多站点访问、带参数请求、Json数据、Xml数据
- ASP.NET MVC3 快速入门-第五节 从控制器访问模型中的数据(转)
- Android入门:通过XML数据与服务器进行通信
- ASP.NET MVC3快速入门——第五节、从控制器访问模型中的数据
- ADO.NET 快速入门(十三):使用 OLE DB 检索数据
- Android入门:通过XML数据与服务器进行通信
- ASP.NET MVC3 快速入门-第五节 从控制器访问模型中的数据