更新错误:Column name or number of supplied values does not match table definition

songhongzhe123 2012-03-29 11:00:47
private void btnSave_Click(object sender, System.EventArgs e)
{
string sqlStatement;
//根据是否正在添加新记录来建立适当的查询语句
if (bNewRocord == true)
{
sqlStatement = "INSERT INTO Customers VALUES(" + "'" + cbxID.Text + "'," + "'" + txtCompanyName.Text + "'," + "'" + txtContactTitle.Text + "'," + "'" + txtAddress.Text + "'," + "'" + txtCity.Text + "'," + "'" + txtRegion.Text + "'," + "'" + txtPostalCode.Text + "'," + "'" + txtCountry.Text + "'," + "'" + txtPhone.Text + "'," + "'" + txtFax.Text + "')";
}
else
{
sqlStatement = "UPDATA Customers SET " + "CompanyName= '" + txtCompanyName.Text + "'," + "ContactName= '" + txtContactName.Text + "'," + "ContactTitle= '" + txtContactTitle.Text + "'," + "Address= '" + txtAddress.Text + "'," + "City= '" + txtCity.Text + "'," + "Region= '" + txtRegion.Text + "'," + "PostalCode= '" + txtPostalCode.Text + "'," + "Country= '" + txtCountry.Text + "'," + "Phone= '" + txtPhone.Text + "'," + "Fax= '" + txtFax.Text + "," + "WHERE GetCustomeID= '" + cbxID.Text + "'";
}

//创建sql命令
SqlCommand sqlcmd = new SqlCommand(sqlStatement, sqlConnection1);
try
{
sqlConnection1.Open();
int rowAffected = sqlcmd.ExecuteNonQuery();
if (rowAffected == 1)
cbxID.Items.Add(cbxID.Text);
}
catch (SqlException ex)
{
MessageBox.Show("更新错误:"+ex.Message,"出现错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
sqlConnection1.Close();
}
if (bNewRocord == true)
{
cbxID.DropDownStyle = ComboBoxStyle.DropDownList;
bNewRocord = false;
cbxID.SelectedIndex = cbxID.Items.Count - 1;
}
}

...全文
1844 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
songhongzhe123 2012-03-30
  • 打赏
  • 举报
回复
正解。我这写10个 数据库里11个[Quote=引用 1 楼 的回复:]
插入的值和数据库中的不匹配

别拼接SQL语句
用参数!
[/Quote]
mabaolin 2012-03-29
  • 打赏
  • 举报
回复
cbxID.Text 是key吧?每次insert的值应当不同
songhongzhe123 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
把 sqlStatement 显示出来看看,或者放到数据库中执行一下
[/Quote]貌似是我数据库的问题,那我数据库就开始插入了一行,后面就没法插入新的行了?什么原因呢?
bdmh 2012-03-29
  • 打赏
  • 举报
回复
把 sqlStatement 显示出来看看,或者放到数据库中执行一下
  • 打赏
  • 举报
回复
插入的值和数据库中的不匹配

别拼接SQL语句
用参数!

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧