在 C# 中结果看起来成功,但数据未插入数据库

In C# result looks successful, but data not inserted in database

这几天我一直在摸不着头脑,找不到我做错了什么。我在本地 database.Dataset 中有一个数据库 "compinfo.sdf" 和 table "cname" 作为 "compinfoDataset"。我正在使用此代码插入数据。

static class sqlFunction
{
  static public void insert(String _compName)
  {
    static private SqlCeConnection connection =
        new SqlCeConnection(@"Data Source=|DataDirectory|\compinfo.sdf");
     try
     {
        connection.Open();
        SqlCeCommand commandInsert = new SqlCeCommand("INSERT into [cname]  VALUES (@companyName)", connection);
        commandInsert.Parameters.AddWithValue("@companyName", _compName);
        int result = commandInsert.ExecuteNonQuery();
        MessageBox.Show(result.ToString());
    }
    catch (SqlCeException exception)
    {
            MessageBox.Show(exception.ToString());
    }
    finally
    {
        connection.Close();
    }
  }
}

来自另一个 class 我正在发送数据:

sqlfunction.insert(companyName);

所以当这段代码执行时,我得到的执行结果为1(在消息框中)。但是当我检查数据库 table 中的数据时,它又什么也没有。请帮我找出错误。

很可能 Visual Studio 设置为始终将 sdf 文件复制到 bin 目录,覆盖之前的任何更改。将行为更改为 "Do not copy",如 in this article 所述。