vb创建数据表

时间:2022-10-15 14:34:49
Public Sub Create_TABLE(TableSTR As String)
'----------------------创建表---------------------------
'TableSTR 创建表的命令字符串
'-------------------------------------------------------
    On Error GoTo errc
    Dim connectstring As String
    Dim con2 As ADODB.Connection
    Dim Trs As ADODB.Recordset
    Dim i As Integer
    Dim SQL As String
    Dim SQLstr() As String
    Set con2 = New ADODB.Connection
    Call Open_Recordset(Trs)
    SQL = "select * from Data_info"
    Trs.Open SQL
    If Trs.EOF Then
       MsgBox "请先创建数据库!"
       Exit Sub
    End If
    'connectstring = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Northwind;Data Source=."
    connectstring = "Driver=SQL Server;Server=" & Trs("IP") & ";UID=" & Trs("Servername") & ";PWD=" & Trs("Serverpwd") & ";Database=" & Trs("Dataname")
    con2.Open connectstring
    SQLstr = Split(TableSTR, "GO")
    For i = 0 To UBound(SQLstr)
        DoEvents
        con2.Execute (SQLstr(i))
    Next i
    con2.Close
    Exit Sub
errc:
      MsgBox "建表失败,请按正确的格式输入建表命令!" & Chr(13) & Chr(10) & _
         "例如:" & Chr(13) & Chr(10) & _
         "CREATE TABLE 表名" & Chr(13) & Chr(10) & _
         "(" & Chr(13) & Chr(10) & _
         "字段名1 数据类型 约束方式," & Chr(13) & Chr(10) & _
         ".............." & Chr(13) & Chr(10) & _
         "字段名N 数据类型 约束方式" & Chr(13) & Chr(10) & _
         ")", vbOKOnly + vbInformation, "提示"
End Sub