I and trying to create a method to run a .sql file on an SQL Server database.
The code i have is:
SqlConnection dbCon = new SqlConnection(connstr);
FileInfo file = new FileInfo(Server.MapPath("~/Installer/JobTraksDB.sql"));
StreamReader fileRead = file.OpenText();
string script = fileRead.ReadToEnd();
fileRead.Close();
SqlCommand command = new SqlCommand(script, dbCon);
try
{
dbCon.Open();
command.ExecuteNonQuery();
dbCon.Close();
}
catch (Exception ex)
{
throw new Exception("Failed to Update the Database, check your Permissions.");
}
But i keep getting errors about "incorrect syntax near keyword 'GO'" My SQL File starts like this: (Generated from SQL Management Studio)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Job_Types](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
CONSTRAINT [PK_JobTypes] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
How should i be executing this script?