2012-03-05 96 views
2

嘿,夥計們我正在使用Windows Mobile 5.0(.Net 2.0) ,並且我陷入了一個小問題,我的閱讀器始終爲空。我的聯繫很好,我相信其他一切都很好,唯一的問題是讀者。 在我的異常俘獲它說SqlCeDataReader始終爲空

((System.Data.SqlServerCe.SqlCeException)(e)) : {"The operation completed successfully."} 
InnerException: Could not evaluate expression 

我的數據庫文件未損壞我打開它的應用程序之外,一切都看起來很好。 我的代碼如下:

public static List<Image> GetAll(BOI caller) 
    { 
     List<Image> images = new List<Image>(); 
     SqlCeConnection c = Connection.GetConnection(caller.ConnectionString); 

     if (c != null) 
     { 
      SqlCeCommand command = new SqlCeCommand("SELECT * FROM Images", c); 

      SqlCeDataReader reader = null; 
      try 
      { 
       reader = command.ExecuteReader(); <<<<< reader is null <<<<<<< 
      } 
      catch (Exception e) 
      { 
       while (reader != null && reader.Read()) 
       { 
        Image temp = new Image((int)reader["imageKey"], 
           (String)reader["filename"], 
           (byte[])reader["image"], 
           (reader["labelKey"] == DBNull.Value ? null : (int?)reader["labelKey"]), 
           (int)reader["priority"]); 
        temp.SetDBName(caller.ConnectionString); 

        images.Add(temp); 
       } 
      } 
     } 

     return images; 
    } 

編輯 我打開我的Connection.GetConnection連接(..);

編輯:2 的e.StackTrace:

at System.Data.SqlServerCe.SqlCeDataReader.ProcessResults(Int32 hr) 
    at System.Data.SqlServerCe.SqlCeDataReader.FillMetaData(SqlCeCommand command) 
    at System.Data.SqlServerCe.SqlCeCommand.InitializeDataReader(SqlCeDataReader reader, Int32 resultType) 
    at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) 
    at System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior) 
    at System.Data.SqlServerCe.SqlCeCommand.ExecuteReader() 
    at Oralys.BOI.DataAccess.ImageMapper.GetAll(BOI caller) 
    at Oralys.BOI.BOI.get_Images() 
    at Oralys.BOI.BOI_Controller.FetchAllImages() 
    at IdeoVoiceMobile.RunProfile.InitBOI() 
    at IdeoVoiceMobile.RunProfile..ctor() 
    at IdeoVoiceMobile.Program.startProfile() 
    at IdeoVoiceMobile.Program.Main() 

獲取連接函數:

public static SqlCeConnection GetConnection(string connectionString) 
    { 
     SqlCeConnection conn = null; 
     try 
     { 
      if (connections.Count == 0) 
      { 
       OpenConnection(connectionString); 
      } 
      conn = connections[connectionString]; 
     } 
     catch (System.Exception) 
     { 
     } 
     return conn; 
    } 

編輯:3 異常代碼使用時

SqlCeCommand command = new SqlCeCommand("SELECT * FROM Images Where imageKey=6", c);

ExceptionCode: 0xc0000005 
ExceptionAddress: 0x0115438c 
Reading: 0x00000000 
Faulting module: sqlceqp35.dll 
Offset: 0x0001438c 

    at NativeMethods.GetKeyInfo(IntPtr pIUnknown, 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 behavior, String method, ResultSetOptions options) 
    at SqlCeCommand.ExecuteReader(CommandBehavior behavior) 
    at SqlCeCommand.ExecuteReader() 
    at ImageMapper.GetAll(BOI caller) 
    at BOI.get_Images() 
    at BOI_Controller.FetchAllImages() 
    at RunProfile.InitBOI() 
    at RunProfile..ctor() 
    at Program.startProfile() 
    at Program.Main() 
+2

顯示接下來的幾行代碼會有幫助 – 2012-03-05 16:06:34

+0

我看不到你明確地打開連接?這是在'GetConnection'中嗎? – rrrr 2012-03-05 16:08:19

+0

是要編輯我的問題寫這個。 – raym0nd 2012-03-05 16:08:47

回答

1

問題結束了,因爲我使用System.Data.SqlServerCe 3.5.1.0當我切換到3.5.0.0它工作沒有任何錯誤。 我把while循環放在catch語句之外。

但我仍然從MS惱怒,因爲他們沒有顯示任何錯誤!

+1

這個解決方案具有**所有的功能,可以將循環移到catch外面,並且可以很少或者與更改sql server compact provider的版本無關。 – 2012-03-07 22:50:47

+0

@JoelCoehoorn沒有你的錯。我把這個循環移到catch的外面,它仍然給我一個錯誤。然後我改變了SQL DLL的版本,它工作正常。 – raym0nd 2012-03-08 14:49:17

+0

@ raym0nd:然後你在PC和目標設備上有不匹配的二進制文件。有很多使用3.5.1的使用沒有任何問題。你必須根據正確的版本進行編譯並將相同的版本部署到目標上,否則你會得到奇怪的行爲。 – ctacke 2012-03-08 15:33:01

2

你永遠不會打下面的代碼行,除非cm.executereader拋出一個異常:

while (reader != null && reader.Read()) 

嘗試將下面的代碼catch語句

  while (reader != null && reader.Read()) 
      { 
       Image temp = new Image((int)reader["imageKey"], 
          (String)reader["filename"], 
          (byte[])reader["image"], 
          (reader["labelKey"] == DBNull.Value ? null : (int?)reader["labelKey"]), 
          (int)reader["priority"]); 
       temp.SetDBName(caller.ConnectionString); 

       images.Add(temp); 
      } 
+0

我的問題是讀者總是空! – raym0nd 2012-03-05 16:11:03

+0

我注意到你沒有指定一個命令類型? – Adam 2012-03-05 16:14:54

+0

我必須嗎?我不認爲我需要指定命令類型。 – raym0nd 2012-03-05 16:17:49

0

Raym0nd之外,我猜猜你是新來的。

看看你提供的這個版本。

public static List<Image> GetAll(BOI caller) { 
    List<Image> images = new List<Image>(); 
    using (SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Images", Connection.GetConnection(caller.ConnectionString))) { 
    try { 
     cmd.Connection.Open(); 
     using (SqlCeReader reader = cmd.ExecuteReader()) { 
     while (reader.Read()) { 
      object imageKey = reader["imageKey"]; 
      object filename = reader["filename"]; 
      object image = reader["image"]; 
      object labelKey = reader["labelKey"]; 
      object priority = reader["priority"]; 
      Image img = new Image((interface)imageKey, (string)filename, (byte[])image, (int?)labelKey, (int)priority); 
      img.SetDBName(caller.ConnectionString); 
      images.Add(img); 
     } 
     } 
    } catch (SqlCeException err) { 
     Console.WriteLine(err.Message); 
     throw err; 
    } finally { 
     cmd.Connection.Close(); 
    } 
    } 
    return images; 
} 

你要添加一些功能在你的空整數的情況下,以測試你的對象,但大部分是乾淨和簡單。

Console.WriteLine()上劃一個斷點,如果發生異常,則將鼠標懸停在Message上。

+0

感謝幫助...但問題是因爲我使用System.Data.SqlServerCe版本3.5.1.0我使用3.5.0.0,它的工作就像一個魅力。 – raym0nd 2012-03-07 18:42:38