数据库
首页 > 数据库> > 搬家第一天-1.WinccV7.3 使用VBS判断数据库Mydatabase下是否存在数据表Mytable

搬家第一天-1.WinccV7.3 使用VBS判断数据库Mydatabase下是否存在数据表Mytable

作者:互联网

从今天开始搬运某浪上以前写的内容

Sub OnClick(Byval Item)                          
Dim ors,conn,con,ssql,ocom
Dim ors1,conn1,con1,ssql1,ocom1
Dim ocatalog,otable
Dim i,TableExist
Dim PCName
PCName=HMIRuntime.Tags("@LocalMachineName").Read
con="Provider = SQLOLEDB.1;password = sa;user id = sa;Initial Catalog =master;Data Source = " & PCName & "\WINCC"
Set conn=CreateObject("ADODB.Connection")
conn.ConnectionString=con
conn.Cursorlocation=3
conn.open
ssql="select * from master.dbo.sysdatabases where [name] ='MyDataBase'"
Set ors=CreateObject("ADODB.RecordSet")
Set ocom=CreateObject("ADODB.Command")
Set ocom.activeconnection=conn
ocom.CommandType=1
ocom.CommandText=ssql
Set ors=ocom.Execute
If ors.recordcount>0 Then
   con1="Provider = SQLOLEDB.1;password = sa;user id = sa;Initial Catalog =Mydatabase;Data Source = " & PCName & "\WINCC"
   Set conn1=CreateObject("ADODB.Connection")
  conn1.ConnectionString=con1
   conn1.Cursorlocation=3
   conn1.open
   ssql="select name from SYSOBJECTS where type ='U'"
   Set ors1=CreateObject("ADODB.RecordSet")
   Set ocom1=CreateObject("ADODB.Command")
   Set ocom1.activeconnection=conn1
   ocom1.CommandType=1
   ocom1.CommandText=ssql
   Set ors1=ocom1.Execute
   If ors1.recordcount>0 Then
     ors1.movefirst
     For i=1 To ors1.recordcount
        If UCase(ors1.fields("name").value)=Ucase("mytable") Then   '大小写敏感
           TableExist=True
           Exit For
        Else
           ors1.movenext
        End If
     Next
   Else
    TableExist=False
   End If
   If TableExist Then
     Msgbox "表存在"
   Else
     Msgbox "表不存在"
   End If
Else     
    Msgbox "数据库MyDataBase不存在,表也不存在"
End If
Set ors=Nothing
conn.close
Set conn=Nothing
Set ors1=Nothing
conn1.close
Set conn1=Nothing
End Sub

标签:Set,WinccV7.3,CreateObject,ors,VBS,数据表,ADODB,conn1,ors1
来源: https://www.cnblogs.com/fishingsriver/p/14342879.html