Rubén
Rubén

Reputation: 31

C# SQLite insert does not work

I have a problem with the command INSERT. I compile the application and this works but the row isn't inserted in the database (no throws any error). The database is included in the project and has the property "Copy always". I'm developing a project with WPF and C#, but for the connection and querys, i use C#.

Thanks!

public SQLiteConnection conexion;
        SQLiteCommand cmd;

        public MainWindow()
        {
            InitializeComponent();
            Conectar();
        }

        public void Conectar ()
        {
            try
            {
                //Connection
                conexion = new SQLiteConnection("Data Source=eldelbar.s3db;Version=3;New=True;Compress=True;");
                conexion.Open();

                //Insert query
                string insercion = "insert into cerveza (nombre,precio) values('amstel', 1.3);";
                cmd = new SQLiteCommand(insercion, conexion);
                int cantidad = cmd.ExecuteNonQuery();
                if (cantidad < 1)
                    MessageBox.Show("No se ha podido insertar");

                cmd.Dispose();
                conexion.Close();

            }
            catch (Exception e)
            {
                MessageBox.Show("Error2!" + e.Message);
            }
        }

Upvotes: 0

Views: 572

Answers (1)

Steve
Steve

Reputation: 216353

If you set the property Copy To The Output Directory to Copy Always, everytime you start a debug session in VS, a copy of the database from the project folder is copied to the output directory (usually BIN\DEBUG). You need simply to change that property to Copy If Newer

Upvotes: 1

Related Questions