求教,关于insert语句的问题,
求教各位大侠,小弟执行以下语句时 cmd.ExecuteNonQuery()报错,为insert语句错误,麻烦各位帮忙检查下问题出在哪了,小弟跪谢~~
string c1 = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
double c2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
string c3 = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
double c4 = Convert.ToDouble(dataGridView1.Rows[i].Cells[4].Value);
double c5 = Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value);
double c6 = Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value);
double c8 = Convert.ToDouble(dataGridView1.Rows[i].Cells[8].Value);
string ConStr1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + @"\db1.mdb";
OleDbConnection conn1 = new OleDbConnection();
String str1 = "./db1.mdb";
OleDbConnection OleDbcon1 = new OleDbConnection("Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + str1 + ";User ID=admin;Password=;Jet OLEDB:Database Password=");
try
{
OleDbcon1.Open();
OleDbCommand cmd = new OleDbCommand("insert into " + s + " (商品名称,数量,单位,单价,小计,进价,利润) values(" + c1 + "," + c2 + "," + c3 + "," + c4 + "," + c5 + "," + c6 + "," + c8 + ",)", OleDbcon1);
int result1 = cmd.ExecuteNonQuery();
}
finally
{
OleDbcon1.Close();
}
------解决方案--------------------
加单引号改成
OleDbCommand cmd = new OleDbCommand("insert into " + s + " (商品名称,数量,单位,单价,小计,进价,利润) values('" + c1 + "','" + c2 + "','" + c3 + "','" + c4 + "','" + c5 + "','" + c6 + "','" + c8 + "',)", OleDbcon1);
你的s也是变量?
------解决方案--------------------string ConStr1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + @"\db1.mdb";
OleDbConnection conn1 = new OleDbConnection();
这些代码干什么用的?
------解决方案--------------------加上单引号。。还是OleDbConnection conn1 = new OleDbConnection();这句你就没用。。。
你用的是OleDbcon1这个OleDbConnection
------解决方案--------------------
------解决方案--------------------OleDbCommand cmd = new OleDbCommand("insert into [" + s + "] (商品名称,数量,单位,单价,小计,进价,利润) values('" + c1 + "'," + c2 + ",'" + c3 + "'," + c4 + "," + c5 + "," + c6 + "," + c8 + ")", OleDbcon1);