gpt4 book ai didi

c# - MySQL .Net 连接器 - MySqlReader.Read() 返回 false

转载 作者:可可西里 更新时间:2023-11-01 08:49:33 25 4
gpt4 key购买 nike

我有一些奇怪的问题。我使用 MySQL Connector.NET,但 MySqlReader.Read() 有时返回 true,有时返回 false。 MySqlReader.HasRows 在这两种情况下都是正确的,在 VisualStudio 中我可以看到读取器对象包含所有值。

为什么会这样?

这是我的代码:

MySqlCommand sqlCommand = new MySqlCommand(sqlCode, this._conn);
MySqlDataReader rdr = sqlCommand.ExecuteReader();
PopulateMessage("--> " + serverName + ": " + dbName);
int fields = rdr.VisibleFieldCount;

//make headers
int[] fmaxl = new int[fields];
string[] headers = new string[fields];
List<string[]> vals = new List<string[]>();
if (rdr.HasRows)
{
for (int hi = 0; hi < fields; hi++)
{
string val = rdr.GetName(hi);
headers[hi] += val;
fmaxl[hi] = val.Length;
}
while (rdr.HasRows && rdr.Read()) // <-- here the Read() method returns
// false or true sometimes
// while HasRows is true
{
...

EID:rdr 包含例如 99 行的值(在 VS 中检查)并且在第一次调用 Read() 方法时返回 false。感谢 Joachim 让我发布了这条有用的通知。

最佳答案

此 HasRows 属性并非在所有版本的 .net 中。为什么不这样重写您的代码?

boolean firstRow = true;
while (rdr.Read())
{
if (firstRow) { // get the column names, but only once
firstRow = false;
for (int hi = 0; hi < fields; hi++)
{
string val = rdr.GetName(hi);
headers[hi] += val;
fmaxl[hi] = val.Length;
}
}

... //process each row.
}

完成过吨位这类工作后,我知道这很管用。

关于c# - MySQL .Net 连接器 - MySqlReader.Read() 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17653732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com