有一个智能设备,此设备的操作系统是Windows CE 5.我想编写一个在此设备上运行的c#智能设备应用程序。 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; }
}
BLL中的 RefreshGrid()
方法调用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;
}
和BLL中GetAll()
的对应方法在DAL中为GetAll()
:
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中发生了本机异常
详细说明此异常写:
ExceptionCode:0xc0000005
异常地址:0x01ca4008
阅读:0x00650094
错误模式:sqlceme35.dll
偏移量:0x00004008在SqlCeDataReader.FillMetaData(SqlCeCommand命令)的NativeMethods.GetKeyInfo(IntPtr pTx,String 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 [] datatables,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand命令,CommandBehavior行为)
在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(布尔fVis)
在Control.set_Visible(布尔值)
在Application.Run(Form frm)
在Program.Main()
有什么问题?我该如何解决?
问候
答案 0 :(得分: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