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

*爱你&永不变心* 提交于 2019-12-13 04:38:54

问题


I have developed a desktop application using C# and sql server compact 3.5

I have import all necessary dll's

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

And deploy it while it is running without any error on this PC when i install setup on client machine it will do insertion accurately in database.sdf and also retrieve data for auto-complete.

When I want to retrieve data from it to fill combo box or grid it will generate error

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

Note: if I install this setup on another pc which have VS 2008 on it will work fine without any error. Will I have to install something on the Client Pc?

I also try to build in VS2008:

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

but result is same.

Here is a class that I used for store and retrive data from 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:


From my experience, this happens when you access the same SqlConnection from different threads.

SQL CE is not very thread-friendly.



来源:https://stackoverflow.com/questions/18547759/attempted-to-read-write-protected-memory-this-is-often-an-indication-that-other

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!