So i have this "database is locked" exception since yesterday. Tried everything i found here, yet still nothing. I'm using two forms - simple login form and then another one, where user can add new product to database. Checking if product already exist in db works, but adding new one throws that exception. Check it out please:
try
{
using (SqliteConnection db = new SqliteConnection("Filename=Magazyn.sqlite"))
{
db.Open();
string SQLcheck = "select * from Administrator";
using (SqliteCommand cmd = new SqliteCommand(SQLcheck, db))
{
using (SqliteDataReader reader = cmd.ExecuteReader())
{
var count = 0;
while (reader.Read())
{
count = count + 1;
}
if (count > 0 && textBox1 != null && !string.IsNullOrWhiteSpace(textBox1.Text))
{
string sql = "select * from Administrator WHERE Name='" + textBox1.Text +
"' AND Password ='" + textBox2.Text + "'";
using (SqliteCommand command = new SqliteCommand(sql, db))
{
using (SqliteDataReader rdr = command.ExecuteReader())
{
if (rdr.Read())
{
MessageBox.Show(textBox1.Text + ", zostałeś pomyślnie zalogowany", "Logowanie");
AddProduct a = new AddProduct();
a.ShowDialog();
this.Close();
return;
}
else
{
MessageBox.Show("Nie ma administratora o loginie " + textBox1.Text +" lub Twoje hasło jest niepoprawne", "Błąd logowania");
textBox1.Clear();
textBox2.Clear();
}
}
}
}
else if (count == 0)
{
MessageBox.Show(
"W systemie nie istnieje konto administratora - nastąpi przekierowanie do formularza rejestracyjnego",
"Pierwsze logowania - konieczna rejestracja");
AddAdmin a = new AddAdmin();
a.ShowDialog();
return;
}
}
}
db.Close();
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
And 2nd Form:
try
{
using (SqliteConnection db = new SqliteConnection("Filename = Magazyn.sqlite"))
{
db.Open();
string sqlCheck = "select * from Produkty WHERE RFID='" + RFID.Text + "'";
using (SqliteCommand cmd = new SqliteCommand(sqlCheck, db))
{
using (SqliteDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
MessageBox.Show("W bazie produktów jest już produkt o podanym tagu RFID", "Wykryto duplikat");
RFID.Clear();
}
else
{
reader.Close();
reader.Dispose();
string sql = "INSERT INTO Produkty (Name, RFID, Price, Unit, VAT) values ('" + Nazwa.Text + "','" +
RFID.Text + "','" + Cena.Text + "','" + Jednostka.Text + "','" + VATcat + "');";
//string sql = @"INSERT INTO Produkty (Name, RFID, Price, Unit, VAT) values (@name, @RFID, @price, @unit, @vat)";
using (SqliteCommand command = new SqliteCommand(sql, db))
{
using (SqliteDataReader rdr = command.ExecuteReader())
{
MessageBox.Show("Pomyślnie dodano produkt " + Nazwa.Text + " do bazy danych", "Dodano produkt");
}
/* command.CommandText = sql;
command.Connection = db;
command.Parameters.Add(new SqliteParameter("@name", Nazwa.Text));
command.Parameters.Add(new SqliteParameter("@RFID", RFID.Text));
command.Parameters.Add(new SqliteParameter("@price", Cena.Text));
command.Parameters.Add(new SqliteParameter("@unit", Jednostka.Text));
command.Parameters.Add(new SqliteParameter("@vat", VATcat));
command.ExecuteNonQuery();
MessageBox.Show("Pomyślnie dodano produkt " + Nazwa.Text + " do bazy danych", "Dodano produkt");
*/
}
}
}
}
db.Close();
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
I commented another way of adding to db (Will test both when I get rid of that exception) Funny thing that if i change INSERT to SELECT it's working. If I use INSERT query directly in database file it's working too.