检查表存在 [英] Checking for table existence

查看:65
本文介绍了检查表存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何通过名称检查某个表的存在并访问

数据库,如果该表存在,则查找记录数在它?


我想我可以通过为

数据库设置数据集来访问这些信息,但我想知道是否有一种快速的方法可以找到这个。


谢谢


问候

Hi

How can I check for existence of a certain table by name in and access
database and if the table exists find the number of records in it?

I guess I can access this information by setting up a dataset for the
database but I am wondering if there is a quick way to find this.

Thanks

Regards

推荐答案

发行try catch循环中的count select语句可以同时处理两个?


如果表存在且错误,它会给你总记录并输入

catch代码,如果它不是吗?

Rob


" John" < Jo ** @ nospam.infovis.co.ukwrote in message

news:%2 **************** @ TK2MSFTNGP02.phx.gbl。 ..
Issuing a count select statement within a try catch loop would handle both?

It would give you the total records if the table exists and error and enter
the catch code if it doesn''t?
Rob

"John" <Jo**@nospam.infovis.co.ukwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...




如何通过名称检查某个表的存在并访问

数据库,如果表存在,找到其中的记录数量?


我想我可以通过设置
$ b的数据集来访问这些信息$ b数据库,但我想知道是否有快速找到这个。


谢谢


问候


" Rob Blackmore" < ro*@robblackmore.com写在

新闻:66 ****************************** **** @ microsof t.com:
"Rob Blackmore" <ro*@robblackmore.comwrote in
news:66**********************************@microsof t.com:

在try catch循环中发出count select语句会处理

吗?


如果表存在并且错误,它将为您提供总记录,如果不存在,则输入捕获代码


Issuing a count select statement within a try catch loop would handle
both?

It would give you the total records if the table exists and error and
enter the catch code if it doesn''t?



更好的方法是使用ADO.NET的GetSchema或直接查询元数据

表来检查表是否存在。

-
sp ********** @ rogers.com (不要发电子邮件)

A better way would be to use ADO.NET''s GetSchema or query the metadata
table directly to check if a table exists.
--
sp**********@rogers.com (Do not e-mail)


John写道:
John wrote:

如何我可以通过名称检查某个表的存在并访问

数据库,如果该表存在,则查找其中的记录数量?
How can I check for existence of a certain table by name in and access
database and if the table exists find the number of records in it?



Rob Blackmore的方法肯定会有效,但是如果你想检查

表是否存在而没有得到例外如果它不存在

(这绝对是我的首选方法),这里有一种方法可行:


\\\\ \\ / $
Dim tableInfo As DataTable

tableInfo = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,New

String(){Nothing,Nothing," TableName"," TABLE"})

///


GetOleDbSchemaTable方法将返回包含架构的数据表

信息。这组参数将返回有关表格的信息

,称为TableName。如果存在的话。

执行此操作后检查tableInfo.Rows.Count;如果它包含零,则该表不存在,如果它b / b
包含它,那么它确实存在。


然后如果它存在,执行SELECT在Rob's $

电子邮件中提到的COUNT声明。


HTH,


-


(O)enone

Rob Blackmore''s approach will certainly work, but if you prefer to check for
the existence of the table without getting an exception if it doesn''t exist
(which is definitely my preferred approach), here''s a way that will work:

\\\
Dim tableInfo As DataTable
tableInfo = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables , New
String() {Nothing, Nothing, "TableName", "TABLE"})
///

The GetOleDbSchemaTable method will return a datatable containing schema
information. This set of parameters will return information about a table
called "TableName" if one exists. Check the tableInfo.Rows.Count after
executing this; if it contains zero then the table doesn''t exist, if it
contains one then it does exist.

Then if it exists, execute the SELECT COUNT statement as mentioned in Rob''s
email.

HTH,

--

(O)enone


这篇关于检查表存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆