'************************************************************************* '**函 数 名:s_ExecuteSqlFile '**功能描述:在指定的数据库对象中,执行指定的脚本文件 '**输 入: sFileName 要执行的脚本文件名 '** : iDb 数据库对象,要求是:ADODB.Connection '** : sPassword 登陆密码 '**输 出: 无 '**调用模块: 无 '**作 者: 邹建 '**日 期: 2004年03月09日 '************************************************************************* Sub s_ExecuteSqlFile(ByVal sFileName$, ByVal iDb As Object) Dim iFn As Object
Dim iSql$, iStr$
Set iFn = CreateObject("FileSystemObject").OpenTextFile("c:\1.txt", 1)
With iFn While .AtEndOfStream = False iStr = iFn.ReadLine If UCase(iStr) = "GO" Then If iSql <> "" Then iDb.Execute iSql iSql = "" End If Else iSql = iSql & vbCrLf & iStr End If Wend If iStr<>"" Then iDb.Execute iSql iFn.Close End With End Sub '调用示例 '要求引用Microsoft ActiveX Data Objects 2.x Library ' '数据库连接字符串模板 '身份验证模式为:"sql server和windows" 'Provider=SQLOLEDB.1;Persist Security Info=True;User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名 ' '身份验证模式为:"仅windows" 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=数据库名;Data Source=SQL服务器名Sub test() Dim iDb As ADODB.Connection Dim iConn$
那么可能最后可能只有12个试图被创建成功,
我把错误输出到ERROR.TXT里面了,有报错。这是不加延迟的输出结果
1> 2> 3> 1> 2> 3> 1> 2> 1> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> 14> 15> 16> 17> 18> 1> 2> 1> 2> 1> 2> > 1> 2> 1> 2> 1> 2> > 29> 30> 31> 32> 33> 34> 35> 36> 37> 38> 39> 40> 41> 42> 1> 2> 1> 2> 1> 2> VIEW_MemberLevelAlways,行 4
对象名 'dbo.VIEW_MemberRetailAlways' 无效。
1> 2> 这是加上延迟后的输出结果
1> 2> 3> 1> 2> 3> 1> 2> 1> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> 14> 15> 16> 17> 1> 2> 1> 2> 1> 2> 执行的东西是一模一样的!
下面是我的调用语句
For i As Integer = 1 To Me.Txt_Display.Lines.Length - 1
FilePath = Me.Txt_Display.Lines(i - 1) StrExe = "osql -i " & FilePath & " -d " & MyPub.MyDatabaseName & " -U " & MyPub.MyLoginName & " -S " & MyPub.MyServerName & " -P " & MyPub.MyLoginPass & " -o C:\ERROR.txt -b"
ReturnValue = Shell(StrExe, AppWinStyle.Hide)
'ReturnValue = Shell(StrExe, AppWinStyle.NormalFocus)
'Thread.Sleep(1000)
Next被屏蔽掉的是加上延迟的
缺少 dbo.VIEW_MemberRetailAlways如果这个对象也是要创建的,那就是你创建的顺序搞错了.
一个试图是依赖于另一个试图的,但是顺序是没错的阿!
这两个sql文件之间只隔了一个sql文件
有没有可能第一个sql没有执行完毕的时候第三个就开始执行了?
我延迟加了一秒,试验了几次,没有问题
但是万事无绝对阿
osql的返回参数都被shell屏蔽掉了
我如何得到返回参数来判断是否执行完毕阿?
不可能我再打开sql文件把所有脚本粘贴拷贝到一个文件里阿
'**函 数 名:s_ExecuteSqlFile
'**功能描述:在指定的数据库对象中,执行指定的脚本文件
'**输 入: sFileName 要执行的脚本文件名
'** : iDb 数据库对象,要求是:ADODB.Connection
'** : sPassword 登陆密码
'**输 出: 无
'**调用模块: 无
'**作 者: 邹建
'**日 期: 2004年03月09日
'*************************************************************************
Sub s_ExecuteSqlFile(ByVal sFileName$, ByVal iDb As Object)
Dim iFn As Object
Dim iSql$, iStr$
Set iFn = CreateObject("FileSystemObject").OpenTextFile("c:\1.txt", 1)
With iFn
While .AtEndOfStream = False
iStr = iFn.ReadLine
If UCase(iStr) = "GO" Then
If iSql <> "" Then
iDb.Execute iSql
iSql = ""
End If
Else
iSql = iSql & vbCrLf & iStr
End If
Wend
If iStr<>"" Then iDb.Execute iSql
iFn.Close
End With
End Sub
'调用示例
'要求引用Microsoft ActiveX Data Objects 2.x Library
'
'数据库连接字符串模板
'身份验证模式为:"sql server和windows"
'Provider=SQLOLEDB.1;Persist Security Info=True;User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名
'
'身份验证模式为:"仅windows"
'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=数据库名;Data Source=SQL服务器名Sub test()
Dim iDb As ADODB.Connection
Dim iConn$
'数据库连接字符串 iConn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名"
Set iDb = New ADODB.Connection
iDb.Open iConn
s_ExecuteSqlFile "c:\a.sql", iDb '执行脚本文件: c:\a.sql
iDb.Close
Set iDb = Nothing
End Sub
osql是不是不支持超过8位的文件夹和文件名,也不支持中文
连error.txt都没有
感谢之至!
也谢谢w_rose(w_rose) 对我的解答!
结了!