从 DataTable C# 添加到 OLE 类型表
Posted
技术标签:
【中文标题】从 DataTable C# 添加到 OLE 类型表【英文标题】:Adding to OLE type table from DataTable C# 【发布时间】:2017-01-30 18:28:37 【问题描述】:我正在尝试使用来自 DataTable 对象的集合 DataRows 将数据添加到 Access 表中,并在查看器中看到一组很好的数据 (43) 记录,但它填充了相同的记录 43 次。我的参数设置错误还是我做错了什么?
private void PopulateDB(DataTable dtDB)
lblDataStatus.Text = "populating master table...";
this.Refresh();
progressBar1.Visible = true;
progressBar1.Value = 1;
progressBar1.Maximum = dtDB.Rows.Count;
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\path\fname.accdb";
using (OleDbConnection conn = new OleDbConnection (strConn ))
using (OleDbCommand cmd = new OleDbCommand())
foreach (DataRow dr in dtDB .Rows)
progressBar1.PerformStep();
try
cmd.CommandText = "INSERT INTO PMADocMaster( PN, PNNewRev, PN8Digit, ECO, Mon, SupNum, URL ) VALUES (?,?,?,?,?,?,?)";
cmd.Parameters.Add("@pn", OleDbType.VarChar).Value = dr.Field<string>("PNFullNum");
cmd.Parameters.Add("@rev", OleDbType.VarChar).Value = dr.Field<string>("PNNewRev");
cmd.Parameters.Add("@pn8", OleDbType.VarChar).Value = dr.Field<string>("PN8Dig");
cmd.Parameters.Add("@eco", OleDbType.VarChar).Value = dr.Field<string>("ECO");
cmd.Parameters.Add("@mon", OleDbType.VarChar).Value = dr.Field<string>("Mon");
cmd.Parameters.Add("@supnum", OleDbType.VarChar).Value = dr.Field<string>("SupNum");
cmd.Parameters.Add("@url", OleDbType.VarChar).Value = dr.Field<string>("URL");
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
catch (Exception e)
MessageBox.Show("Error: " + e.Message);
lblDataStatus.Text = e.Message;
return;
// duplicates are happening, will check veracity of data afterwards
progressBar1.Visible = false;
【问题讨论】:
如果您使用 DataAdapter,它将为您完成所有更新。从 DataTable 中提取数据以发送到 DB 是相当奇怪的 【参考方案1】:您可以使用 cmd.Parameters.clear() 或优化您的代码,如下所示..
private void PopulateDB(DataTable dtDB)
lblDataStatus.Text = "populating master table...";
this.Refresh();
progressBar1.Visible = true;
progressBar1.Value = 1;
progressBar1.Maximum = dtDB.Rows.Count;
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\path\fname.accdb";
using (OleDbConnection conn = new OleDbConnection(strConn))
using (OleDbCommand cmd = new OleDbCommand("INSERT INTO PMADocMaster( PN, PNNewRev, PN8Digit, ECO, Mon, SupNum, URL ) VALUES (?,?,?,?,?,?,?)",conn))
try
cmd.Parameters.Add("@pn");, OleDbType.VarChar);
cmd.Parameters.Add("@rev", OleDbType.VarChar);
cmd.Parameters.Add("@pn8", OleDbType.VarChar);
cmd.Parameters.Add("@eco", OleDbType.VarChar);
cmd.Parameters.Add("@mon", OleDbType.VarChar);
cmd.Parameters.Add("@supnum", OleDbType.VarChar);
cmd.Parameters.Add("@url", OleDbType.VarChar);
conn.Open();
foreach (DataRow dr in dtDB.Rows)
progressBar1.PerformStep();
cmd.Parameters["@pn"].Value = dr.Field<string>("PNFullNum");
cmd.Parameters["@rev"].Value = dr.Field<string>("PNNewRev");
cmd.Parameters["@pn8"].Value = dr.Field<string>("PN8Dig");
cmd.Parameters["@eco"].Value = dr.Field<string>("ECO");
cmd.Parameters["@mon"].Value = dr.Field<string>("Mon");
cmd.Parameters["@supnum"].Value = dr.Field<string>("SupNum");
cmd.Parameters["@url"].Value = dr.Field<string>("URL");
cmd.ExecuteNonQuery();
conn.Close();
catch (Exception e)
MessageBox.Show("Error: " + e.Message);
lblDataStatus.Text = e.Message;
return;
// duplicates are happening, will check veracity of data afterwards
progressBar1.Visible = false;
【讨论】:
以上是关于从 DataTable C# 添加到 OLE 类型表的主要内容,如果未能解决你的问题,请参考以下文章
在 C# 中将数据加载到 DataTable 会出现“未知 SQL 类型 - 0”错误