我运行下面代码,连续2次插入时,第一次能更新插入,第二次插入的记录为空,为什么会有这种怪事 /// <summary>
/// 更新
/// </summary>
/// <param name="strDbPath">数据库</param>
/// <param name="TabName">表名</param>
/// <param name="dt">要更新的数据</param>
public static void InsertTab(string strDbPath, string TabName, DataTable dt)
{
OleDbConnection conn = Common.GetOleConn(strDbPath);
if (conn == null) return;
try
{
string sql = " select top 1 * from " + TabName;//获取一张空表
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable DestDt = ds.Tables[0];
if (DestDt.Rows.Count > 0) DestDt.Rows.Clear();
DestDt = dt;
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
adapter.Update(DestDt);
}
catch (Exception err)
{
MessageBox.Show("frmStantardPropertyTab->InsertInoTab" + "\r\n" + err.StackTrace);
}
finally
{
conn.Close();
conn.Dispose();
}
}
/// 更新
/// </summary>
/// <param name="strDbPath">数据库</param>
/// <param name="TabName">表名</param>
/// <param name="dt">要更新的数据</param>
public static void InsertTab(string strDbPath, string TabName, DataTable dt)
{
OleDbConnection conn = Common.GetOleConn(strDbPath);
if (conn == null) return;
try
{
string sql = " select top 1 * from " + TabName;//获取一张空表
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable DestDt = ds.Tables[0];
if (DestDt.Rows.Count > 0) DestDt.Rows.Clear();
DestDt = dt;
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
adapter.Update(DestDt);
}
catch (Exception err)
{
MessageBox.Show("frmStantardPropertyTab->InsertInoTab" + "\r\n" + err.StackTrace);
}
finally
{
conn.Close();
conn.Dispose();
}
}
UpdateCommand沒有定義,沒有給出Update的Sql語句,當然不會更新啦!
TOiceburnt:是没有UPDATE语句,但为什么第一次插入更新可以,接着在重复第二次插入就什么也没有.
DestDt = dt; 你应该对DestDt 直接操作,而不能使用dt,因为这样就改变了DestDt的原有引用了。
第二次和第一次的DT(DATATABLE 要更新的数据)是一样的,但是重复的做第二次时,
插入到ACCESS表中的记录为空,记录数没变化.
DestDt 是ACCESS中等待要插入的表.而且每次都初始化了.应该不会改变了原有的东西把.
其实我这个东西就是:把一个DT(DATATABLE)中的数据完完全全地插入到ACCESS中.
想找个简单点的方法.
具体说明请在MSDN索引“自动生成的命令”。
^_^一次存储多条数据
1、用dataset
try
{
con = new OleDbConnection(constr);
con.Open();
OleDbCommand cmd = new OleDbCommand("Select St_CPID as 产品编号,St_CPName as 产品名称,St_CPAnquan as 安全存量 from TbStoreChanpinxinxi", con);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
OleDbCommandBuilder cbuild = new OleDbCommandBuilder(da);
da.Update(dscpxx1, "TbStoreChanpinxinxi");
dataGridView1.DataSource = dscpxx1.Tables["TbStoreChanpinxinxi"].DefaultView;
MessageBox.Show("更新成功");
button2.Enabled = false;
con.Close();
con.Dispose();
cmd.Dispose();
da.Dispose();
dscpxx1.Dispose();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }2、用循环int j = dataGridView3.NewRowIndex;
if (comboBox2.Text != "" && dataGridView3.Rows[j+1].Cells[1].Value.ToString() != "")
{
try
{
for (int i = 0; i <= dataGridView3.RowCount - 1; i++)
{ con = new OleDbConnection(constr);
con.Open();
OleDbCommand cmd = new OleDbCommand("insert into TbStoreTZxinxi(St_TZCPJID, St_TZCPID, St_TZCPName,St_TZCPGuige, St_TZCPDW,St_TZCPShuliang, St_TZCPJiage, St_TZCPJine,St_TZCKID, St_TZCKName, St_TZDYhaoma,St_TZDate) values('" + dataGridView3.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView3.Rows[i].Cells[1].Value.ToString() + "','" + dataGridView3.Rows[i].Cells[2].Value.ToString() + "','" + dataGridView3.Rows[i].Cells[3].Value.ToString() + "','" + dataGridView3.Rows[i].Cells[4].Value.ToString() + "','" + dataGridView3.Rows[i].Cells[5].Value.ToString() + "','" + dataGridView3.Rows[i].Cells[6].Value.ToString() + "','" + dataGridView3.Rows[i].Cells[7].Value.ToString() + "','" + comboBox2.Text + "','" + comboBox3.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')", con);
cmd.ExecuteNonQuery();
OleDbCommand cmd2 = new OleDbCommand("select St_CPKucun from TbStoreChanpinxinxi where St_CPID='" + dataGridView3.Rows[i].Cells[1].Value.ToString() + "'", con);
OleDbDataReader myread = cmd2.ExecuteReader();
if (myread.Read())
{
OleDbCommand cmd1 = new OleDbCommand("Update TbStoreChanpinxinxi set St_CPKucun=('" + (Int32.Parse(myread["St_CPKucun"].ToString()) + Int32.Parse(dataGridView3.Rows[i].Cells[5].Value.ToString())) + "') where St_CPID='" + dataGridView3.Rows[i].Cells[1].Value.ToString() + "'", con);
cmd1.ExecuteNonQuery();
cmd1.Dispose();
}
con.Close();
con.Dispose();
cmd.Dispose();
}
MessageBox.Show("保存了" + dataGridView3.Rows.Count.ToString() + "条数据");
MessageBox.Show("库存已更新");
tzxinxi1.Tables[0].Clear();
dataGridView3.DataSource = tzxinxi1.Tables[0].DefaultView;
}
catch (Exception ex) { MessageBox.Show("错误:" + ex.Message); }
}
else if (comboBox2.Text == "" || dataGridView3.Rows[j].Cells[1].Value.ToString() == "")
{
MessageBox.Show("产品编号不能为空");
}数据库字段太多了。