0

I'm trying to put anything into my DB. Message Box is showing that I did put 1 row to DB, but when I check it - nothing happened.

Anybody know the cure?

SqlCeConnection cn = new SqlCeConnection(@"Data Source = Database1.sdf");
try
{
    cn.Open();
}
catch (SqlCeException ex)
{
    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
    Application.ExitThread();
}

SqlCeCommand cm = new SqlCeCommand("INSERT INTO Sentences (Sentence) VALUES ('Sentence')", cn);
//cm.Parameters.AddWithValue("@Sentence", "Any sentence");
try
{
    int rowsAffected = cm.ExecuteNonQuery();
    MessageBox.Show((rowsAffected.ToString()));
}
catch (Exception)
{
    MessageBox.Show("Insertion Failed");
}
cn.Close();
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Don't use tags in Title !! – huMpty duMpty Mar 25 '14 at 18:15
  • Are you committing the transaction anywhere? http://stackoverflow.com/questions/2912112/c-sharp-rollback-sqltransaction-in-catch-block-problem-with-object-accessabi – rie819 Mar 25 '14 at 18:20
  • Are you sure? Close and then re-open the table. – Olivier Jacot-Descombes Mar 25 '14 at 18:20
  • 2
    What do you mean "when I check it?" How are you checking it? – Craig W. Mar 25 '14 at 18:21
  • I don't want to use transactions, because I don't need to. I used http://www.youtube.com/watch?v=blNb-9vhFLM and for this guy it worked. I checked it in MS Visual Studio. - Right Click on my table -> "Show Table Data" – user1798039 Mar 25 '14 at 18:30
  • `"Data Source = Database1.sdf"` is probably wrong; there is no path so you have no idea what file it is using for the database. You probably want `"Data Source=|DataDirectory|Database1.sdf"` but without more information this is a total guess. – Dour High Arch Mar 25 '14 at 18:42

2 Answers2

0

Try some nice and easy code.

using (SqlCeConnection con = new SqlCeConnection(strConn))
{
   try
   {
        con.Open();
        using (SqlCeCommand cmd = new SqlCeCommand("insert into Table (column1,Column2) values (@Val1, @val2)", con))
        {
            cmd.Parameters.AddWithValue("@Val1", value1);
            cmd.Parameters.AddWithValue("@Val2", value2);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        con.Close();
    } catch (Exception ex){
        //handle any exception here
    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Ok, I changed it, and nothing happened... But if I'll use "New Query" In my Visual Studio and put any data there it works... Any clues? – user1798039 Mar 25 '14 at 18:28
0

suggest you to use a full path to your database file in your connection string.

complete example like =

@"Data Source=C:\Users\MYPC\Documents\Visual Studio 2010\Projects\MyProjectFolder\MyProject-1\bin\Debug\MyDatabase.sdf;Persist Security Info=False;"

Well explained here !!

Hardik Vinzava
  • 968
  • 10
  • 22
  • That is not going to work if anyone else runs your code, if it runs on any other machine, on any other version of Visual Studio, or from anywhere else than the debugger. You should be using environment variables or deployment properties. – Dour High Arch Mar 25 '14 at 19:30
  • @user1798039 please mark as answer if its solved your problem. – Hardik Vinzava Mar 27 '14 at 05:01