我有生成bymyDBcontext.Database.Log = s => myTextWriter(s)
的
INSERT [dbo].[Commodities]([IsGas], [IsPower], [Name])
VALUES (@0, @1, @2)
SELECT [Id]
FROM [dbo].[Commodities]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()
-- @0: 'True' (Type = Boolean)
-- @1: 'False' (Type = Boolean)
-- @2: 'Gas' (Type = String, Size = -1)
-- Executing at 24.6.2015 13:20:32 +02:00
-- Completed in 0 ms with result: SqlDataReader
--
--Other Inserts here
--
我想在我的测试服务器上执行此SQL查询。不幸的是这个sql代码不可执行。我需要这样的东西:
INSERT [dbo].[Commodities]([IsGas], [IsPower], [Name])
VALUES (1, 0, 'Gas')
SELECT [Id]
FROM [dbo].[Commodities]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()
你能否告诉我如何处理这个sql(我猜的是正则表达式),以便它开始可执行?
答案 0 :(得分:1)
您需要删除以--
开头的所有行和空行,然后使用插入VALUES
行的注释中的值。
以下代码可能有所帮助:
var rxGetValues = new Regex(@"(?ms)(?<=^VALUES\s+\([^)]*?)@(?<id>\d+)(?=.*?^-{2}\s+@\k<id>:\s+'(?<val>[^']*)')");
var str = "YOUR_STRING";
str = rxGetValues.Replace(str, m => m.Groups["val"].Value.ToLower() == "false"
|| m.Groups["val"].Value.ToLower() == "true" ?
Convert.ToInt32(Boolean.Parse(m.Groups["val"].Value)).ToString()
: m.Groups["val"].Value);
str = string.Join(Environment.NewLine, str.Split(new[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).Where(p => !p.Trim().StartsWith("--")).ToArray());
结果:
INSERT [dbo].[Commodities]([IsGas], [IsPower], [Name])
VALUES (1, 0, Gas)
SELECT [Id]
FROM [dbo].[Commodities]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()