Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 10-Apr-11 21:34pm
Jagz W1.9K
Edited 10-Apr-11 21:41pm
Tarun.K.S42.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:

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:
sql = "SELECT 1" + vbNewLine + _
      "GO" + vbNewLine + _
      "SELECT 2" + vbNewLine + _
      "GO" + vbNewLine + _
      "SELECT 3" + vbNewLine + _
      "GO".Replace(vbNewLine + "GO", ";")
sql = sql.Replace(vbNewLine + "GO", ";")
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 215
1 Sebastiaan Meijerink 141
2 Sergey Alexandrovich Kryukov 124
3 Rana Waqas 85
4 Maciej Los 84
0 Sergey Alexandrovich Kryukov 6,558
1 OriginalGriff 6,288
2 Peter Leow 2,534
3 Abhinav S 2,358
4 Maciej Los 2,352


Advertise | Privacy | Mobile
Web01 | 2.8.150414.1 | Last Updated 11 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100