'引用ADO: Private Sub Command1_Click() Dim cn As New ADODB.Connection cn.CursorLocation = adUseClient cn.Open "Provider=SQLOLEDB.1;Password=123;Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=img" '具体的数据库连接字符串请你自己改过来 '读取SQL脚本,开始初始化数据库 cn.BeginTrans On Error GoTo installErr Dim strSql As String, s As String Open "C:\test.sql" For Input As #1 While Not EOF(1) Line Input #1, s If UCase(Trim(s)) = "GO" Then cn.Execute (strSql) strSql = "" Else strSql = strSql & vbCrLf & s End If Wend Close #1 cn.CommitTrans MsgBox "数据库初始化完成!" Exit Sub installErr: MsgBox Err.Description cn.RollbackTrans End Sub
Private Sub Command1_Click()
Dim cn As New ADODB.Connection
cn.CursorLocation = adUseClient
cn.Open "Provider=SQLOLEDB.1;Password=123;Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=img" '具体的数据库连接字符串请你自己改过来
'读取SQL脚本,开始初始化数据库
cn.BeginTrans
On Error GoTo installErr
Dim strSql As String, s As String Open "C:\test.sql" For Input As #1
While Not EOF(1)
Line Input #1, s
If UCase(Trim(s)) = "GO" Then
cn.Execute (strSql)
strSql = ""
Else
strSql = strSql & vbCrLf & s
End If
Wend
Close #1
cn.CommitTrans
MsgBox "数据库初始化完成!"
Exit Sub
installErr:
MsgBox Err.Description
cn.RollbackTrans
End Sub