c# - How to run SQL File having large number of lines? -
i having sql file(filenamescript) having more 10k lines of code. each block of sql starts go , ends go. while executing file c#, getting exception near go statements. when running same file in sql server working fine.
con.connectionstring = sqlconn; fileinfo file = new fileinfo(filenamescript); string script = file.opentext().readtoend(); sqlcommand command = new sqlcommand(script, con); con.open(); command.executenonquery(); close(); i think executenonquerry not able handle many \n,\r,and \t file read stored in single line many \n , \r. there other method same? in advance.
no, issue not length of file, nor existence of \r and/or \n characters. because executing sql using method can run single batch, , script having go statements causes multiple batches.
one possibility split text on keyword go , execute each individual part:
con.connectionstring = sqlconn; var commands = file.readalltext(filenamescript).split(new []{"go"},stringsplitoption.removeemptyentries); con.open(); foreach(var batch in commands) { sqlcommand command = new sqlcommand(batch, con); command.executenonquery(); } con.close() additionally, wrap in transaction ensure batches executed atomically.
an alternative provided in question: how execute large sql script (with go commands) c#
Comments
Post a Comment