我正在尝试将数据插入数据库,但我在这行代码中不断收到此错误:
command.ExecuteNonQuery();
这是我得到的错误:
数据类型文本和文本在等于运算符中不兼容。
这是我的方法:
public List<QuestionsClass> updateQuestions(List<QuestionsClass> items)
{
connection = new SqlConnection(connectionString);
command = new SqlCommand(@"IF EXISTS(SELECT * FROM QuestionnaireAnswer WHERE questionID = @questionID AND community = @community AND lot = @lot)
BEGIN
UPDATE QuestionnaireAnswer SET answer = @answer, textBox = @textBox WHERE questionID = @questionID AND community = @community AND lot = @lot
END
ELSE
BEGIN
INSERT INTO QuestionnaireAnswer (questionID, answer, community, lot, textBox) VALUES (@questionID, @answer, @community, @lot, @textBox)
END");
command.Parameters.Add("@questionID", System.Data.SqlDbType.Int);
command.Parameters.Add("@answer", System.Data.SqlDbType.Text);
command.Parameters.Add("@community", System.Data.SqlDbType.Text);
command.Parameters.Add("@lot", System.Data.SqlDbType.Int);
command.Parameters.Add("@textBox", System.Data.SqlDbType.Text);
command.Connection = connection;
connection.Open();
for (int i = 0; i < items.Count; i++)
{
command.Parameters["@questionID"].Value = items[i].questionID;
command.Parameters["@answer"].Value = items[i].answer;
command.Parameters["@community"].Value = items[i].community;
command.Parameters["@lot"].Value = items[i].lot;
command.Parameters["@textBox"].Value = items[i].textBox;
command.ExecuteNonQuery();
}
connection.Close();
return items;
}
这是我的QuestionsClass
public class QuestionsClass
{
public int questionID { get; set; }
public string answer { get; set; }
public string community { get; set; }
public int lot { get; set; }
public string textBox { get; set; }
}
回答,社区和textBox正在填充字符串。
在我的数据库答案中,社区和文本框是数据类型文本。我做错了什么?
请帮助。