更新错误:Column name or number of supplied values does not match table definition
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;
}
}
------解决方案--------------------插入的值和数据库中的不匹配
别拼接SQL语句
用参数!
------解决方案--------------------把 sqlStatement 显示出来看看,或者放到数据库中执行一下