2012-03-05 9 views
2

Hey Guys私はWindows Mobile 5.0(.Net 2.0) で作業していますが、私の読者が常にnullになっている小さな問題に悩まされています。私の接続は正常ですし、私は他のすべてがうまくいくと信じています。唯一の問題は読者にあります。それはSqlCeDataReaderは常にnullです

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

EDIT 私はConnection.GetConnection(..)での私の接続を開きます。

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

EDIT: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

次の数行のコードを表示すると便利です –

+0

明示的に接続を開くことができません。それは 'GetConnection'ですか? – rrrr

+0

私はそれを書くために私の質問を編集しようとしていました。 – raym0nd

答えて

1

3.5.0.0に切り替えたときに私がSystem.Data.SqlServerCe 3.5.1.0を使用していたために問題が終了しましたが、エラーなしで機能しました。 そしてcatch文の外側にwhileループを置いています。

しかし、私はまだ彼らが何かエラーを示していないので、私はMSから迷惑をかけています!

+1

このソリューションには、ループをキャッチの外に移動させるための**すべての**と、SQL Serverのコンパクトプロバイダのバージョンを変更することとはほとんど関係がありません。 –

+0

@JoelCoehoorn間違いありません。私はキャッチの外にループを移動し、それでも私にエラーを与えた。その後、私はSQLのDLLのバージョンを変更し、それは正常に働いた。 – raym0nd

+0

@ raym0nd:次に、PCとターゲットデバイスでバイナリが一致しませんでした。何も問題なく3.5.1を使用して多くの使用があります。適切なバージョンに対してコンパイルし、同じバージョンをターゲットにデプロイする必要があります。そうしないと、奇妙な動作が発生します。 – ctacke

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

私の問題は、読者が常にnullであることです! – raym0nd

+0

コマンドタイプを指定していないことがわかりましたか? – Adam

+0

私はする必要がありますか?私はコマンドの種類を指定する必要はないと思う。 – raym0nd

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

あなたがNULL可能で、整数の場合には、あなたのオブジェクトをテストするためにいくつかの機能を追加したいと思うでしょうが、ほとんどの部分はクリーンでシンプルです。

例外が発生した場合は、Console.WriteLine()にブレークポイントを置き、にマウスを合わせます。

+0

ありがとうございました...しかし、問題は、System.Data.SqlServerCeバージョン3.5.1.0を使用していたためです。私は3.5.0.0を使用しましたが、それは魅力的でした。 – raym0nd

関連する問題