Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 binDebug folder - it looks right to me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...