Solved

executing a .sql file from a VB exe

Posted on 2001-09-06
5
292 Views
Last Modified: 2012-06-21
Can I execute a .sql file from a VB app, the documentation with VB only describes how to do this with J++ and C++, if this is possible, how can it be achieved?
0
Comment
Question by:louise_8
5 Comments
 
LVL 5

Expert Comment

by:AndrewDev
Comment Utility
Try using the sp_executesql Extended Stored Procedures which are part of SQLServer.

Create an ADO connection to your database and call the stored procedure with your .sql file as a parameter

eg.
myADOConnection.exectue sp_executesql mysqlfile.sql

Regards
Andrew
0
 
LVL 4

Expert Comment

by:Jeremy_D
Comment Utility
sp_executesql will only accept a unicode string as input, not a filename. Besides that, your batch might well be more then 4000 characters long, so sp_executesql won't work.

I would go for a Shell call to osql.exe, something like:

Shell "c:\mssql7\binn\osql.exe -S YourServer -d YourDatabase -U YourUserID -P YourPassword -i c:\mssql7\MyScripts\MyScript.sql"

Look up osql in BOL for the exact command-line switches for osql, there are a lot of them. You can also use isql.exe or isqlw.exe, depending on what's best for your environment.
0
 
LVL 5

Expert Comment

by:AndrewDev
Comment Utility
Well done Jeremy
Shell was my first choice but I couldn't get the syntax right - thanks for the help
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 60 total points
Comment Utility
louise_8: The following solution works using the SQL-DMO library, add a reference to Microsoft SQL-DMO to your project and away you go. You need to read the contents of the .Sql file into strBatch first. You can also use a specific database by changing the executeimmediate line to : sqlServer.Databases("My Database").ExecuteImmediate.....

  Dim sqlServer As SQLDMO.sqlServer
  Set sqlServer = New SQLDMO.sqlServer
  sqlServer.Connect "MySQLServer"
  sqlServer.ExecuteImmediate Replace(strBatch,"GO",vbCRLF & "GO" & vbCRLF), SQLDMOExec_ContinueOnError
  sqlServer.DisConnect
  Set sqlServer = Nothing

This assumes that strBatch contains the complete text from the .SQL file.
0
 

Author Comment

by:louise_8
Comment Utility
Hi TimCottee

Thanks for your comment, however Im not familar with SQLDMO, I pasted your code in and am getting a connection error
Microsoft[ODBC SQL DRIVER][DBNETLIB] server does not exist or access denied
Microsoft[ODBC SQL DRIVER][DBNETLIB] ConnectionOpen Connect()

I had an odbc connection opened in the previous form but I close it before this one opens, so I cant figure out the error, my db
incidentally is located on a server but opens fine in the other form

is sqlServer.Connect "LMSDB" sufficient to open the LMSDB database on the server, or do I need to add more parameters to the string?

any help would be greatly appreciated

Louise

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now