Reputation: 815
I can't get what is happening to my app. I'm using a SQL CE (.sdf file) to a local database (winform app) and when I debug, everything runs pretty well, I have in the end of it the message that my data was inserted well. But later on, when I check my databse, its empty.
What should I do?
This is my code:
SqlCeConnection conn = new SqlCeConnection(@"Data Source=|DataDirectory|\Database\Livraria.sdf;");
SqlCeCommand cmd = new SqlCeCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"INSERT INTO Livros (Codigo, ISBN, Titulo, Editora, Localizacao, Valor, QTD, Autor, AutorEspiritual, Data_)
VALUES (@Codigo, @ISBN, @Titulo, @Editora, @Localizacao, @Valor, @QTD, @Autor, @AutorEspiritual, @Data_)";
cmd.Parameters.AddWithValue("@Codigo", codigo);
cmd.Parameters.AddWithValue("@ISBN", isbn);
cmd.Parameters.AddWithValue("@Titulo", titulo);
cmd.Parameters.AddWithValue("@Editora", editora);
cmd.Parameters.AddWithValue("@Localizacao", localizacao);
cmd.Parameters.AddWithValue("@Valor", valor);
cmd.Parameters.AddWithValue("@QTD", entradas);
cmd.Parameters.AddWithValue("@Autor", autor);
cmd.Parameters.AddWithValue("@AutorEspiritual", autorEspiritual);
cmd.Parameters.AddWithValue("@Data_", data_);
try
{
conn.Open();
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("Livro adicionado com sucesso!");
}
else
{
MessageBox.Show("O livro não foi adicionado.");
}
// Reset campos
Codigo.Text = "";
Titulo.Text = "";
Editora.SelectedIndex = 0;
Valor.SelectedIndex = 0;
Localizacao.SelectedIndex = 0;
Entrada.Text = "";
Isbn.Text = "";
Autor.SelectedIndex = 0;
AutorEspiritual.SelectedIndex = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
Upvotes: 0
Views: 2086
Reputation: 2204
The first thing you need to do is to create a simple select statement first that gets record from you sqlce database.
if( has record//connected )
{
//Proceed to your insert statement remove if else and use
conn.Open();
cmd.ExecuteNonQuery()
//then check your DB again
}
else
{
//Problem on you connection string
}
Your sqlce database is located on your bin folder
Regards
Upvotes: 0
Reputation: 20640
You have two databases. You are inserting to one and looking for the results in another.
Or, you are starting a transaction, inserting but then not committing.
Upvotes: 3