一个参数值是逐字更新,但其他工作良好c#
本文关键字:更新 其他 工作 一个 参数 | 更新日期: 2023-09-27 18:13:03
我在插入语句中工作,其中3个值需要从gridview抓取数据并将其保存在表中插入,但"关键字参数值正在像数据库中的@关键字一样更新",但其他值得到很好。
下面是代码
using (SqlCommand com = new SqlCommand("INSERT INTO PositionsTable (Id,Keyword,Position) VALUES (@Id,'@Keyword',@Position)", con))
{
com.Parameters.AddWithValue("@Keyword", SqlDbType.VarChar);
com.Parameters.AddWithValue("@Position", SqlDbType.Int);
com.Parameters.AddWithValue("@Id", SqlDbType.Int);
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string keywordCell = dataGridView1.Rows[i].Cells["Keywords"].Value.ToString();
int positionCell = Convert.ToInt16(dataGridView1.Rows[i].Cells["Position"].Value);
com.Parameters["@Keyword"].Value = keywordCell;
com.Parameters["@Position"].Value = positionCell;
com.Parameters["@Id"].Value = count;
com.ExecuteNonQuery();
}
}
我在调试中测试了keywordCell变量成功地从gridview中获得像positionCell一样的值,但它在数据库中获得更新"@Keyword",其中positionCell更新正确。
你能指出我错在哪里吗?
SQL语句中的关键字有引号
改变"INSERT INTO PositionsTable (Id,Keyword,Position) VALUES (@Id,'@Keyword',@Position)"
"INSERT INTO PositionsTable (Id,Keyword,Position) VALUES (@Id,@Keyword,@Position)"
'@Keyword'
in VALUES(…)错误。
INSERT INTO PositionsTable (Id,Keyword,Position) VALUES (@Id, @Keyword, @Position)"