I am trying to create an initial database in my web application, and I managed to create a database, fill in tables, I was just stuck in getting stored procedures to work. Here is what I have so far, but I get CREATE / ALTER PROCEDURE 'should be the first statement in the query package. \ R \ nInvalid syntax near' GO '. I also tried removing GO, and also adding \ r \ n between the USE statement and the no-luck creation routine. Any help would be appreciated.
StringBuilder sbSP = new StringBuilder(); sbSP.AppendLine("USE [" + txtDBName.Text + "]"); sbSP.AppendLine("GO"); sbSP.AppendLine("CREATE PROCEDURE [spInsertADAuthorization] @AD_Account varchar(255),@AD_SID varchar(255),@AD_EmailAddress varchar(255),@DateImported datetime,@Active bit AS BEGIN SET NOCOUNT ON; INSERT INTO AD_Authorization (AD_Account, AD_SID, AD_EmailAddress, DateImported, Active) VALUES (@AD_Account,@AD_SID,@AD_EmailAddress,@DateImported,@Active) END"); sbSP.AppendLine("GO"); using (SqlConnection connection = new SqlConnection(ConnectionString)) { using (SqlCommand cmd = new SqlCommand(sbSP.ToString(), connection)) { connection.Open(); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); connection.Close(); } }
source share