Public Function Load_ini(Strx As String, FileNames As String) As String
'从指定文件中读取指定字符
Dim Textline As String
On Error Resume Next
Open FileNames For Input As #1 ' 打开配置文件。
Do While Not EOF(1)
Line Input #1, Textline
If InStr(Textline, Strx) Then
Load_ini = Mid(Textline, InStr(Textline, "=") + 1)
End If
Loop
Close #1
Sub ConSQL(服务器名 As String, 用户名 As String, 口令 As String, 数据库名 As String, 变量 As String)
'连接SQL Server
If 变量 = "Cn" Then
On Error Resume Next
Cn.Close
On Error GoTo Err1
Cn.ConnectionTimeout = 2100
Cn.Open "Driver={SQL Server};Server=" & 服务器名 & ";Uid=" & 用户名 & ";Pwd=" & 口令 & ";Database=" & 数据库名 & ""
End If
If 变量 = "Cm" Then
On Error Resume Next
Cm.Close
On Error GoTo Err1
Cm.ConnectionTimeout = 2100
Cm.Open "Driver={SQL Server};Server=" & 服务器名 & ";Uid=" & 用户名 & ";Pwd=" & 口令 & ";Database=" & 数据库名 & ""
End If
Exit Sub
Err1:
MsgBox "连接服务器失败!请检查网络的连接是否正确"
'Resume
End Sub
to jinesc(归去来兮:我很笨,因此只用VB):
我是说"批分隔符"是可自定义的,不一定必须是"GO"!
建议:
Public Function Creat_Table(data As String, Files As String,optional EndOFBat as String="GO") As Boolean
Public Function Creat_Table(data As String, Files As String) As Boolean
'本模块用于从*.sql中导入数据库结构到SQL Server
Dim SQL, Txtline As String
On Error Resume Next
Cn.Close
On Error GoTo Err1
ConSQL Load_ini("服务器名"), Load_ini("用户名"), Load_ini("口令"), data, "Cn"
SQL = " "
Open App.Path & "\log.txt" For Output As #2 ' 打开输出文件。
' 将错误数据写入文件。
Open App.Path & Files For Input As #1 ' 打开SQL文件。
Do While Not EOF(1)
Line Input #1, Textline
If Not (Trim(Textline) = "GO") Then
SQL = SQL & Textline & Chr(13)
Else
Cn.Execute SQL 'Mid(SQL, InStr(SQL, "C"))
SQL = " "
End If
Loop
Close #1
Close #2
If Creat_Table = False Then
Shell App.Path & "\log.txt"
End If
Exit Function
Err1:
Print #2, "This is a Error !"
Print #2, Err.Description
Print #2, Err.Number
Print #2, "**********"
Creat_Table = False
Resume Next
End Function