Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

executing a .sql file from a VB exe

Posted on 2001-09-06
5
Medium Priority
?
322 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 240 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
Suggested Courses

610 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