请问各位老大:如何在VB中执行一段SQL server2000生成的脚本文件(*.sql文件)??谢谢~~~~~~~~~~~在线等待。

wangwm 2003-10-21 09:45:14
如题
...全文
42 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangwm 2003-10-21
  • 打赏
  • 举报
回复
up up up
有谁帮帮我啊
wangwm 2003-10-21
  • 打赏
  • 举报
回复
楼上的大哥,请问
shellexecute "osql ...."这句怎么用,有没有例子啊
vcshcn 2003-10-21
  • 打赏
  • 举报
回复
shellexecute "osql ...."
vcshcn 2003-10-21
  • 打赏
  • 举报
回复
如果判断go的话,比如insert就不行
如果判断#13的话,其他的就不行

听我的吧,用osql
yoki 2003-10-21
  • 打赏
  • 举报
回复
如果出现了不认识的非法字符,多半是转脚本的时候每行开始的"if"转成了"f",(好像是个bug??)
对其进行另外的处理就可以了:
Private Sub CreateDataBase(cnDataBase as connect,sqlFile As String)
Dim strSql As String, strTmp As String

Open sqlFile For Input As #1
strSql = ""
Do While Not EOF(1)
Line Input #1, strTmp
If Left(strTmp, 3) = "f" Then strTmp = "if" & Right(strTmp, Len(strTmp) - 3)
If UCase(strTmp) = "GO" Then
cn.Execute strSql
strSql = ""
Else
strSql = strSql & strTmp & vbCrLf
End If
Loop
If strSql <> "" Then cnDataBase.Execute strSql
Close #1
End Sub
yoki 2003-10-21
  • 打赏
  • 举报
回复
楼上的名字居然和我一样??呵呵~
wangwm 2003-10-21
  • 打赏
  • 举报
回复
楼上的方法不行,我试过了,错误提示是无法识别的字符串
yoki 2003-10-21
  • 打赏
  • 举报
回复
用vb的话你可以这样

Private Sub CreateDataBase(cnDataBase as connect,sqlFile As String)
Dim strSql As String, strTmp As String

Open sqlFile For Input As #1
strSql = ""
Do While Not EOF(1)
Line Input #1, strTmp
If UCase(strTmp) = "GO" Then
cn.Execute strSql
strSql = ""
Else
strSql = strSql & strTmp & vbCrLf
End If
Loop
If strSql <> "" Then cnDataBase.Execute strSql
Close #1
End Sub

其他的你可以定义一个到服务器的ado连接
然后可以这样:
比如:vb中
dim cn as New ADODB.Connection
dim sql as string

cn.open ".......到服务器的连接"
sql="master.dbo.xp_cmdshell ' osql -U username -P password -i c:\myquery.sql'"
cn.execute sql
marckle 2003-10-21
  • 打赏
  • 举报
回复
读文件,将文件内容变成sql字符串,
13014592842 2003-10-21
  • 打赏
  • 举报
回复
补充一句。如果要执行sql的内容只是导数据的话.不如用bcp,具体的可以查查t_sql帮助.
13014592842 2003-10-21
  • 打赏
  • 举报
回复

isql.exe copy to you dir,
用isql带命令行的方式.
Dublue 2003-10-21
  • 打赏
  • 举报
回复
不对,正解在这里::)))
shell "osql.exe -U " & trim(登录用户名) & " -P " & trim(登录密码) & " -d " & trim(登录的数据库名) & " -i " trim(sql脚本路径及名称)
举个例子:
ShellPath = tPath & "osql.exe -U " & UID & " -d " & DBName _
& " -i " & tPath
dSuc = Shell(ShellPath & "create_proc.sql", vbHide)
If dSuc = 0 Then
CreateProc = False
MsgBox sAllProc(i) + "未执行成功", vbCritical, "错误"
Exit Function
End If
(tpath为osql.exe所在路径,此文件存在于sql安装目录下,拷贝过来就行了)

li_ping 2003-10-21
  • 打赏
  • 举报
回复
ShellExecute的用法:

ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。

  有几个API函数都可以实现这些功能,但是在大多数情况下ShellExecute是更多的被使用的,同时它并不是太复杂。下面举例说明它的用法。

开始一个新的应用程序
   ShellExecute(Handle, 'open', PChar('c:\test\app.exe'), nil, nil, SW_SHOW);

打开记事本,并打开一个文件(系统能识别记事本应用程序的路径,因此我们不必使用绝对路径)
   ShellExecute(Handle, 'open', PChar('notepad'), PChar('c:\test\readme.txt'), nil, SW_SHOW);

打印一个文档
   ShellExecute(Handle, 'print', PChar('c:\test\test.doc'), nil, nil, SW_SHOW);

   注意:可能你会看到word暂时的被打开,但它会自动关闭。

打开一个HTML页面
   ShellExecute(Handle, 'open', PChar('http://www.festra.com/'), nil, nil, SW_SHOW);

你能通过一个已经注册的文件类型来打开应用程序
   ShellExecute(Handle, 'open', PChar('c:\test\readme.txt'), nil, nil, SW_SHOW);

用windows Explorer 打开一个目录
   ShellExecute(Handle, 'explore', PChar('c:\windows)', nil, nil, SW_SHOW);

运行一个DOS命令并立即返回
   ShellExecute(Handle, 'open', PChar('command.com'), PChar('/c copy file1.txt file2.txt'), nil, SW_SHOW);

运行一个DOS命令并保持DOS窗口存在
   ShellExecute(Handle, 'open', PChar('command.com'), PChar('/k dir'), nil, SW_SHOW);

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧