Executing external .SQL file from C# application

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

H

Can we execute an exteral SQL file (say abc.sql - contains one or more SQL statements) from a C# application

Regard
Manish Gupta
 
I would guess you can - I have not done it from C#, but I have done it in
the past using ASP and VBScript. Here is some old VB code that you could
adapt to C#:

Function RunSQLScript (file_path)
if objFSO.FileExists(file_path) then
Set objFile = objFSO.GetFile(file_path)
Set objFileRead = objFile.OpenAsTextStream(1, -2)

strSQLFile = objFileRead.ReadAll

strSQLFile = Replace(strSQLFile,"GO" & vbCRLF,"")

Set objCmd = CreateObject("ADODB.Command")

objCmd.CommandType = adCmdText
objCmd.CommandText = strSQLFile
objCmd.ActiveConnection = objConn
objCmd.Execute

objFileRead.Close
RunSQLScript = 1
else
RunSQLScript = -1
end if
End Function

I don't recall why I stripped out the GO commands, but it must have been a
good reason ;)


Matt


Manish Gupta said:
Hi

Can we execute an exteral SQL file (say abc.sql - contains one or more SQL
statements) from a C# application?
 
Back
Top