SqlCeDataReader始终为null

时间:2012-03-05 16:05:17

标签: c# .net sql windows-mobile sql-server-ce

Hey Guys我正在使用Windows Mobile 5.0(.Net 2.0) 而且我遇到了一个小问题,我的读者永远都是空的。我的联系很好,我相信其他一切都很好,唯一的问题是读者。 在我的例外中,抓住它说

((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()

3 个答案:

答案 0 :(得分: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);
            }

答案 1 :(得分:1)

问题最终是因为我在切换到System.Data.SqlServerCe 3.5.1.0时使用3.5.0.0它没有任何错误。 我将while循环放在catch语句之外。

但我仍然对MS感到恼火,因为他们没有表现出任何错误!

答案 2 :(得分: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上。