jeudi 5 mai 2016

Problems inserting items in a database using SQLite in C#

i am trying to insert items in a database using SQLite but when i call loadfunction i receive an error like out of index. I think the problem is when i call add function. I checked parameters values and all seems to be ok, but the elements are not inserted in the table. Bellow you will see my table, add function and load function.

The table:

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

The insert function:

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;
                        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");

                    }
                }


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

            }
        }

Load file function

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();
            }
        }

Aucun commentaire:

Enregistrer un commentaire