简体   繁体   English

通过ASP.NET 3.5 C#方法插入表时返回异常

[英]Exception returned when inserting into table through ASP.NET 3.5 C# method

I have a method that sends two variables, an int and a delimited string, to an SQL Server proc. 我有一个方法可以将两个变量(一个int和一个定界的字符串)发送到SQL Server proc。

Variable values (copied from debugger): 变量值(从调试器复制):

detailId: 5
fileNames: "recruiter.txt|cert.pdf"

The method: 方法:

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();
        }
    }
}

Here is the proc: 这是过程:

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

The exception received when debugging: 调试时收到的异常:

{"Incorrect syntax near 'dbo'."}

Yet when I execute from the proc: 但是,当我从proc执行时:

exec [dbo].[InsertFiles] 5, "recruiter.txt|cert.pdf"

It works fine. 工作正常。 There error isn't code side as it is being caught in the catch block of the method above. 错误不在代码侧,因为它被上述方法的catch块捕获。 I'm stumped. 我很沮丧

You need to specify that the command is a stored procedure: 您需要指定该命令是存储过程:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM