简体   繁体   English

在具有Windows CE 5的设备上使用SQL Server CE数据库进行CRUD操作

[英]CRUD Opertaions with SQL Server CE database on device with Windows CE 5

There are a smart device, OS of this device is Windows CE 5. I want to write ac# smart device application program that running on this device. 有一个智能设备,此设备的操作系统是Windows CE5。我想编写在此设备上运行的ac#智能设备应用程序。 C# program must communicate with a SQL Server CE database. C#程序必须与SQL Server CE数据库进行通信。

cedb1.sdf is a SQL Server CE database that are creating on device when program will be run, I call below method on FormLoad(): 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);
}

Database is successfully created. 数据库创建成功。

Then on FormLoad() I call RefreshGrid() method to show all PersonelTypes in data grid: 然后在FormLoad()我叫RefreshGrid()方法来显示所有PersonelTypes在数据网格:

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

PersonelType is a businness object class: PersonelType是的businness对象类:

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

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

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;
}

and corresponding method of GetAll() in BLL is GetAll() in DAL: 和相应的方法GetAll()中BLL是GetAll()在DAL:

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

the ExecuteDataTable method implemented in this way: 以这种方式实现的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 property is: 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);
    }
}

An exception occurred, message of exception is: 发生异常,异常消息为:

Error, a native exception has occurred in SDPOffDbPersonel.exe 错误,SDPOffDbPersonel.exe中发生了本机异常

an in details of this exception write: 有关此异常的详细信息,请写:

ExceptionCode: 0xc0000005 异常代码:0xc0000005
ExceptionAddress: 0x01ca4008 异常地址:0x01ca4008
Reading: 0x00650094 阅读:0x00650094
Faulting Mode: sqlceme35.dll 故障模式:sqlceme35.dll
offset: 0x00004008 偏移量:0x00004008

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

What is problem? 怎么了 And how can I solve it? 我该如何解决呢?

regards 问候

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM