C# 将包含 'GO' 关键字的字符串拆分为多个 sql 语句
C# split string containing 'GO' keyword into multiple sql statements
如果我打开 SQL Management Studio 并写入以下文本:
select a =1
GO
select b =2
SQL Management Studio 将文本拆分为两个语句,在 'GO' 关键字上进行条带化。
我应该如何在 C# 中做同样的事情?
我可以使用正则表达式或字符串拆分,但我无法正确拆分这样的字符串:
select text ='
this is a unique statement
GO
'
如您所知,GO 是 SQL management studio 中的默认批处理分隔符,而不是 SQL 关键字。
但是,management studio 有可用的库,您可以在 C# 中使用。
解释 here(选项 2)
string connectionString, scriptText;
SqlConnection sqlConnection = new SqlConnection(connectionString);
ServerConnection svrConnection = new ServerConnection(sqlConnection);
Server server = new Server(svrConnection);
server.ConnectionContext.ExecuteNonQuery(scriptText);
这将执行 "scriptText" 就像您在 Management Studio 中执行它一样。默认情况下,它将 "GO" 作为批处理分隔符处理。
如果我打开 SQL Management Studio 并写入以下文本:
select a =1
GO
select b =2
SQL Management Studio 将文本拆分为两个语句,在 'GO' 关键字上进行条带化。
我应该如何在 C# 中做同样的事情? 我可以使用正则表达式或字符串拆分,但我无法正确拆分这样的字符串:
select text ='
this is a unique statement
GO
'
如您所知,GO 是 SQL management studio 中的默认批处理分隔符,而不是 SQL 关键字。
但是,management studio 有可用的库,您可以在 C# 中使用。 解释 here(选项 2)
string connectionString, scriptText;
SqlConnection sqlConnection = new SqlConnection(connectionString);
ServerConnection svrConnection = new ServerConnection(sqlConnection);
Server server = new Server(svrConnection);
server.ConnectionContext.ExecuteNonQuery(scriptText);
这将执行 "scriptText" 就像您在 Management Studio 中执行它一样。默认情况下,它将 "GO" 作为批处理分隔符处理。