日期:2014-05-17  浏览次数:20844 次

INSERT INTO 语句错误
private int myExecutNoQurey(string sql)
{
OleDbConnection cnn = new OleDbConnection(myConnectionString);
cnn.Open();
OleDbCommand cmd = new OleDbCommand(sql, cnn);
cmd.CommandType = CommandType.Text;
int i = cmd.ExecuteNonQuery();
cnn.Close();
return i;
}
private void button1_Click(object sender, EventArgs e)
{
string sql = string.Format
("insert into Books(Name,Price,Author,Publish,Number) values ('{0}','{1}','{2}','{3}','{4}')"
, textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text);
int flag = myExecutNoQurey(sql);
if (flag > 0)
{
MessageBox.Show("Sccessful!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Defeated!", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

------最佳解决方案--------------------
检查下字段类型,比如你的表Books中Price是decimal的话,那么你插入个字符型是不对的,还是Name字段是保留字,使用最好加一对中括号

string sql = string.Format
("insert into Books([Name],Price,Author,Publish,Number) values ('{0}','{1}','{2}','{3}','{4}')"
, textBox1.Text, decimal.Parse(textBox2.Text), textBox3.Text, textBox4.Text, textBox5.Text);

你自己对照数据库表字段类型,进行检查即可。

其实这个最好的办法就是断点调试,很快就能定位出错的地方。
------其他解决方案--------------------
下一个断点看看sql是否正确。
是否字段名、字段类型正确,是否有重复的主键记录。
------其他解决方案--------------------
断点 看向sql 字符串 仔细比对 每个字符 是否有错误
------其他解决方案--------------------
检查下字段名和字段属性是否有误,你也可以进行调试将插入语句在MicrosoftSQL中运行下,看能否插入数据......
------其他解决方案--------------------
就是这个,忘打中括号了,谢谢各位哦。
引用:
检查下字段类型,比如你的表Books中Price是decimal的话,那么你插入个字符型是不对的,还是Name字段是保留字,使用最好加一对中括号

string sql = string.Format
("insert into Books([Name],Price,Author,Publish,Number) values ('{0}','{1}','{2}','……