Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I insert a record into my SQL Compact 3.5 database?

I just made a very simple test app using documentation from MSDN. All I want to do is insert a record into my table which is in a SQL Server Compact database in my VS 2010 app.

But when I run it, it acts like it executes fine (no errors), but a record is never inserted into my SQL Compact 3.5 database when I go to view the table data from Server Explorer.

My code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        SqlCeConnection conn = null;

        try
        {
            conn = new SqlCeConnection("Data Source = test.sdf; Password ='pass'");
            conn.Open();
            SqlCeCommand cmd = conn.CreateCommand();
            cmd.CommandText = "INSERT INTO TEST ([test]) Values('NWIND')";

            cmd.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }

    }
}

}

I have a database named test.sdf. It contains a table "test" with 1 column "test".

My sample test app is available at: http://dl.dropbox.com/u/3051071/ConsoleApplication1.zip

I have tested the database connection to my test.sdf database and that tests fine.

I can not figure for the life of my why I am unable to insert a record into my table. Whenever I view my data after execution, the value is always null in my table.

Can someone please tell me what I am doing wrong here?

Thanks.

like image 811
fraXis Avatar asked May 17 '26 04:05

fraXis


1 Answers

Which .sdf file are you viewing in SSMS? I've just tried your app - check the content of the test table in the .sdf in the bin\Debug folder - it looks right to me.

like image 112
Will A Avatar answered May 19 '26 18:05

Will A