开发者

Simple task: Connect to database, execute a stored procedure, disconnect

开发者 https://www.devze.com 2023-03-06 06:47 出处:网络
I don\'t necessarily need to pass the stored procedures any variables from my VBScript, I just need to run the 开发者_如何学Cstored procedure on the server. I haven\'t been able to find any clear exam

I don't necessarily need to pass the stored procedures any variables from my VBScript, I just need to run the 开发者_如何学Cstored procedure on the server. I haven't been able to find any clear examples of how to do this—just a lot of people explaining how to pass a variable from a SP back to a VBScript.

Any help would be so appreciated! It looks like I'll have to open a connection, then send the command to execute the stored procedure, then close the connection, but I'm a bit lost about how to do this from a VBscript.

Thanks!


you can use the ADODB.Connection object from VbScript

check this sample

Dim sServer, sConn, oConn, sDatabaseName, sUser, sPassword
sDatabaseName="test"
sServer="localhost"
sUser="sa"
sPassword="yourpassword"
sConn="provider=sqloledb;data source=" & sServer & ";initial catalog=" & sDatabaseName

Set oConn = CreateObject("ADODB.Connection")
oConn.Open sConn, sUser, sPassword
oConn.Execute "exec sp_help"

WScript.Echo "executed"
oConn.Close
Set oConn = Nothing


You can create a method like this:

Public Sub ExecuteSql( sqlString )
    Dim oConn
    Set oConn = Server.CreateObject("ADODB.Connection")
    oConn.Open connectionString
    oConn.Execute( CStr(sqlString) )
    oConn.Close
    Set oConn = Nothing
End Sub

Note: This routine assumes that the SQL statement was built by the calling routine and properly escaped. In addition, connectionString is a constant that you store somewhere with the connection string to the db.

Example call:

Call ExecuteSql( "exec MyProc" )
0

精彩评论

暂无评论...
验证码 换一张
取 消