我创建了一个包含表格的数据库我试图将参数化的查询插入到表格中。我需要一个循环,可以根据查询中的数据识别要添加的参数。循环执行得很好并且在调试时所有正确的数据都正确地放在参数中。但是,一旦查询执行并且我检查数据库,我看到已添加新记录但所有列的所有值都为空或空。
我无法理解为什么如果发送正确的数据并且没有错误返回,为什么空白数据会输入。有什么想法吗?
这是我的代码: 查询:
INSERT into alerts (`emailAddress`,`authorName`,`alertSubj`,`alertBody`,`email`,`alertExpires`,`releaseDateTime`)
VALUES (@emailAddress,@authorName,@alertSubj,@alertBody,@email,@alertExpires,@releaseDateTime);
valueArray中元素的图像:
emailAddress
authorName
alertSubj
alertBody
email
alertExpires
releaseDateTime
循环参数:
foreach(String data in valueArray)
{
String paraName = "\"";
paraName+="@" + valueArray[p];
paraName += "\"";
switch (valueArray[p])
{
case "emailAddress":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(emailAddress));
break;
case "twitter":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(twitter));
break;
case "email":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(email));
break;
case"sms":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(text));
break;
case "desktop":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(desktop));
break;
case "playSound":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(voice));
break;
case"releaseDateTime":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(releaseDate_Time));
break;
case "alertExpires":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(expiresDate_Time));
break;
case "alertSubj":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(alertSubject));
break;
case "alertBody":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(emailBody));
break;
case "shortMsgBody":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(shortAlert));
break;
case"authorName":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(author));
break;
case "criticality":
cmd.Parameters.AddWithValue(paraName, HttpUtility.HtmlEncode(level));
break;
default:
break;
}
p++;
}
cmd.ExecuteNonQuery();
答案 0 :(得分:0)
不要在参数名称中添加引号。而不是
String paraName = "\"";
paraName+="@" + valueArray[p];
paraName += "\"";
只做
String paraName = "@" + valueArray[p];
此外,由于您已经通过阵列循环 - 因此无需添加其他计数器。你可以做点什么
foreach (String data in valueArray)
{
switch (data)
{
case "emailAddress":
cmd.Parameters.AddWithValue("@" + data, HttpUtility.HtmlEncode(emailAddress));
break;
....
}
}