在C#中使用SQLite在数据库中插入项目时出现问题

时间:2016-05-06 01:37:47

标签: c# sqlite insert

我正在尝试使用SQLite在数据库中插入项目,但是当我调用loadfunction时,我收到的错误就像索引一样。我认为问题是当我调用add函数时。我检查了参数值,所有似乎都没问题,但元素没有插入表中。 Bellow你会看到我的表,添加功能和加载功能。

表格:

CREATE TABLE `Fisiere` (
    `Nume`  TEXT,
    `Dimensiune`    INTEGER,
    `Data`  BLOB,
    `Rating_imdb`   REAL,
    `Cale`  TEXT
);

插入功能:

public void addFisier(DirectorVideo[] directors)
        {
            var dbCommand = new SQLiteCommand();
            dbCommand.Connection = _dbConnection;
            dbCommand.CommandText = "insert into Fisiere(Nume, Dimensiune, Data, Rating_imdb, Cale) values(@nume, @dimensiune, @data, @rating_imdb, @cale);";
            try {
                _dbConnection.Open();
                dbCommand.Transaction = _dbConnection.BeginTransaction();
                for (int i = 0; i < directors.Length - 1; i++)
                {
                    for (int j = 0; j < directors[i].nrFisiere; j++)
                    {
                        var numeParam = new SQLiteParameter("@nume");
                        numeParam.Value = directors[i].fisiere[j].numeFisier;
                        var dimensiuneParam = new SQLiteParameter("@dimensiune");
                        dimensiuneParam.Value = directors[i].fisiere[j].dimensiune;
                        var dataParam = new SQLiteParameter("@data");
                        dataParam.Value = directors[i].fisiere[j].data;
                        var ratingParam = new SQLiteParameter("rating_imdb");
                        IMDb rat = new IMDb(directors[i].fisiere[j].numeFisier);
                        ratingParam.Value = rat.Rating;
                        var caleParam = new SQLiteParameter("cale");
                        caleParam.Value = directors[i].cale;

                        Console.WriteLine(numeParam.Value);

                        dbCommand.Parameters.Add(numeParam);
                        dbCommand.Parameters.Add(dimensiuneParam);
                        dbCommand.Parameters.Add(dataParam);
                        dbCommand.Parameters.Add(ratingParam);
                        dbCommand.Parameters.Add(caleParam);



                        Console.WriteLine(caleParam.Value);


                        dbCommand.Transaction.Commit();
                        Console.WriteLine("A fost inserat");

                    }
                }


            }
            catch (Exception)
            {
                Console.WriteLine("muie");
                dbCommand.Transaction.Rollback();
                throw;
            }
            finally
            {
                if (_dbConnection.State != ConnectionState.Closed) _dbConnection.Close();

            }
        }

加载文件功能

public void LoadFiles()
        {
            const string stringSql = "PRAGMA database_list";
            try
            {
                _dbConnection.Open();
                SQLiteCommand sqlCommand = new SQLiteCommand(stringSql, _dbConnection);
                SQLiteDataReader sqlReader = sqlCommand.ExecuteReader();
                try
                {
                    while (sqlReader.Read())
                    {
                        Console.WriteLine("se afiseaza");

                        Console.WriteLine((long)sqlReader["Id_fisier"]);
                        Console.WriteLine((string)sqlReader["Nume"]);
                        Console.WriteLine((long)sqlReader["Dimensiune"]);
                        Console.WriteLine(DateTime.Parse((string)sqlReader["Data"]));
                        Console.WriteLine((long)sqlReader["Rating_imdb"]);
                        Console.WriteLine((string)sqlReader["Cale"]);

                    }
                }
                finally
                {
                    // Always call Close when done reading.
                    sqlReader.Close();
                }
            }
            finally
            {
                if (_dbConnection.State != ConnectionState.Closed) _dbConnection.Close();
            }
        }

1 个答案:

答案 0 :(得分:0)

您需要Commit您的交易。您调用BeginTransaction()但您从未提交过您的事务,因此数据永远不会写入数据库。通过“交易”查看详细信息和工作流程。 https://www.sqlite.org/lang_transaction.html

另一个问题是你的交易和你的命令都乱了。您需要在事务中执行命令,然后提交事务。

    public void addFisier(DirectorVideo[] directors)
        {
            SQLiteTransaction dbTrans;

            try
            {
                _dbConnection.Open();

                // Start Transaction first.
                dbTrans = _dbConnection.BeginTransaction();

                for (int i = 0; i < directors.Length - 1; i++)
                {
                    for (int j = 0; j < directors[i].nrFisiere; j++)
                    {
                        // Create commands that run based on your number of inserts.
                        var dbCommand = new SQLiteCommand();
                        dbCommand.Connection = _dbConnection;
                        dbCommand.CommandText = "insert into Fisiere(Nume, Dimensiune, Data, Rating_imdb, Cale) values(@nume, @dimensiune, @data, @rating_imdb, @cale);";
                        dbCommand.Transaction = dbTrans;

                        var numeParam = new SQLiteParameter("@nume");
                        numeParam.Value = directors[i].fisiere[j].numeFisier;
                        var dimensiuneParam = new SQLiteParameter("@dimensiune");
                        dimensiuneParam.Value = directors[i].fisiere[j].dimensiune;
                        var dataParam = new SQLiteParameter("@data");
                        dataParam.Value = directors[i].fisiere[j].data;
                        var ratingParam = new SQLiteParameter("rating_imdb");
                        IMDb rat = new IMDb(directors[i].fisiere[j].numeFisier);
                        ratingParam.Value = rat;
                        var caleParam = new SQLiteParameter("cale");
                        caleParam.Value = directors[i].cale;

                        Console.WriteLine(numeParam.Value);

                        dbCommand.Parameters.Add(numeParam);
                        dbCommand.Parameters.Add(dimensiuneParam);
                        dbCommand.Parameters.Add(dataParam);
                        dbCommand.Parameters.Add(ratingParam);
                        dbCommand.Parameters.Add(caleParam);

                        Console.WriteLine(caleParam.Value);

                        Console.WriteLine("A fost inserat");

                        // Actually execute the commands.
                        dbCommand.ExecuteNonQuery();
                    }
                }

                // If everything is good, commit the transaction.
                dbTrans.Commit();
            }
            catch (Exception)
            {
                Console.WriteLine("muie");
                dbTrans.Rollback();
                throw;
            }
            finally
            {
                if (_dbConnection.State != ConnectionState.Closed) _dbConnection.Close();

            }
        }

这里也是SQLiteTransaction类文档的一个片段。我建议阅读它:https://www.devart.com/dotconnect/sqlite/docs/Devart.Data.SQLite~Devart.Data.SQLite.SQLiteTransaction.html

public static void RunSQLiteTransaction(string myConnString) { 
    using (SQLiteConnection sqConnection = new SQLiteConnection(myConnString)) { 
        sqConnection.Open(); 
        // Start a local transaction 
        SQLiteTransaction myTrans = sqConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted); 
        SQLiteCommand sqCommand = sqConnection.CreateCommand(); 
        try { 
            sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')"; 
            sqCommand.ExecuteNonQuery(); 
            sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')"; 
            sqCommand.ExecuteNonQuery(); 
            myTrans.Commit(); 
            Console.WriteLine("Both records are written to database."); 
        } 
        catch (Exception e) { 
            myTrans.Rollback(); 
            Console.WriteLine(e.ToString()); 
            Console.WriteLine("Neither record was written to database."); 
        } 
        finally { 
            sqCommand.Dispose(); 
            myTrans.Dispose(); 
        } 
    } 
}