为什么我不能将记录插入到我的SQL Compact 3.5数据库中?

时间:2010-10-10 06:40:51

标签: c# sql-server sql-server-ce

我刚刚使用MSDN的文档制作了一个非常简单的测试应用程序。我想要做的就是在我的VS 2010应用程序的SQL Server Compact数据库中的表中插入一条记录。

但是当我运行它时,它表现得很好(没有错误),但是当我从Server Explorer查看表数据时,记录永远不会插入到我的SQL Compact 3.5数据库中。

我的代码如下:

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();
        }

    }
}

}

我有一个名为test.sdf的数据库。它包含一个表“test”,其中包含1列“test”。

我的示例测试应用程序位于:http://dl.dropbox.com/u/3051071/ConsoleApplication1.zip

我已经测试了与test.sdf数据库的数据库连接,并且测试正常。

我无法想象为什么我无法在我的桌子中插入记录。每当我在执行后查看我的数据时,我的表中的值总是为空。

有人可以告诉我这里的错误吗?

感谢。

2 个答案:

答案 0 :(得分:8)

您在SSMS中查看哪个.sdf文件?我刚试过你的应用程序 - 检查bin \ Debug文件夹中.sdf中测试表的内容 - 它看起来对我来说。

答案 1 :(得分:0)

我将数据库移出| DataDirectory |因为我在Bin \ Debug Folder中使用数据库感觉不舒服....所以现在一切都很好: - )

因为在调试时,所有内容都被移动到Bin \ Debug文件夹: - /不知道:-(但现在好了,谢谢fellas

了解更多Here is more info