I am trying to insert elements in a database using SQLite in C#, but i always received error:
Object reference not set to an instance of an object.
The function is working only if a delete the query which is checking if an elements exits in the table. The code:
public void addFisier(DirectorVideo[] directors)
{
var dbCommand = new SQLiteCommand();
dbCommand.Connection = _dbConnection;
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++)
{
dbCommand.CommandText = ("SELECT count(*) FROM Fisiere WHERE Nume=' " + directors[i].fisiere[j].numeFisier + "'");
int numara = Convert.ToInt32(dbCommand.ExecuteScalar());
if (numara == 0)
{
dbCommand.CommandText = "insert into Fisiere(Nume, Dimensiune, Data, Rating_imdb, Cale) values(@nume, @dimensiune, @data, @rating_imdb, @cale)";
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);
Console.WriteLine(dimensiuneParam.Value);
Console.WriteLine(dataParam.Value);
Console.WriteLine(ratingParam.Value);
Console.WriteLine(caleParam.Value);
dbCommand.Parameters.Add(numeParam);
dbCommand.Parameters.Add(dimensiuneParam);
dbCommand.Parameters.Add(dataParam);
dbCommand.Parameters.Add(ratingParam);
dbCommand.Parameters.Add(caleParam);
dbCommand.ExecuteNonQuery();
dbCommand.Transaction.Commit();
}
else Console.WriteLine("Exista");
}
}
}
catch (Exception)
{
Console.WriteLine("muie");
dbCommand.Transaction.Rollback();
throw;
}
finally
{
if (_dbConnection.State != ConnectionState.Closed) _dbConnection.Close();
}
}
Aucun commentaire:
Enregistrer un commentaire