简体   繁体   中英

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.

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.

I Moved my database out of the |DataDirectory| because I didn't feel confy using the db in Bin\\Debug Folder.... so now everything is fine :-)

because at the debugging time everything is moved to Bin\\Debug folder :-/ didn't knew that :-( but now great, thankx fellas

for more Here is more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM