我有一个方法可以将两个变量(一个 int 和一个分隔字符串)发送到 SQL Server proc。
变量值(从调试器复制):
detailId: 5
fileNames: "recruiter.txt|cert.pdf"
方法:
public void InsertFiles(int detailId, string fileNames)
{
ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings["PRADB"];
using (SqlConnection conn = new SqlConnection(connString.ToString()))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "dbo.InsertFiles";
cmd.Parameters.AddWithValue("@detailId", detailId);
cmd.Parameters.AddWithValue("@fileNames", fileNames);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
string exc = ex.ToString();
}
finally
{
conn.Close();
}
}
}
这是过程:
ALTER PROCEDURE [dbo].[InsertFiles]
@detailId int,
@fileNames varchar(max)
AS
BEGIN
SET NOCOUNT ON;
insert into [dbo].[PRA_Files] (detailId, fileNames)
values (@detailId, @fileNames)
END
调试时收到的异常:
{"Incorrect syntax near 'dbo'."}
然而,当我从 proc 执行时:
exec [dbo].[InsertFiles] 5, "recruiter.txt|cert.pdf"
它工作正常。错误不是代码方面的,因为它被上述方法的 catch 块捕获。我难住了。