如何利用ADO.NET设计获取架构方法实现

开发 后端
文章这里利用ADO.NET设计获取架构使用OleDbDataReader的GetSchemaTable 方法,还介绍了CommandBehavior.SchemaOnly、CommandBehavior.KeyInfo、CommandBehavior.SchemaOnly。

我们今天利用ADO.NET设计获取架构使用OleDbDataReader的GetSchemaTable 方法,publicvirtualDataTableGetSchemaTable();该ADO.NET设计方法需要结合OleDbCommand.ExecuteReader的一个重载方法才能完成,可以设置重载方法的唯一参数,publicOleDbDataReaderExecuteReader(CommandBehaviorbehavior);CommandBehavior为一枚举对象,定义为

  1. [Flags]  
  2. [Serializable]  
  3. publicenumCommandBehavior 

#T#CommandBehavior.SchemaOnly:只返回列信息,不影响数据库状态;CommandBehavior.KeyInfo:返回列和主键信息。执行此查询时不锁定选定的行。如果使用CommandBehavior.SchemaOnly就不需要再加CommandBehavior.KeyInfo了。如果你从前是个ADO开发人员,现在已经用ADO.NET了,那么你可能把数据访问看做是一个万能的对象,如Recordset。我们很自然地会将旧的对象模式同新的对象模式匹配起来,并将现有的方法用于.NET应用程序。然而,在ADO环境中的某些好的方法在转换到ADO.NET环境时就可能并不强大了。下面是ADO.NET设计演示代码:

  1. privateDataTableGetSchemaUsingOleDbDataReader(stringconnString)  
  2. {  
  3. OleDbConnectionmyConn=newOleDbConnection(connString);  
  4. DataTabletable1=null;  
  5. try  
  6. {  
  7. OleDbCommandcmd=newOleDbCommand("Select*fromCustomers",myConn);  
  8. myConn.Open();  
  9. OleDbDataReaderdataReader=cmd.ExecuteReader(CommandBehavior.SchemaOnly);  
  10. table1=dataReader.GetSchemaTable();  
  11. dataReader.Close();  
  12. }  
  13. catch(Exceptionex)  
  14. {  
  15. MessageBox.Show(ex.ToString());  
  16. }  
  17. finally  
  18. {  
  19. if(myConn.State!=ConnectionState.Closed)  
  20. myConn.Close();  
  21. myConn.Dispose();  
  22. }  
  23. returntable1;  

该方法返回了Customers表的架构信息,当然,如果连接到Access数据库,上面的函数只需修改一下连接字符串和查询字符串就可以正确执行。

责任编辑:田树 来源: 博客
相关推荐

2009-11-11 10:01:56

ADO.NET程序设计

2009-11-13 10:53:54

ADO.NET Dat

2009-11-11 10:55:10

ADO.NET对象

2009-11-04 12:45:33

ADO.NET Dat

2009-12-24 14:39:53

设计ADO.NET

2009-11-11 11:00:38

ADO.NET结果集

2009-11-03 17:12:33

ADO.NET Exe

2009-11-03 17:46:50

ADO.NET Sel

2009-11-12 16:52:23

ADO.NET记录集

2009-12-21 16:02:48

ADO.NET命令

2009-12-28 15:11:36

ADO.NET专家

2009-12-21 11:00:05

ADO.NET 结构

2009-11-04 10:35:42

ADO.NET Con

2009-12-31 13:41:39

ADO.NET架构

2009-11-03 16:37:10

2009-12-30 15:11:35

ADO.NET数据

2011-05-20 11:31:07

ADO.NET

2009-11-12 09:25:21

ADO.NET连接池

2009-11-11 11:19:57

ADO.NET使用

2009-09-14 13:37:25

LINQ ADO.NE
点赞
收藏

51CTO技术栈公众号