1

我有一个方法可以将两个变量(一个 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 块捕获。我难住了。

4

1 回答 1

5

您需要指定该命令是一个存储过程:

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "dbo.InsertFiles";
cmd.Parameters.AddWithValue("@detailId", detailId);
cmd.Parameters.AddWithValue("@fileNames", fileNames);
cmd.CommandType = CommandType.StoredProcedure;
于 2013-10-03T18:03:05.983 回答