Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i execute a .sql file in vb.net

I tried using command object


Dim Objread As New StreamReader("path....")
Dim commandobj As New SqlCommand
commandobj.CommandText=objread.ReadToEnd()

commandobj.ExecuteNonQuery()



I am getting error: Incorrect syntax near GO
Posted
Updated 10-Apr-11 20:41pm
v2

 
Share this answer
 
This is something I've never tested, but ADO should be able to handle multiple commands as a batch. It doesn't accept GO but it should accept semicolon. So basically if you convert GO's to semicolons it could work. You could try something like:

C#
string sql = @"SELECT 1
GO
SELECT 2
GO
SELECT 3
GO";
sql = sql.Replace(@"
GO", ";");


This would most likely depend on the statements you're going to run, for example with procedure definitions including GO's could have problems.

[EDIT]
Sorry, just realized that this was VB, so something like:
VB
sql = "SELECT 1" + vbNewLine + _
      "GO" + vbNewLine + _
      "SELECT 2" + vbNewLine + _
      "GO" + vbNewLine + _
      "SELECT 3" + vbNewLine + _
      "GO".Replace(vbNewLine + "GO", ";")
sql = sql.Replace(vbNewLine + "GO", ";")
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900