试图读取写保护的内存。这通常表明其他内存已损坏

时间:2013-08-31 11:42:22

标签: c# sql-server-ce

我使用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

在它运行时部署它而没有任何错误,当我在客户端计算机上安装它时,它会在database.sdf中准确插入并检索数据以便自动完成。

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

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

注意:如果我在另一台安装了VS 2008的电脑上安装此设置,它将正常工作,没有任何错误。我是否必须在客户端PC上安装一些东西?

我也尝试建立 在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();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

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

SQL CE不是非常友好的线程。