3

I am a beginner in SQLite. Now I am trying to insert new record in sample northwind database. My C# Code is below:

       SQLiteConnection myConnection = new SQLiteConnection();

        myConnection.ConnectionString = @"Data Source=|DataDirectory|\northwindEF.db";

        using (myConnection)
        {
            SQLiteCommand myCommand = myConnection.CreateCommand();
            myCommand.CommandText = "INSERT INTO Categories(CategoryID,CategoryName,Description) VALUES(11,'Bakery','Baked goods such as bread and cakes')";
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }

When i run this program, i didn't get any error. But my new record is not inserted into table. What wrong with my code. Please help me.

6
  • I am not sure but if CategoryId is primary key, change your query like this : "INSERT INTO Categories(CategoryName,Description) VALUES('Bakery','Baked goods such as bread and cakes')" Commented Mar 20, 2013 at 22:13
  • CategoryID is primary key. I change my query as you said, but the problem still occur. :-( Commented Mar 20, 2013 at 22:23
  • SQLiteCommand may require the CommandText property be terminated with a semicolon. Commented Mar 20, 2013 at 22:24
  • semicolon can't solve my problem. How do I? :-( Commented Mar 20, 2013 at 22:27
  • Do you have the database file listed in your project files? If is listed there, what is the value for the property 'Copy to the output directory' ? and what is your connection string? Commented Mar 20, 2013 at 22:53

3 Answers 3

5

It is a common pitfall. A connection in server Explorer is build to the database file stored in the project folder. Then, at Runtime the database is copied to the output directory and every insert, update, delete works against that database. In Server Explorer nothing changes because the database is different. The same happens also if the property 'Copy to the output directory' is set to Copy always. In this case at every run the database in the output directory is overwritten with a fresh copy taken from the project root folder and again every delete, update or insert disappears at the next run.

Sign up to request clarification or add additional context in comments.

Comments

0

Why do you think the "new record is not inserted"? ExecuteNonQuery will tell you how many records were affected; what does it return?

How are you running the program? From the debugger? DataDirectory is the folder the data was installed to. If you have not changed something in your .sdf file project properties, Visual Studio installs data to the project\debug\bin\ folder. Look for an .sdf file there, is your "new record" there?

Comments

0

try coding like this, it is always check if connection has an error or your query has an error.

using (SQLiteConnection con = new SQLiteConnection { ConnectionString = "connectionstring" })
{
     using(SQLiteCommand cmd = new SQLiteCommand { Connection = con })
     {
         // check connection state if open then close, else close proceed
         if(con.State == ConnectionState.Open)
           con.Close();

         //then
         try
         {
            // try connection to Open
            con.Open();
         }
         catch
         {
            // catch connection error.
         }

         try
         {
             // your insert query
         }
         catch
         {
             // catch query error
         }

     } // Close Command

}  // Close Connection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.