0

我使用 C# 和 sql server compact 3.5 开发了一个桌面应用程序

我已经导入了所有必要的 dll

  • System.Data.SqlServerCe.dll
  • sqlceca35.dll
  • sqlcecompact35.dll
  • sqlceer35EN.dll
  • sqlceme35.dll
  • sqlceoledb35.dll
  • sqlceqp35.dll
  • sqlcese35.dll

当我在客户端机器上安装安装程序时,它在这台 PC 上运行且没有任何错误时部署它,它将在 database.sdf 中准确插入,并检索数据以自动完成。

当我想从中检索数据以填充组合框或网格时,它将产生错误

attempted to read write protected memory. 
this is often an indication that other memory is corrupt

注意:如果我将此设置安装在另一台装有 VS 2008 的电脑上,它将正常工作而不会出现任何错误。我必须在客户端电脑上安装一些东西吗?

我也尝试在 VS2008 中构建:

 Tools->Options
 Debugging->General
 uncheck option "Suppress JIT optimization on module load"

但结果是一样的。

这是我用于从 db 存储和检索数据的类

class dataBase
{

    private SqlCeDataAdapter ad;
    private SqlCeCommand cmd;
    private string StringdbFileName=("Data Source=" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\sp.sdf").Replace(@"file:\", "");
    private SqlCeConnection coon;

    public dataBase()
    {
        coon = new SqlCeConnection(StringdbFileName);
        coon.Close();
    }

    public int ExecuteSQL(string Query)
    {
        try
        {
            coon.Open();
            cmd = new SqlCeCommand();
            cmd.Connection = this.coon;
            cmd.CommandText = Query;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            coon.Close();

        }
    }

    public DataTable GetDataTable(string Query)
    {
        try
        {
            coon.Open();
            DataTable dt = new DataTable();
            cmd = new SqlCeCommand();
            cmd.CommandText = Query;
            ad = new SqlCeDataAdapter(Query,coon);
            ad.Fill(dt);

            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            coon.Close();
        }
    }

    public void FillComboBox(string Query, string DisplayMember,string ValueMember, ComboBox cmb)
    {
        try
        {
            coon.Open();

            DataTable dt = new DataTable();
            cmd = new SqlCeCommand();
            cmd.CommandText = Query;
            ad = new SqlCeDataAdapter(Query, coon);
            ad.Fill(dt);

            cmb.DataSource = dt;
            cmb.DisplayMember = DisplayMember;
            cmb.ValueMember = ValueMember;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            coon.Close();
        }
    }
}
4

1 回答 1

3

根据我的经验,当您从不同的线程访问相同的 SqlConnection 时会发生这种情况。

SQL CE 对线程不是很友好。

于 2013-08-31T11:50:53.243 回答