繁体   English   中英

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

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

有一个智能设备,此设备的操作系统是Windows CE5。我想编写在此设备上运行的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是的businness对象类:

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

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

和相应的方法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

在SqlCeDataReader.FillMetaData(SqlCeCommand命令)处的NativeMethods.GetKeyInfo(IntPtr pTx,字符串pwszBaseTable,IntPtr PrgDbKeyInfo,Int32 cDbKeyInfo,IntPtr pError)
在SqlCeCommand.InitializeDataReader(SqlCeDataReader reader,Int32 resultType)
在SqlCeCommand.ExecuteCommand处(CommandBehavior行为,String方法,ResultSetOptions选项)
在SqlCeCommand.ExecuteDbDataReader(CommandBehavior行为)
在DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior行为)
在DbDataAdapter.FillInternal(DataSet数据集,DataTable []数据表,Int32 startRecord,Int32 maxRecords,字符串srcTable,IDbCommand命令,CommandBehavior行为)
在DbDataAdapter.Fill(DataTable []数据表,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)
在Program.Main()

怎么了 我该如何解决呢?

问候

看起来这是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