select * from sysobjects where XType = 'U' AND name=表名 if @@rowcounr>0 就存在
我的代码如下:各位看一下 Dim flag As Boolean Dim cnn As New ADODB.Connection Dim rs_Tabs As New ADODB.Recordset Dim count As Integer Dim carID() As String我用carID()记录的表名,需要判断数据库中是否有这几个表? '******判断用于生成报表的表是否在数据库中存在********** Set rs_Tabs = cnn.OpenSchema(adSchemaTables) '创建数据库记录集为了得到数据库中所有表名 Do While Not rs_Tabs.EOF For i = 1 To Count If carID(i) = rs_Tabs!TABLE_NAME Then 提示该行出错(eof,bof......数据越界) flag = True MsgBox flag Exit Do End If rs_Tabs.MoveNext Next Loop rs_Tabs.Close
错误拦截 + 直接删除Private Sub ......Dim cnn As New ADODB.Connection On Error Goto EH cnn.Open "......",...... On Error Resume Next cnn.Execute "Drop Table yourTable" On Error Goto EH '......Exit SubEH: MsgBox "实时错误" & Err.Number & ":" & vbCrLf & Err.Description End Sub
select * from sysobjects where XType = 'U' AND name=表名 if @@rowcounr>0 就存在 drop table 表名
For i = 1 To Count If carID(i) = rs_Tabs!TABLE_NAME Then 提示该行出错(eof,bof......数据越界)--数组下标应该是0才对 for i = 0 to count-1好像还漏了redim preserve carID(i)
TO:sdjack(爱喝橙汁的蚂蚁), jilong4(凹凹) select * from sysobjects where XType = 'U' AND name=表名 if @@rowcounr>0 就存在 里面的sysobjects代表什么, @@rowcounr>0 又是什么东东? 能解释一下嘛?
拜托 楼主,这个就是一个 sql 问题 '假设 conn 为 adodb.connection 对象conn.Execute "if exists(select * from sysobjects where XType = 'U' AND name='表名') drop table 表名"'或者 rs.open "select * from sysobjects where XType = 'U' AND name='表名'",conn if not rs.open then conn.execute "drop table 表名" end if rs.close '其中: sysobjects 是 系统表,看名字就知道了,是存储系统对象 的一个表 '每个数据库中都有,存储本数据库中所有的 表,视图,存储过程,触发器,函数 ........ 'xtype 表示 数据类型 其中 U 表示 用户表; V 表示 视图 ........ '解释到这里,往下不用说了吧 呵呵 '没事多看看 系统表里的东东,挺好玩的
if @@rowcounr>0 就存在
Dim flag As Boolean
Dim cnn As New ADODB.Connection
Dim rs_Tabs As New ADODB.Recordset
Dim count As Integer
Dim carID() As String我用carID()记录的表名,需要判断数据库中是否有这几个表?
'******判断用于生成报表的表是否在数据库中存在**********
Set rs_Tabs = cnn.OpenSchema(adSchemaTables) '创建数据库记录集为了得到数据库中所有表名
Do While Not rs_Tabs.EOF
For i = 1 To Count
If carID(i) = rs_Tabs!TABLE_NAME Then 提示该行出错(eof,bof......数据越界)
flag = True
MsgBox flag
Exit Do
End If
rs_Tabs.MoveNext
Next
Loop
rs_Tabs.Close
cnn.Open "......",......
On Error Resume Next
cnn.Execute "Drop Table yourTable"
On Error Goto EH
'......Exit SubEH:
MsgBox "实时错误" & Err.Number & ":" & vbCrLf & Err.Description
End Sub
我现在的情况是程序运行的时候向表中加记录
当程序关闭再运行的时候,这个时候需要进行判断,如果表存在则打开表进行添加记录
我想用select error的方法可以实现巴
if @@rowcounr>0 就存在
drop table 表名
If carID(i) = rs_Tabs!TABLE_NAME Then 提示该行出错(eof,bof......数据越界)--数组下标应该是0才对
for i = 0 to count-1好像还漏了redim preserve carID(i)
select * from sysobjects where XType = 'U' AND name=表名
if @@rowcounr>0 就存在
里面的sysobjects代表什么, @@rowcounr>0 又是什么东东?
能解释一下嘛?
@@rowcount 表示上次操作数据的行数
比如我用ado连接的数据库,rs为数据集,数据库已经存在,并且打开,要看里面是否存在名为test的表,用上面的方法如何写?
'假设 conn 为 adodb.connection 对象conn.Execute "if exists(select * from sysobjects where XType = 'U' AND name='表名') drop table 表名"'或者
rs.open "select * from sysobjects where XType = 'U' AND name='表名'",conn
if not rs.open then
conn.execute "drop table 表名"
end if
rs.close
'其中: sysobjects 是 系统表,看名字就知道了,是存储系统对象 的一个表
'每个数据库中都有,存储本数据库中所有的 表,视图,存储过程,触发器,函数 ........
'xtype 表示 数据类型 其中 U 表示 用户表; V 表示 视图 ........
'解释到这里,往下不用说了吧 呵呵
'没事多看看 系统表里的东东,挺好玩的
@@rowcount在vb 中没有这个用法
但可以这么用dim strSql as string strSql = "select * from sysobjects where xtype = 'u' and [name] = '表名'"
strsql = chr(10) & strsql & " if @@rowcount = 0 "
strsql = chr(10) & strsql & " select 0 as [TableExists]"
strsql = chr(10) & strsql & " else "
strsql = chr(10) & strsql & " select 1 as [TableExists]"set rs = Conn.execute(strsql)if rs.fields(0) = "1" then
'表存在,想怎么处理都可以
else
‘表不存在,就不要费心了
end if
rs.close'strsql 放了那么多的 换行符 只是为了保证格式好看,等将来调试的时候 在立即窗口中生成的 sql代码整齐,调试方便罢了
因为access 也是有系统表的,通过改变 数据库的选项可以看到系统表
(因为我没装 access 所以不能演示)然后就可以用我的第一个帖 中的第二个方法 进行操作
"通过改变 数据库的选项可以看到系统表" 能具体说一下吗?
工具 -> 选项 ; 在 “视图”标签中,把 系统对象(Y) 选中 就可以了
系统表中 的 “MSysObjects” 就是存储系统对象的表了
至于怎么 实现 帖子中的功能,你就自己试试吧呵呵strsql = "select * from MSysObjects where [name] = '表名' and [type] = 1;"access 比较恶心,不能用存储过程
只好用 记录集 来实现了。