c# - GO statements blowing up sql execution in .NET -
i have simple c# command shell app executes sql script generated sql server scripting schema , data. it's blowing on "go" statements. error message:
incorrect syntax near 'go'.
here full sql script:
/****** object: table [gym].[membershipstatus] script date: 9/3/2013 9:24:01 ******/ set ansi_nulls on go set quoted_identifier on go set ansi_padding on go create table [gym].[membershipstatus]( [membershipstatusid] [tinyint] identity(1,1) not null, [name] [varchar](75) not null, [description] [varchar](400) not null, [allowcheckin] [bit] not null, [includeincollections] [bit] not null, [schedulefutureinvoices] [bit] not null, constraint [membershipstatus_pk] primary key clustered ( [membershipstatusid] 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 set identity_insert [gym].[membershipstatus] on insert [gym].[membershipstatus] ([membershipstatusid], [name], [description], [allowcheckin], [includeincollections], [schedulefutureinvoices]) values (1, n'active', n'active', 1, 1, 1) insert [gym].[membershipstatus] ([membershipstatusid], [name], [description], [allowcheckin], [includeincollections], [schedulefutureinvoices]) values (2, n'cancelled', n'cancelled', 0, 1, 0) insert [gym].[membershipstatus] ([membershipstatusid], [name], [description], [allowcheckin], [includeincollections], [schedulefutureinvoices]) values (3, n'collection', n'collection', 0, 0, 0) insert [gym].[membershipstatus] ([membershipstatusid], [name], [description], [allowcheckin], [includeincollections], [schedulefutureinvoices]) values (4, n'deleted', n'deleted', 0, 0, 0) insert [gym].[membershipstatus] ([membershipstatusid], [name], [description], [allowcheckin], [includeincollections], [schedulefutureinvoices]) values (5, n'expired', n'expired', 1, 1, 1) insert [gym].[membershipstatus] ([membershipstatusid], [name], [description], [allowcheckin], [includeincollections], [schedulefutureinvoices]) values (6, n'freeze', n'freeze', 0, 1, 0) insert [gym].[membershipstatus] ([membershipstatusid], [name], [description], [allowcheckin], [includeincollections], [schedulefutureinvoices]) values (7, n'inactive', n'inactive', 0, 1, 1) set identity_insert [gym].[membershipstatus] off alter table [gym].[membershipstatus] add default ('') [name] go alter table [gym].[membershipstatus] add default ('') [description] go alter table [gym].[membershipstatus] add default ((0)) [allowcheckin] go alter table [gym].[membershipstatus] add default ((0)) [includeincollections] go alter table [gym].[membershipstatus] add default ((0)) [schedulefutureinvoices] go
the relevant section of code looks this:
sqlcommand command = new sqlcommand(script, connection); command.commandtype = commandtype.text; command.executenonquery();
any ideas?
as others mentioned, split string go
statements. careful, may have text "go"
in other parts of script. might have whitespace before or after go statement, , might have comments on line after go statement also. of valid in ssms, may want test it.
here method use:
private static ienumerable<string> splitsqlstatements(string sqlscript) { // split "go" statements var statements = regex.split( sqlscript, @"^[\t ]*go[\t ]*\d*[\t ]*(?:--.*)?$", regexoptions.multiline | regexoptions.ignorepatternwhitespace | regexoptions.ignorecase); // remove empties, trim, , return return statements .where(x => !string.isnullorwhitespace(x)) .select(x => x.trim(' ', '\r', '\n')); }
Comments
Post a Comment