0

有一个智能设备,这个设备的操作系统是Windows CE 5。我想写一个ac#智能设备应用程序,运行在这个设备上。C# 程序必须与 SQL Server CE 数据库通信。

cedb1.sdf是在程序运行时在设备上创建的 SQL Server CE 数据库,我在 FormLoad() 上调用以下方法:

public void InitializeDatabase()
{
    string startupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
    string datalogicFilePath = Path.Combine(startupPath, "cedb1.sdf");

    SqlCeEngine engine = new SqlCeEngine(ConnectionString);

    if(!File.Exists(datalogicFilePath))
        engine.CreateDatabase();
    string query = "create table PersonelType(Id int primary key identity not null,Caption nvarchar(100) null)";
        ExecuteNonQuery(query);

    query = "create table Personel(Id int primary key identity not null,PersonelTypeId int not null,FirstName nvarchar(100) not null,LastName nvarchar(100) not null,CardNumber nvarchar(100) null)";
        ExecuteNonQuery(query);

    query = @"ALTER TABLE Personel
                ADD CONSTRAINT MyConstraint FOREIGN KEY (PersonelTypeId) REFERENCES
                PersonelType(Id)
                ON UPDATE CASCADE
                on delete cascade";
    ExecuteNonQuery(query);
}

数据库创建成功。

然后FormLoad()我调用RefreshGrid()方法PersonelTypes在数据网格中显示所有内容:

private void RefreshGrid()
{
    PersonelTypeBLL personelTypeManager = new PersonelTypeBLL();
    dgPersonelTypes.DataSource = personelTypeManager.GetAll();
}

PersonelType是一个业务对象类:

public class PersonelType
{
    public int Id { get; set; }
    public string Caption { get; set; }
}

RefreshGrid()方法调用GetAll()BLL 中的方法:

public List<PersonelType> GetAll()
{
    var repository = new PersonelTypeDAL();
    var data = repository.GetAll();
    List<PersonelType> personelTypes = new List<PersonelType>();

    for (int i = 0; i < data.Rows.Count; i++)
    {
        PersonelType personelType = new PersonelType();
        personelType.Id = Convert.ToInt32(data.Rows[i]["Id"]);
        personelType.Caption = data.Rows[i]["Caption"].ToString();
        personelTypes.Add(personelType);
    }
    return personelTypes;
}

GetAll()BLL中对应的方法GetAll()在DAL中:

public DataTable GetAll()
{
    string query = "select id, caption from personeltype";
    return ExecuteDataTable(query);
}

ExecuteDataTable这种方式实现的方法:

protected DataTable ExecuteDataTable(string commandText)
{
    using (SqlCeConnection con = new SqlCeConnection(ConnectionString))
    {
        SqlCeCommand cmd = new SqlCeCommand();
        cmd.Connection = con;
        cmd.CommandText = commandText;
        DataTable dt = new DataTable();
        SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
        con.Open();
        da.Fill(dt);
        con.Close();
        return dt;
    }
}

ConnectionString属性是:

protected string ConnectionString
{
    get
    {
        string startupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        string datalogicFilePath = Path.Combine(startupPath, "cedb1.sdf");
        return string.Format("DataSource={0};password=123456", datalogicFilePath);
    }
}

发生异常,异常消息是:

错误,SDPOffDbPersonel.exe 中发生本机异常

此异常的详细信息如下:

异常代码:0xc0000005
异常地址:0x01ca4008
读取:0x00650094 故障
模式:sqlceme35.dll
偏移量: 0x00004008

在 NativeMethods.GetKeyInfo(IntPtr pTx, String pwszBaseTable, IntPtr PrgDbKeyInfo, Int32 cDbKeyInfo, IntPtr pError) at SqlCeDataReader.FillMetaData(SqlCeCommand command)
at SqlCeCommand.InitializeDataReader(SqlCeDataReader reader, Int32 resultType)
at SqlCeCommand.ExecuteCommand(CommandBehavior, String method, ResultSetOptions options)
at SqlCeCommand.ExecuteDbDataReader(CommandBehavior behavior)
at DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command,命令行为行为)
在 DbDataAdapter.Fill(DataTable[] datatables, Int32 startRecord, Int32 maxRecords, IDbCommand 命令, CommandBehavior 行为)
在 DbDataAdapter.Fill(DataTable dataTable)
在 DALBase.ExecuteDataTable(String commandText) 在 PersonelTypeDAL.GetAll(
)在 PersonelTypeBLL.GetAll
()
在 Form1.RefreshGrid()
在 Form1.Form1_Load(Object sender, EventArgs e)
在 Form.OnLoad(EventArgs e)
在 Form._SetVisibleNotify(Boolean fVis)
在 Control.set_Visible(布尔值)
在 Application.Run(Form frm)
在程序.Main()

什么是问题?我该如何解决?

问候

4

1 回答 1

1

看起来这是 System.Data.SqlServerCe.dll 文件和设备上的非托管 dll 文件之间版本不匹配的问题 - http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/ fd60ba69-e4d6-441a-901f-947ac7a46d3c/ - 解决方案是确保在开发环境和设备中使用相同的版本,最好是 SSCE 3.5 SP2 - http://www.microsoft.com/en-us/download/details.aspx ?id=8831

于 2013-06-03T07:54:59.133 回答