我有一个更新我的数据库的存储过程。
它在查询分析器中运行良好,但是当我尝试从我的 C# Web 应用程序运行它时,表没有更新。
我收到以下错误,所以我将 ARITHABORT 设置为“ON”,但我仍然收到错误。
更新失败,因为以下 SET 选项的设置不正确:“ARITHABORT”。验证 SET 选项是否适用于索引视图和/或计算列上的索引和/或查询通知和/或 xml 数据类型方法。
C#代码:
p = new SqlParameter("@userAnswers", SqlDbType.VarChar, 1000);
p.Value = "";
p.Value = exam.ExamQuestions.Rows[0].ItemArray[0] + ":" + exam.UserAnswerChoices[0];
for (int x = 1; x < exam.NumQuestions; x++)
{
p.Value+= ", " + exam.ExamQuestions.Rows[x].ItemArray[0] + ":" + exam.UserAnswerChoices[x];
}
conn.query("insertAnswers", p);
return p.Value.ToString();
存储过程代码:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ARITHABORT ON
GO
ALTER PROCEDURE [dbo].[insertAnswers]
@userAnswers VarChar(1000)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @input XML
Set @input = '<Questions><Question id="' + Replace(Replace(@userAnswers, ':', '">'), ', ', '</Question><Question id="') + '</Question>' + '</Questions>'
;WITH ParsedXML AS
(
SELECT
ID = C.value('(@id)[1]', 'int'),
ColumnName = C.value('(.)[1]', 'varchar(10)')
FROM @input.nodes('/Questions/Question') AS T(C)
)
UPDATE CourseQuestions
SET a = CASE WHEN p.ColumnName = 'a' THEN t.a + 1 ELSE t.a END,
b = CASE WHEN p.ColumnName = 'b' THEN t.b + 1 ELSE t.b END,
c = CASE WHEN p.ColumnName = 'c' THEN t.c + 1 ELSE t.c END,
d = CASE WHEN p.ColumnName = 'd' THEN t.d + 1 ELSE t.d END,
e = CASE WHEN p.ColumnName = 'e' THEN t.e + 1 ELSE t.e END,
f = CASE WHEN p.ColumnName = 'f' THEN t.f + 1 ELSE t.f END
FROM CourseQuestions t
INNER JOIN ParsedXml p ON t.QuestionID = p.ID
END
@userAnswers
基本上是一个逗号分隔的字符串(因为我无法将数组发送到 SQL Server)。它看起来像:'1:d,2:a,3:b'