SQLiteDataAdapter Update方法返回0

时间:2010-04-26 03:38:06

标签: c# sqlite ado.net dataadapter

我从我的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));
            }
        }
    }
}

有关为什么没有更新正确行数的任何提示?

更新:

我尝试在调用update之前设置命令,但我仍然遇到同样的问题......代码现在是:

sqliteAdapter.InsertCommand = cmndBldr.GetInsertCommand();
Console.WriteLine("Num rows updated is " + sqliteAdapter.Update(data, tableName));

当我调试它时,命令文本是:_commandText = "INSERT INTO [Tags] ([tagId], [tagName], [description], [colName], [dataType], [realTime]) VALUES (@param1, @param2, @param3, @param4, @param5, @param6)"

这是一个以xml格式显示数据集状态的贴图:http://pastie.org/936882

2 个答案:

答案 0 :(得分:2)

<强>更新

在阅读完解决方案后,让我说我很尴尬,我没有抓住这个。枚举行以将rowstate设置为添加将有效。

但是,让我使用adapter.AcceptChangesDuringFill为您提供更清晰的方法。

    using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + name, conn))
    {
        // this is the proper way to transfer rows
        adapter.AcceptChangesDuringFill = false;

        QuoteDataSet ds = new QuoteDataSet();
        adapter.Fill(ds, tableName);
        Console.WriteLine("Num rows loaded is " + ds.Tags.Rows.Count);
        InsertData(ds, tableName);
    }

导入83行:

Program.cs的

using System;
using System.Data;
using System.Data.SQLite;

namespace SqliteCsv
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // fill your dataset
            QuoteDataSet ds = new QuoteDataSet();
            ds.ReadXml("data.xml", XmlReadMode.InferTypedSchema);


            // hack to flag each row as new as per lirik
            // OR just set .AcceptChangesDuringFill=false on adapter
            foreach (DataTable table in ds.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    row.SetAdded();
                }
            }

            int insertedRows;
            using (
                SQLiteConnection con =
                    new SQLiteConnection(@"data source=C:\Projects\StackOverflowAnswers\SqliteCsv\SqliteCsv\Quotes.db"))
            {
                con.Open();

                // clear table - you may not want this
                SQLiteCommand cmd = con.CreateCommand();
                cmd.CommandText = "delete from Tags";
                cmd.ExecuteNonQuery();


                using (SQLiteTransaction txn = con.BeginTransaction())
                {
                    using (SQLiteDataAdapter dbAdapter = new SQLiteDataAdapter("select * from Tags", con))
                    {
                        dbAdapter.InsertCommand = new SQLiteCommandBuilder(dbAdapter).GetInsertCommand(true);
                        insertedRows = dbAdapter.Update(ds, "Tags");
                    }
                    txn.Commit();
                }
            }
            Console.WriteLine("Inserted {0} rows", insertedRows);

            Console.WriteLine("Press any key");
            Console.ReadKey();
        }
    }
}

原始答案:

这是最终被调用的SQLiteDataAdapter ctor的源代码。请注意,不使用命令构建器。您需要在SQLiteDataAdapter上显式设置InserCommand属性,可能是使用SQLiteCommandBuilder吗?

public SQLiteDataAdapter(string commandText, SQLiteConnection connection)
{
    this.SelectCommand = new SQLiteCommand(commandText, connection);
}

答案 1 :(得分:1)

我能够通过调用SetAdded();来检查每一行并通过调用public void InsertData(QuoteDataSet dataSet, String tableName) { int numRowsUpdated = 0; using (SQLiteConnection conn = new SQLiteConnection(_connectionString)) { conn.Open(); using (SQLiteTransaction transaction = conn.BeginTransaction()) using (SQLiteDataAdapter sqliteAdapter = new SQLiteDataAdapter("SELECT * FROM " + tableName, conn)) { using (sqliteAdapter.InsertCommand = new SQLiteCommandBuilder(sqliteAdapter).GetInsertCommand()) { var rows = dataSet.Tags.AsEnumerable(); foreach (var row in rows) { row.SetAdded(); } numRowsUpdated = sqliteAdapter.Update(dataSet, tableName); } transaction.Commit(); } } Console.WriteLine("Num rows updated is " + numRowsUpdated); } 来改变它的状态来解决这个问题...在我这样做之后,Update命令就像魅力一样。

DataSet

我假设当从CSV文件填充Update然后我尝试调用DataAdapter以便将数据插入数据库时​​,行的状态不表示任何变化。我们必须告诉DataSet DataSet发生了变化,因为它所看到的是{{1}}对其填充的CSV文件没有任何变化没有意识到这些是数据库的全新行我试图将数据放入其中。