[英]Dapper - Stored Procedure - Incorrect syntax near 'GO'
I have a stored procedure in a myscript.sql
file that looks like this:我在myscript.sql
文件中有一个存储过程,如下所示:
CREATE PROCEDURE [dbo].[_GetUserID]
@EmailAddress NVARCHAR(254)
AS
DECLARE @UserID UNIQUEIDENTIFIER;
SELECT @UserID = [ID]
FROM [dbo].[User]
WHERE [EmailAddress] = @EmailAddress
PRINT @UserID
GO
I have some C# code that relies on Dapper to run this script.我有一些 C# 代码依赖于 Dapper 来运行这个脚本。 I can successfully run this script when I copy-and-paste it into Azure Data Studio.当我将它复制并粘贴到 Azure Data Studio 时,我可以成功运行该脚本。 However, when I am trying to run this script from code, I get an error:但是,当我尝试从代码运行此脚本时,出现错误:
Incorrect syntax near 'GO' “GO”附近的语法不正确
My C# code looks like this:我的 C# 代码如下所示:
try
{
var script = File.ReadAllText("<path to myScript.sql is here>");
using (var connection = new SqlConnection(dbConnectionString))
{
var command = connection.CreateCommand();
command.CommandText = script;
command.CommandType = CommandType.Text;
connection.Open();
command.ExecuteNonQuery();
}
Console.WriteLine("Success.");
}
catch (Exception ex)
{
Console.WriteLine($"Failed. Reason: '{ex.Message}')");
}
I don't understand why I can run myScript.sql
from Azure Data Studio, however, it's not working from my C# code.我不明白为什么我可以从 Azure Data Studio 运行myScript.sql
,但是,它在我的 C# 代码中不起作用。 I'm also creating tables using the same approach and it works fine.我也在使用相同的方法创建表,它工作正常。 I'm not sure what I'm missing.我不确定我错过了什么。
GO
is not a valid T-SQL keyword - it's a separator that is used by SQL Server Management Studio and obviously also Azure Data Studio. GO
不是有效的 T-SQL 关键字 - 它是 SQL Server Management Studio 使用的分隔符,显然也是 Azure Data Studio。
To fix this, just simply remove that GO
line from your .sql
script file and run it without this - should be just fine.要解决此问题,只需从您的.sql
脚本文件中删除该GO
行并在没有它的情况下运行它 - 应该没问题。
On a different note: having nothing but a PRINT
statement in your stored procedure doesn't make a lot of sense - don't you want to actually SELECT @UserId
to get that data sent back to the caller?? SELECT @UserId
说法:在您的存储过程中只有PRINT
语句并没有多大意义-您难道不想实际SELECT @UserId
将数据发送回调用者吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.