Solved

executing a .sql file from a VB exe

Posted on 2001-09-06
5
302 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
ID: 6460397
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
ID: 6460440
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
ID: 6460702
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
ID: 6460707
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
ID: 6463899
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 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