SQLiteDataAdapter 更新方法返回 0
我从 CSV 文件加载了 83 行,但是当我尝试更新 SQLite 数据库时,我得到 0 行...我不知道我做错了什么。
程序输出:
Num rows loaded is 83
Num rows updated is 0
源代码是:
public void InsertData(String csvFileName, String tableName)
{
String dir = Path.GetDirectoryName(csvFileName);
String name = Path.GetFileName(csvFileName);
using (OleDbConnection conn =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
dir + @";Extended Properties=""Text;HDR=Yes;FMT=Delimited"""))
{
conn.Open();
using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + name, conn))
{
QuoteDataSet ds = new QuoteDataSet();
adapter.Fill(ds, tableName);
Console.WriteLine("Num rows loaded is " + ds.Tags.Rows.Count);
InsertData(ds, tableName);
}
}
}
public void InsertData(QuoteDataSet data, String tableName)
{
using (SQLiteConnection conn = new SQLiteConnection(_connectionString))
{
using (SQLiteDataAdapter sqliteAdapter = new SQLiteDataAdapter("SELECT * FROM " + tableName, conn))
{
using (new SQLiteCommandBuilder(sqliteAdapter))
{
conn.Open();
Console.WriteLine("Num rows updated is " + sqliteAdapter.Update(data, tableName));
}
}
}
}
关于为什么它没有更新正确的行数的任何提示?
更新:
我尝试在调用更新之前设置命令,但仍然遇到同样的问题...代码现在是:
sqliteAdapter.InsertCommand = cmndBldr.GetInsertCommand();
Console.WriteLine("Num rows updated is " + sqliteAdapter.Update(data, tableName));
当我调试它时,命令文本是:_commandText = "插入 [标签] ([tagId]、[tagName]、[描述]、[colName]、[dataType]、[realTime]) 值 (@param1、@param2、@param3、@param4、@param5、@param6) "
这是一个以 xml 格式显示数据集状态的粘贴: http://pastie.org/936882< /a>
I loaded 83 rows from my CSV file, but when I try to update the SQLite database I get 0 rows... I can't figure out what I'm doing wrong.
The program outputs:
Num rows loaded is 83
Num rows updated is 0
The source code is:
public void InsertData(String csvFileName, String tableName)
{
String dir = Path.GetDirectoryName(csvFileName);
String name = Path.GetFileName(csvFileName);
using (OleDbConnection conn =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
dir + @";Extended Properties=""Text;HDR=Yes;FMT=Delimited"""))
{
conn.Open();
using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + name, conn))
{
QuoteDataSet ds = new QuoteDataSet();
adapter.Fill(ds, tableName);
Console.WriteLine("Num rows loaded is " + ds.Tags.Rows.Count);
InsertData(ds, tableName);
}
}
}
public void InsertData(QuoteDataSet data, String tableName)
{
using (SQLiteConnection conn = new SQLiteConnection(_connectionString))
{
using (SQLiteDataAdapter sqliteAdapter = new SQLiteDataAdapter("SELECT * FROM " + tableName, conn))
{
using (new SQLiteCommandBuilder(sqliteAdapter))
{
conn.Open();
Console.WriteLine("Num rows updated is " + sqliteAdapter.Update(data, tableName));
}
}
}
}
Any hints on why it's not updating the correct number of rows?
Update:
I tried to set the command before calling update and I'm still getting the same issue... the code is now:
sqliteAdapter.InsertCommand = cmndBldr.GetInsertCommand();
Console.WriteLine("Num rows updated is " + sqliteAdapter.Update(data, tableName));
When I debug it the command text is: _commandText = "INSERT INTO [Tags] ([tagId], [tagName], [description], [colName], [dataType], [realTime]) VALUES (@param1, @param2, @param3, @param4, @param5, @param6)"
Here is a pastie showing the state of dataset in xml format: http://pastie.org/936882
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:
读完你的解决方案后,让我说,我很尴尬,因为我没有明白这一点。枚举行以将行状态设置为添加将起作用。
但是,让我为您提供一种使用adapter.AcceptChangesDuringFill 更简洁的方法来完成此操作。
这将导入 83 行:
Program.cs
原始答案:
这是最终被调用的 SQLiteDataAdapter 构造函数的源代码。请注意,没有使用命令生成器。您需要在 SQLiteDataAdapter 上显式设置 InserCommand 属性,也许可以使用 SQLiteCommandBuilder?
Update:
After reading your solution let me say that I am embarrased that I did not catch that. Enumerating rows to set rowstate to added will work.
But let me give you a cleaner way to do that using adapter.AcceptChangesDuringFill.
This imports 83 rows:
Program.cs
Original Answer:
This is the source of the SQLiteDataAdapter ctor that ultimately gets called. Note that no command builder is employed. You need to explicitly set the InserCommand property on the SQLiteDataAdapter, perhaps by using a SQLiteCommandBuilder?
我能够通过遍历每一行并通过调用 SetAdded(); 更改其状态来解决这个问题...在我这样做之后,Update 命令就像一个魅力。
我假设当从 CSV 文件填充
DataSet
并尝试调用Update
以便将数据插入数据库时,行的状态会发生变化不表明任何变化。我们必须告诉DataAdapter
DataSet
发生了变化,因为它看到的只是DataSet
没有发生任何变化它填充的 CSV 文件,它没有意识到这些是我试图将数据放入的数据库的全新行。I was able to get around the issue by going through each row and changing it's state by calling
SetAdded();
... after I did that the Update command worked like a charm.I assume that when the
DataSet
is filled from the CSV file and I then I attempt to callUpdate
in order to insert the data into the database, the state of the row does not indicate any changes. We have to tell theDataAdapter
that there is a change in theDataSet
because all it sees is that are no changes to theDataSet
with respect to the CSV file it was populated from and it doesn't realize that these are brand new rows for the database I'm trying to put the data in.