2013-06-03 29 views
0

有一個智能設備,該設備的操作系統是Windows CE 5.我想寫一個運行在此設備上的c#智能設備應用程序。 C#程序必須與SQL Server CE數據庫進行通信。帶有Windows CE的設備上的SQL Server CE數據庫的CRUD操作系統5

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()方法調用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; 
} 

和在BLL GetAll()相應的方法是GetAll()在DAL:

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

例外

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

以這種方式實現的ExecuteDataTable方法發生異常的信息是:

錯誤,一機異常發生在SDPOffDbPersonel.exe

一個在這個異常寫的細節:

ExceptionCode:0000005
ExceptionAddress:0x01ca4008
閱讀:0x00650094
錯誤模式:sqlceme35.dll
偏移量:0x00004008

在NativeMethods.GetKeyInfo(IntPtr的PTX,字符串pwszBaseTable,IntPtr的PrgDbKeyInfo,的Int32 cDbKeyInfo,IntPtr的PERROR)在SqlCeDataReader.FillMetaData(SqlCeCommand命令)
在SqlCeCommand.InitializeDataReader(SqlCeDataReader讀取器,的Int32與resultType)
在SqlCeCommand。 ExecuteCommand(的CommandBehavior行爲,字符串方法,ResultSetOptions選項)
在SqlCeCommand.ExecuteDbDataReader(的CommandBehavior行爲)
在DbCommand.System.Data.IDbCommand.ExecuteReader(的CommandBehavior行爲)
在DbDataAdapter的。FillInternal(DataSet數據集,DataTable []數據表,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand命令,CommandBehavior行爲)
at DbDataAdapter.Fill(DataTable [] datatables,Int32 startRecord,Int32 maxRecords,IDbCommand command,CommandBehavior behavior)
在DbDataAdapter.Fill(數據表的dataTable)
在DALBase.ExecuteDataTable(字符串的CommandText)
在PersonelTypeDAL.GetAll()
在PersonelTypeBLL.GetAll()
在Form1.RefreshGrid()
在Form1.Form1_Load( Object sender,EventArgs e)
at Form.OnL OAD(EventArgs五)
在Form._SetVisibleNotify(布爾fVis)
在Control.set_Visible(布爾值)
在Application.Run(表格FRM)
在Program.Main()

什麼是問題嗎?我該如何解決它?

問候

+0

你檢查,如果ExecutingAssembly返回值....是正確的? – Steve

+0

是的,這是正確的 –

回答

1

看起來是與System.Data.SqlServerCe.dll文件和設備上的非託管的DLL文件之間的版本不匹配的問題 - http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/fd60ba69-e4d6-441a-901f-947ac7a46d3c/ - 解決方案,以確保相同的版本在開發中使用環境和設備,perferably SSCE 3.5 SP2 - http://www.microsoft.com/en-us/download/details.aspx?id=8831

+0

謝謝你,我使用SSCE 3.5 SP1,以及開發我使用VS 2008,我從上面的鏈接下載SSCE 3.5 SP2和仍然問題是在他的位置。 –

+0

並且您不包括System.Data.SqlServerCe.dll與您的項目? – ErikEJ

+0

我參考這個組件,並將其複製到設備上運行的應用程序文件 –