2

我有一个简单的表,其中包含三个字段:

ID (int)
FormData (xml)
TimeStamp (DateTime).

我创建了一个存储过程来将值插入成功运行的表中。但是,在我的尝试捕获中,它正在捕获

System.Data.SqlClient.SqlException:过程或函数“spInsertApplication”需要参数“@formDataDecoded”,但未提供该参数。

但是,@formDataDecoded 参数被很好地插入到数据库中。

有任何想法吗?我不知道从这里去哪里?

这是存储过程:

ALTER PROCEDURE [dbo].[spInsertApplication]
    -- Add the parameters for the stored procedure here 
    @formDataDecoded xml,
    @timestamp DateTime
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO Applications (formXML, TimeStamp) VALUES (@formDataDecoded, @timestamp)
END

这是c#:

string con = ConfigurationManager.AppSettings["umbracoDbDSN"];

using (SqlConnection connection = new SqlConnection(con))
{
    String encodedFormData = HttpUtility.UrlDecode(formData);

    SqlCommand command = connection.CreateCommand();
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "spInsertApplication";
    command.Parameters.AddWithValue("@formDataDecoded", encodedFormData);
    command.Parameters.AddWithValue("@timestamp", DateTime.Now);

    try
    {
        connection.Open();
        command.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        String errorSql = "INSERT INTO ErrorLog(ErrorText) VALUES (@errorText)";
        SqlCommand errorCommand = new SqlCommand(errorSql, connection);
        errorCommand.Parameters.AddWithValue("@errorText", ex.ToString());
        errorCommand.ExecuteNonQuery();
        Response.Redirect("/enterprise-superstars/sounds-like-you-are-already-a-star.aspx");
    }
    finally
    {
        connection.Close();
    }
}

我得到这样的formData:

String formData = Request.Form["xmlToVar"]; 

我将其传递给 saveApplicationform 方法。

提前致谢。

编辑

在上面运行 SQL Server Profiler 跟踪,结果发现存储过程被调用了两次。

事情是一个闪存表单调用特定页面,我将不得不让某人查看闪存代码并查看它是如何发布的,因为我没有编写它。

4

2 回答 2

1

尝试明确指定参数:

command.Parameters.Add("@formDataDecoded", SqlDbType.Xml).Value = encodedFormData;
command.Parameters.Add("@timestamp", SqlDbType.DateTime).Value = DateTime.Now;

您也可以尝试:

command.CommandText = "exec dbo.spInsertApplication @formDataDecoded, @timestamp";
于 2011-05-17T14:23:34.217 回答
1

问题是由于 Flash 表单正在调用包含插入过程的页面。

它基本上发布到插入页面然后重定向到那里导致页面执行两次,并且在第二次执行时显然没有任何 POST 数据插入到数据库中,因此 SqlException

仅供参考 - Fiddler 帮了大忙!

于 2011-05-17T18:31:48.360 回答