Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to Execute sql script in vb.net

How to Execute sql script in vb.net

Scheduled Pinned Locked Moved Visual Basic
databasehelpcsharptools
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anil Elavumthitta
    wrote on last edited by
    #1

    hi friends, i am suffering with a problem in database programming. my need is to execute a sql scritp using vb.net. i have a file having extension .sql. i want this file to be executed programically. pls help me.

    J 1 Reply Last reply
    0
    • A Anil Elavumthitta

      hi friends, i am suffering with a problem in database programming. my need is to execute a sql scritp using vb.net. i have a file having extension .sql. i want this file to be executed programically. pls help me.

      J Offline
      J Offline
      john nada
      wrote on last edited by
      #2

      ' Create a stream to read the sql file Dim reader As StreamReader = New StreamReader(path) ' Read the contents of the file into a string variable Dim sqlScript as String = reader.ReadToEnd() ' Execute the sql on the database Dim command as New SqlCommand(sqlScript, New SqlConnection(connStr)) command.ExecuteNonQuery()

      L 1 Reply Last reply
      0
      • J john nada

        ' Create a stream to read the sql file Dim reader As StreamReader = New StreamReader(path) ' Read the contents of the file into a string variable Dim sqlScript as String = reader.ReadToEnd() ' Execute the sql on the database Dim command as New SqlCommand(sqlScript, New SqlConnection(connStr)) command.ExecuteNonQuery()

        L Offline
        L Offline
        Leeland
        wrote on last edited by
        #3

        There's one additional aspect to add to this. If your file was created in Enterprise Manager using "Generate SQL Script" then it's going to have "GO" commands through out it. You'll need to add code to remove that by parsing it line by line. If this is the case, then use something like this: 'Open Script from file name. Dim oStreamReader As System.IO.StreamReader Dim sb As New System.Text.StringBuilder Dim sTemp As String = "" oStreamReader = New IO.StreamReader(ScriptName) 'Loop Through Lines until done Do Dim cmd As New SqlClient.SqlCommand sb = New System.Text.StringBuilder Do sTemp = oStreamReader.ReadLine If sTemp > "" And sTemp <> "GO" Then sb.Append(sTemp + vbCrLf) End If Loop Until (sTemp = "GO" Or oStreamReader.Peek = -1) ''Execute chunk against database 'oStreamReader.ReadLine() If sb.ToString > "" Then Try cmd.CommandText = sb.ToString cmd.Connection = cn cmd.ExecuteNonQuery() cmd.Dispose() Catch ex As SqlClient.SqlException MessageBox.Show(ex.Message) End Try End If 'MessageBox.Show(sb.ToString) sb = Nothing 'oStreamReader.ReadLine() Loop Until (oStreamReader.Peek = -1) cn.Close() I just wrote an app last week that did this exact thing.

        A 1 Reply Last reply
        0
        • L Leeland

          There's one additional aspect to add to this. If your file was created in Enterprise Manager using "Generate SQL Script" then it's going to have "GO" commands through out it. You'll need to add code to remove that by parsing it line by line. If this is the case, then use something like this: 'Open Script from file name. Dim oStreamReader As System.IO.StreamReader Dim sb As New System.Text.StringBuilder Dim sTemp As String = "" oStreamReader = New IO.StreamReader(ScriptName) 'Loop Through Lines until done Do Dim cmd As New SqlClient.SqlCommand sb = New System.Text.StringBuilder Do sTemp = oStreamReader.ReadLine If sTemp > "" And sTemp <> "GO" Then sb.Append(sTemp + vbCrLf) End If Loop Until (sTemp = "GO" Or oStreamReader.Peek = -1) ''Execute chunk against database 'oStreamReader.ReadLine() If sb.ToString > "" Then Try cmd.CommandText = sb.ToString cmd.Connection = cn cmd.ExecuteNonQuery() cmd.Dispose() Catch ex As SqlClient.SqlException MessageBox.Show(ex.Message) End Try End If 'MessageBox.Show(sb.ToString) sb = Nothing 'oStreamReader.ReadLine() Loop Until (oStreamReader.Peek = -1) cn.Close() I just wrote an app last week that did this exact thing.

          A Offline
          A Offline
          anom2m
          wrote on last edited by
          #4

          you can use Imports System.IO public sub RunSQL Dim conn As New SqlClient.SqlConnection Dim sqlCmd As New SqlClient.SqlCommand Dim sqltxt As String conn.ConnectionString = ("Data Source= " + DS + " ;Initial Catalog= " + DB + ";Integrated Security=True") conn.Open() sqltxt = System.IO.File.ReadAllText("SP.sql") sqlCmd.CommandText = sqltxt sqlCmd.Connection = conn sqlCmd.ExecuteNonQuery() conn.Close() this code will read the file once and execute it

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups