求教各位大侠,小弟执行以下语句时 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();
}
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也是变量?
OleDbConnection conn1 = new OleDbConnection();
这些代码干什么用的?
string s = comboBox1.Text + DateTime.Now.ToString("yyyyMMddhhmm");
加上单引号。。还是OleDbConnection conn1 = new OleDbConnection();这句你就没用你用的是OleDbcon1这个OleDbConnection
总体上就是点击按钮打印,然后在数据库中创建表并存下datagridview选中的行,
private void button3_Click(object sender, EventArgs e)
{
printDocument1.Print();
DialogResult result = MessageBox.Show("是否存档", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
int count = Convert.ToInt32(dataGridView1.Rows.Count.ToString());
string s = comboBox1.Text + DateTime.Now.ToString("yyyyMMddhhmm");
string Sql = "create table " + s + " (商品名称 varchar(50) null,数量 double null, 单位 varchar(50) null,单价 double null," + "小计 double null, 进价 double null,id counter primary key,利润 double null)";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + @"\db2.mdb";
OleDbConnection conn = new OleDbConnection();
String str = "./db2.mdb";
OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + str + ";User ID=admin;Password=;Jet OLEDB:Database Password=");
try
{ OleDbcon.Open();
OleDbConnection oleCon = new OleDbConnection(ConStr);
using (OleDbConnection cn = new OleDbConnection(ConStr))
{
cn.Open();
OleDbCommand cmd = new OleDbCommand(Sql, cn);
cmd.ExecuteNonQuery();
MessageBox.Show("发货单入库");
cn.Close(); }
}
catch
{
MessageBox.Show("表创建失败");
return; }
for (int i = 0; i < count; i++)
{
dataGridView1.EndEdit();
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true)
{
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";
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();
}
}
}
}
}
string ConStr1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + @"\db2.mdb";
String str1 = "./db2.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();
}