Solved

executing a .sql file from a VB exe

Posted on 2001-09-06
5
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

696 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