当我使用下面的存储过程更新包含双引号的pollQuestion和pollName列时,它会正确保存双引号。但是,对于列option1,...,option9,存储过程不会保存双引号或双引号后的任何字符。就像SQL Server在双引号中过早地截断字符串一样。我有一种预感,QUOTED_IDENTIFIER与此问题有关,但它没有解释我对pollQuestion和pollName以及option1,...,option9的不一致。
问题:为什么SQL Server 2005使用双引号截断字符串来更新列option1,...,option9? (例如:在option1上保存“Hello \”World“将保存为”Hello“)
set ANSI_NULLS OFF
set QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[sp_Poll_UpdateQandAs2]
@POLL_ID INT,
@NAME VARCHAR(140) = NULL,
@QUESTION VARCHAR(300),
@OPT1 VARCHAR(150),
@OPT2 VARCHAR(150),
@OPT3 VARCHAR(150)= NULL,
@OPT4 VARCHAR(150)= NULL,
@OPT5 VARCHAR(150)= NULL,
@OPT6 VARCHAR(150)= NULL,
@OPT7 VARCHAR(150)= NULL,
@OPT8 VARCHAR(150)= NULL,
@OPT9 VARCHAR(150)= NULL,
@VOTES1 INT = 0,
@VOTES2 INT = 0,
@VOTES3 INT = 0,
@VOTES4 INT = 0,
@VOTES5 INT = 0,
@VOTES6 INT = 0,
@VOTES7 INT = 0,
@VOTES8 INT = 0,
@VOTES9 INT = 0
AS
UPDATE tTREpoll2 SET
pollName = @NAME,
pollQuestion = @QUESTION,
option1 = @OPT1,
option2 = @OPT2,
option3 = @OPT3,
option4 = @OPT4,
option5 = @OPT5,
option6 = @OPT6,
option7 = @OPT7,
option8 = @OPT8,
option9 = @OPT9,
votes1 = @VOTES1,
votes2 = @VOTES2,
votes3 = @VOTES3,
votes4 = @VOTES4,
votes5 = @VOTES5,
votes6 = @VOTES6,
votes7 = @VOTES7,
votes8 = @VOTES8,
votes9 = @VOTES9
WHERE pollID = @POLL_ID
更新 人们猜测调用存储过程的代码是罪魁祸首。我怀疑它是,但这是c#做的。
public static void UpdatePoll(PollObj savedPoll)
{
Database db = DatabaseFactory.CreateDatabase("ControlPanelSqlServer");
using (DbCommand cmd = db.GetStoredProcCommand("sp_Poll_UpdateQandAs2"))
{
db.AddInParameter(cmd, "POLL_ID", DbType.String, savedPoll.PollID);
db.AddInParameter(cmd, "QUESTION", DbType.String, savedPoll.Question);
db.AddInParameter(cmd, "NAME", DbType.String, savedPoll.Name);
for (int i = 0; i < MAX_NUMBER_OF_CHOICES; i++)
{
if (i < savedPoll.Answers.Count)
{
db.AddInParameter(cmd, "OPT" + (i + 1), DbType.String, savedPoll.Answers[i].Option);
db.AddInParameter(cmd, "VOTES" + (i + 1), DbType.Int32, savedPoll.Answers[i].NumVotes);
}
else
{
db.AddInParameter(cmd, "OPT" + (i + 1), DbType.String, null);
db.AddInParameter(cmd, "VOTES" + (i + 1), DbType.Int32, 0);
}
}
if (db.ExecuteNonQuery(cmd) <= 0)
{
throw new Exception("Could not save changes with given the Poll_ID:" + savedPoll.PollID);
}
}
}
更新#2
我发现了问题,这不是我发布的代码。数据库实际上可以工作并完成我想要它做的事情。我遇到的问题是我的一个aspx页面如何读取保存的结果。它没有正确地将保存的数据HtmlEncode到输入标签,因此双引号最终用双引号分隔输入标签。
答案 0 :(得分:1)
SQL Server没有理由这样做。它很可能是调用存储过程的代码的问题。
修改强>
要排除调用应用程序代码,请从SQL Server Management Studio中尝试:
EXEC sp_Poll_UpdateQandAs2
@POLL_ID = your_number_here
,@NAME = 'my test "question"'
,@QUESTION= 'will a question with "double quotes" work, even ''single quotes''?'
,@OPT1 = 'with out a ''doubt'', "yes" it will'
,@OPT2 = 'no way, "no" chance'
,@OPT3 = 'maybe'
,@OPT4 = NULL
,@OPT5 = NULL
,@OPT6 = NULL
,@OPT7 = NULL
,@OPT8 = NULL
,@OPT9 = NULL
,@VOTES1 = 100
,@VOTES2 = 0
,@VOTES3 = 0
,@VOTES4 = 0
,@VOTES5 = 0
,@VOTES6 = 0
,@VOTES7 = 0
,@VOTES8 = 0
,@VOTES9 = 0