将 DataTable 保存到数据库表中
我正在从我的应用程序生成一个数据表,我想将整个数据表保存到一个数据库表中。
DataTable ds = //add the info from the queue in the application
数据表正在生成,但接下来要做什么。向我展示一些语法。我也不需要那里的 select 语句,我只想将数据表中的所有信息插入到已经创建的数据库表中(更新表)。 我将使用ODBC连接访问MYSQL数据库
我想直接通过dataset将数据更新到数据库中
public void update(DataTable ds)
{
try
{
lock (myLockHolder)
{
X1 = 1;
OdbcConnection con =
new OdbcConnection(LocalConnection.GetLocalConnetionString());
OdbcCommand cmd;
OdbcDataAdapter da;
DataSet ds1=new DataSet();
string query = "";
query = "update parameter" + Environment.NewLine;
query += "set paramvalue=paramvalue,date_logged1=date_logged1,"
+ Environment.NewLine;
query += " Quality=Quality,date_logged=date_logged"
+ Environment.NewLine;
query += " where itemID=itemID";
cmd = new OdbcCommand(query, con);
da = new OdbcDataAdapter(cmd);
ds1=new DataSet();
ds1.Tables.Add(ds);
da.Update(ds1);
}
}
catch { }
finally { }
}
它将像这样使用方法捕获此异常 “更新无法找到 TableMapping['Table'] 或 DataTable 'Table'。”
I am generating a Data Table from my application and I would like to save the whole Data Table into one database table.
DataTable ds = //add the info from the queue in the application
The DataTable is getting generated but What to do next.Show me some syntax.I dont really need the select statement there either, i just want to insert all the info from the DataTable into already created db table(update the table).
I will use the ODBC connection to access the MYSQL database
i want to update the data into the database through dataset directly
public void update(DataTable ds)
{
try
{
lock (myLockHolder)
{
X1 = 1;
OdbcConnection con =
new OdbcConnection(LocalConnection.GetLocalConnetionString());
OdbcCommand cmd;
OdbcDataAdapter da;
DataSet ds1=new DataSet();
string query = "";
query = "update parameter" + Environment.NewLine;
query += "set paramvalue=paramvalue,date_logged1=date_logged1,"
+ Environment.NewLine;
query += " Quality=Quality,date_logged=date_logged"
+ Environment.NewLine;
query += " where itemID=itemID";
cmd = new OdbcCommand(query, con);
da = new OdbcDataAdapter(cmd);
ds1=new DataSet();
ds1.Tables.Add(ds);
da.Update(ds1);
}
}
catch { }
finally { }
}
It will be used like this method catch this exception
"Update unable to find TableMapping['Table'] or DataTable 'Table'."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该创建插入 sql 字符串,循环遍历数据表中的所有行,为数据表中的每一列添加参数,然后执行此查询。
You should create insert sql string, loop thorugh all rows in your datatable, add parameters for each column in datatable, and execute this query.
您写道:>我想要没有 foreach 循环
尝试在 dotConnect 中使用 MySqlDataTable 类对于 MySQL。阅读有关 MySqlDataTable.Update() 方法和 MySqlDataTable.CachedUpdates 状态的更多信息。
You wrote: >i want without foreach loop
Try to use MySqlDataTable class in dotConnect for MySQL. Read more about the MySqlDataTable.Update() method and MySqlDataTable.CachedUpdates state.