Click here to Skip to main content
12,078,489 members (46,231 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB 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.2K
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160212.1 | Last Updated 11 Apr 2011
Copyright © CodeProject, 1999-2016
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