[英]Send DataTable to Access DB using C#
I have table (about 250k rows) in Access db with date, time and name (other columns are irrelevant for this problem). 我在Access数据库中有表(大约250k行)的日期,时间和名称(其他列与此问题无关)。 I need to get just first (or last) date/time for name value and send it to new table.
我需要获取名称值的第一个(或最后一个)日期/时间并将其发送到新表。
I have tried SQL statement but for large number of rows it is too slow. 我已经尝试过SQL语句但是对于大量的行来说它太慢了。 here: Access SQL to return only first or last occurence by date
here: 访问SQL以仅按日期返回第一个或最后一个
I also tried read data from db to DataTable, looping through and deleting rows that I don't want, but I'm not able to send this edited DataTable back to db. 我也尝试从db到DataTable读取数据,循环并删除我不想要的行,但是我无法将这个编辑过的DataTable发送回db。 Similar to this: How do I insert a datatable into Microsoft Access?
与此类似: 如何将数据表插入Microsoft Access?
using(OleDbConnection con = new OleDbConnection(ConnectionString))
{
SQL = "Select * From OriginalTable";
var adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(SQL, con);
var cbr = new OleDbCommandBuilder(adapter);
try
{
con.Open();
DataTable dtFilter = new DataTable();
adapter.Fill(dtFilter);
string id = dtFilter.Rows[dtFilter.Rows.Count - 1][4].ToString();
for(int i = dtFilter.Rows.Count - 2; i >= 0; i--)
{
DataRow dr = dtFilter.Rows[i];
if(dtFilter.Rows[i][4].ToString() == id)
{
dr.Delete();
}
else
{
id = dtFilter.Rows[i][4].ToString();
}
}
dtFilter.AcceptChanges(); // DataTable looks as I want
adapter.Update(dtFilter); // Returns 0
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message, "OledbException Error");
}
catch(Exception x)
{
MessageBox.Show(x.Message, "Exception Error");
}
}
I expect to export dtFilter to Access db. 我希望将dtFilter导出到Access db。 Why it returns 0?
为什么它返回0?
I'm open for SQL statement as long as it is fast. 只要速度很快,我就会对SQL语句开放。
UPDATE: SQL Statement used for selecting first or last entry 更新: 用于选择第一个或最后一个条目的SQL语句
SELECT DISTINCT
cdate(Format(t.DateOS + t.TimeOS, 'dd.MM.yyyy HH:mm:ss')) AS DateTimeOS,
cdate(Format(t.DateOS, 'dd.MM.yyyy ')) AS DateOS,
cdate(Format(t.TimeOS, 'HH:mm:ss')) AS TimeOS,
t.EP AS EP, t.ID AS ID
FROM TestFirstLast AS t
WHERE t.EP = 'L100'
AND (((t.DateOS + t.TimeOS) > #1/1/2016 12:00:00 AM# AND (t.DateOS + t.TimeOS) <= #3/1/2016 12:00:00 AM#))
AND NOT EXISTS(SELECT 1 FROM TestFirstLast AS t2 WHERE t2.EP = t.EP AND t2.ID = t.ID AND (t2.DateOS < t.DateOS OR t2.DateOS = t.DateOS AND t2.TimeOS < t.TimeOS))
GOOD SOLUTION: I found good description of problem and solution here 好的解决方案:我在这里找到了问题和解决方案的良好描述
To use the .Update method, you have to provide an update SQL command. 要使用.Update方法,必须提供更新SQL命令。
The OleDbDataAdapter provides OleDbDataAdapter提供
If you are not setting the corresponding command, the DataAdapter does not know what to do with your data. 如果您未设置相应的命令,DataAdapter将不知道如何处理您的数据。
Have a look at OleDbDataAdapter Class 看看OleDbDataAdapter类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.