关于c#:SqlCeDataReader始终为null

SqlCeDataReader is always null

嗨,我正在使用Windows Mobile 5.0(.Net 2.0)
而且我遇到了一个小问题,我的读者总是空的。我的关系很好,我相信其他一切都很好,唯一的问题是读者。
在我的例外情况下,它显示

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

我的数据库文件未损坏,我在应用程序外部打开了它,一切看起来都很好。
我的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  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:

1
2
3
4
5
6
7
8
9
10
11
12
13
   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()

获取连接功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    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);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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()


除非cm.executereader引发异常,否则您永远不会触及以下代码行:

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

尝试将以下代码移到catch语句之外

1
2
3
4
5
6
7
8
9
10
11
            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);
            }


问题最终归因于当我切换到3.5.0.0时使用的是System.Data.SqlServerCe 3.5.1.0,它可以正常工作而没有任何错误。
然后将while循环放在catch语句之外。

但是我仍然对MS感到恼火,因为他们没有对此显示任何错误!


Raym0nd,我想你是这个新人。

看看您提供的这个版本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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上。