使用cmd.ExecuteNonQuery();时,出现insert into语法错误
老鸟们帮帮忙!!!我做的是一个C#+access的C/S程序,主要是往库里插数据privatevoidbutton1_Click(objectsender,EventAr...
老鸟们帮帮忙!!!我做的是一个C#+access的C/S程序,主要是往库里插数据
private void button1_Click(object sender, EventArgs e)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=account.mdb";
OleDbConnection conn = new OleDbConnection(strConn);
string date = textBox1.Text.Trim();
string sql = "select count(*) from AccountTable where date='" + textBox1.Text +"'";
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
int n = (int)cmd.ExecuteScalar();
if (n > 0)
{
MessageBox.Show("该记录重复!(可能日期与表内日期重复)");
}
cmd.CommandText = "insert into AccountTable (date,earning,eatery,life,capture,bleeding,borrow,loan,remark) values (@date,@earning,@eatery,@life,@capture,@bleeding,@borrow,@loan,@remark)";
cmd.Parameters.Add("@data", OleDbType.Date).Value =textBox1.Text;
cmd.Parameters.Add("@earning", OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@eatery", OleDbType.VarChar).Value = textBox3.Text;
cmd.Parameters.Add("@life", OleDbType.VarChar).Value = textBox4.Text;
cmd.Parameters.Add("@capture", OleDbType.VarChar).Value = textBox5.Text;
cmd.Parameters.Add("@bleeding", OleDbType.VarChar).Value = textBox6.Text;
cmd.Parameters.Add("@borrow", OleDbType.VarChar).Value = textBox7.Text;
cmd.Parameters.Add("@loan", OleDbType.VarChar).Value= textBox8.Text;
cmd.Parameters.Add("@remark", OleDbType.VarChar).Value = textBox9.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
刚开始以为是sql语句不对,可是看了半天,改了又改还是出现这错误。
哎呀~~上火啊!!!数据库里9个字段,没错,数据类型就date用的是日期类型,然后我试了试改成文本类型的还是不对!!!
哇哇~~~我要疯了~~~
大虾~~大哥~~大姐~~大葱~~大蒜~~大象~~大什么都好,帮帮我这可怜的人吧~~ 展开
private void button1_Click(object sender, EventArgs e)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=account.mdb";
OleDbConnection conn = new OleDbConnection(strConn);
string date = textBox1.Text.Trim();
string sql = "select count(*) from AccountTable where date='" + textBox1.Text +"'";
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
int n = (int)cmd.ExecuteScalar();
if (n > 0)
{
MessageBox.Show("该记录重复!(可能日期与表内日期重复)");
}
cmd.CommandText = "insert into AccountTable (date,earning,eatery,life,capture,bleeding,borrow,loan,remark) values (@date,@earning,@eatery,@life,@capture,@bleeding,@borrow,@loan,@remark)";
cmd.Parameters.Add("@data", OleDbType.Date).Value =textBox1.Text;
cmd.Parameters.Add("@earning", OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@eatery", OleDbType.VarChar).Value = textBox3.Text;
cmd.Parameters.Add("@life", OleDbType.VarChar).Value = textBox4.Text;
cmd.Parameters.Add("@capture", OleDbType.VarChar).Value = textBox5.Text;
cmd.Parameters.Add("@bleeding", OleDbType.VarChar).Value = textBox6.Text;
cmd.Parameters.Add("@borrow", OleDbType.VarChar).Value = textBox7.Text;
cmd.Parameters.Add("@loan", OleDbType.VarChar).Value= textBox8.Text;
cmd.Parameters.Add("@remark", OleDbType.VarChar).Value = textBox9.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
刚开始以为是sql语句不对,可是看了半天,改了又改还是出现这错误。
哎呀~~上火啊!!!数据库里9个字段,没错,数据类型就date用的是日期类型,然后我试了试改成文本类型的还是不对!!!
哇哇~~~我要疯了~~~
大虾~~大哥~~大姐~~大葱~~大蒜~~大象~~大什么都好,帮帮我这可怜的人吧~~ 展开
1个回答
展开全部
OleDbCommand cmd = new OleDbCommand(sql, conn);
int n = (int)cmd.ExecuteScalar();
OleDbCommand cmd1 = new OleDbCommand("insert into AccountTable (date,earning,eatery,life,capture,bleeding,borrow,loan,remark) values (@date,@earning,@eatery,@life,@capture,@bleeding,@borrow,@loan,@remark)";
, conn);
cmd1.ExecuteNonQuery();
应该是这样写吧。。一个cmd怎么能有2个SQL语句呢。。如果想在button1下做2个事情要写2个cmd与cmd1来区分下
int n = (int)cmd.ExecuteScalar();
OleDbCommand cmd1 = new OleDbCommand("insert into AccountTable (date,earning,eatery,life,capture,bleeding,borrow,loan,remark) values (@date,@earning,@eatery,@life,@capture,@bleeding,@borrow,@loan,@remark)";
, conn);
cmd1.ExecuteNonQuery();
应该是这样写吧。。一个cmd怎么能有2个SQL语句呢。。如果想在button1下做2个事情要写2个cmd与cmd1来区分下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询