简体   繁体   English

在C#中返回一个数据读取器

[英]return a datareader in c#

I'm trying to program a method to return a SQLiteDataReader object but without any success. 我正在尝试编写一种方法来返回SQLiteDataReader对象,但没有成功。

Here's the method's source code: 这是该方法的源代码:

In a class file (DBUtils.cs): 在类文件(DBUtils.cs)中:

public static SQLiteDataReader getAction(string dbPath, byte actionLevel)
{
    SQLiteConnection m_dbConnection;
    SQLiteDataReader reader = null;

    m_dbConnection = new SQLiteConnection("Data Source=" + dbPath + ";Version=3;FailIfMissing=True");
    m_dbConnection.Open();

    string sql = "SELECT * FROM Actions WHERE acLevel=" + actionLevel + " ORDER BY RANDOM() LIMIT 1";
    SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);

    reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

    return reader;
}

In the MainWindow: 在主窗口中:

public MainWindow()
{
    InitializeComponent();

    const string dbPath = "../../TestDatabase.sqlite";

    SQLiteDataReader zReader = DBUtils.getAction(dbPath, 1);

    MessageBox.Show(Convert.ToString(zReader["acText"]));

}

Now, when I run step by step I do see that data has been loaded from the database when I'm in the getAction() method but when it comes back to the message box I get an Illegal Exception because there's no current row in the DataReader. 现在,当我逐步运行时,确实看到在getAction()方法中时已从数据库加载数据,但是当它返回到消息框时,我得到了一个非法异常,因为该行中没有当前行DataReader。

Anybody have a clue about what's happening? 有人知道发生了什么吗?

Thanks 谢谢

You need to finish the reading process: 您需要完成阅读过程:

    public MainWindow()
    {
      ...
      using( SQLiteDataReader zReader = DBUtils.getAction(dbPath, 1))
      {
        if( rdr.Read() )
        {
          var someString = rdr.GetString(0);
          ...
        }
      }
    }

You need to check if there are results before trying to fetch them. 在尝试获取结果之前,您需要检查是否有结果。 If you know for sure there is only one result, you can do 如果您确定只有一个结果,则可以执行

if(reader.Read())
{
  MessageBox.Show(Convert.ToString(zReader["acText"]));
}

If you expect multiple rows, do something like 如果您希望有多行,请执行以下操作

while(reader.Read())
{
    // Code to read rows here
}

The DataReader always requires an open connection to read data from the Database, and you have to use Reader.Read method to get values from them, So you need to do small changes in your method signature like this: DataReader始终需要一个开放的连接来从数据库中读取数据,并且您必须使用Reader.Read方法从中获取值,因此您需要对方法签名进行一些小的更改,如下所示:

public static SQLiteDataReader getAction(string dbPath, byte actionLevel)
{
   SQLiteDataReader reader = null;

   SQLiteConnection  m_dbConnection = new SQLiteConnection("Data Source=" + dbPath + ";Version=3;FailIfMissing=True");

    m_dbConnection.Open();

    string sql = "SELECT * FROM Actions WHERE acLevel=" + actionLevel + " ORDER BY RANDOM() LIMIT 1";
   SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection)   

    reader = command.ExecuteReader();
    return reader;  
}

And in the calling method these changes have to be applied: 在调用方法中,必须应用以下更改:

SQLiteDataReader zReader = DBUtils.getAction(dbPath, 1);
while(zReader.Read())
{
    MessageBox.Show(zReader["acText"].ToString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM