直接执行指定SQL文件(参考整理)

本文介绍三种执行SQL文件的方法:使用SQL Server的扩展存储过程xp_cmdshell;通过VB.NET读取并执行SQL文件;利用VB.NET调用xp_cmdshell执行SQL文件。
参考转载http://blog.yesky.com/Blog/hunter/archive/2005/03/25/95357.html
1)     如何执行sql文件
exec master.dbo.xp_cmdshell ' osql -U @username -P @password -i @sqlname'
@username 数据库用户
@password 数据库用户口令
@sqlname   sql文件目录名称,例如:’f://gps_insert.sql’
例如:
exec master.dbo.xp_cmdshell ' osql -U sa -P sa -i f://gps_insert.sql'
 
2)      VB如何执行sql文件 ,读取文件,执行    
Public Sub exesqlfile(cn as ADODB.Connection,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 cn.Execute strSql
      Close #1
End Sub
3)     VB如何执行sql文件 ,直接执行存储过程xp_cmdshell
Public Sub exesqlfile (cn as ADODB.Connection,sqlFile As String)
Dim sql as string
sql="master.dbo.xp_cmdshell '
osql -U username -P  password -i " & sqlFile  &  " ' "
cn.execute sql
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值