MySQL DataReader只读取第一行,其他什么都不读。

huangapple go评论64阅读模式
英文:

MySQL DataReader only read first row and nothing else

问题

我一直在尝试从我的Employee表(7列)中读取数据,有3名员工注册,但DataReader只读取了第一行(7个字段而不是总共的21个字段)。

[HttpGet]
[Route("GetEmployees")]
public List<clsEmployee> GetEmployees()
{
    MySqlDataReader reader;
    List<clsEmployee> employees = new List<clsEmployee>();

    string strSQL = "SELECT e.EmployeeID, e.FirstName, e.LastName, e.email, e.dni, e.phone, e.SpecialityID from Employee e";

    reader = CmdDatabase.GetReader(strSQL, configuration.GetConnectionString("da_adminsys"));

    while (reader.Read())
    {
        clsEmployee emp = new clsEmployee
        {
            EmployeeID = Convert.ToInt32(reader[0]),
            FirstName = reader["firstname"].ToString(),
            LastName = reader["lastname"].ToString(),
            Email = reader["email"].ToString(),
            Phone = reader["phone"].ToString(),
            DNI = reader["dni"].ToString(),
            SpecialityID = Convert.ToInt32(reader["SpecialityID"]),
        };

        employees.Add(emp);
    }

    foreach (var item in employees)
    {
        clsSpeciality speciality = GetSpeciality(item.SpecialityID);
        item.Speciality.Add(speciality);
    }

    reader.Close();

    return employees;
}

以及来自我的CmdDatabase类的GetReader方法:

public static MySqlDataReader GetReader(string strQuery, string CN)
{
    MySqlConnection oConnection = new MySqlConnection(CN);
    MySqlCommand oCommand = new MySqlCommand(strQuery, oConnection);
    oConnection.Open();
    MySqlDataReader oReader;

    oReader = oCommand.ExecuteReader(CommandBehavior.CloseConnection);

    oCommand.Dispose();
    return oReader;
}

在while循环中只计算了7个FieldCount,而表中总共有21个字段。

英文:

I've been trying to read data from my table Employee (7 columns) with 3 employees registered, and DataReader just read first row (7 fields instead the total, 21 fields)

[HttpGet]
[Route(&quot;GetEmployees&quot;)]
public List&lt;clsEmployee&gt; GetEmployees()
{
    MySqlDataReader reader;
    List&lt;clsEmployee&gt; employees = new List&lt;clsEmployee&gt;();

    string strSQL = &quot;SELECT e.EmployeeID, e.FirstName, e.LastName, e.email, e.dni, e.phone, e.SpecialityID from Employee e&quot;;

    reader = CmdDatabase.GetReader(strSQL, configuration.GetConnectionString(&quot;da_adminsys&quot;));

    while (reader.Read())
    {
        clsEmployee emp = new clsEmployee
        {
            EmployeeID = Convert.ToInt32(reader[0]),
            FirstName = reader[&quot;firstname&quot;].ToString(),
            LastName = reader[&quot;lastname&quot;].ToString(),
            Email = reader[&quot;email&quot;].ToString(),
            Phone = reader[&quot;phone&quot;].ToString(),
            DNI = reader[&quot;dni&quot;].ToString(),
            SpecialityID = Convert.ToInt32(reader[&quot;SpecialityID&quot;]),
        };

        employees.Add(emp);
    }

    foreach (var item in employees)
    {
        clsSpeciality speciality = GetSpeciality(item.SpecialityID);
        item.Speciality.Add(speciality);
    }

    reader.Close();

    return employees;
}

And GetReader from my class CmdDatabase:

public static MySqlDataReader GetReader(string strQuery, string CN)
{
    MySqlConnection  oConnection = new MySqlConnection(CN);
    MySqlCommand oCommand = new MySqlCommand(strQuery, oConnection);
    oConnection.Open();
    MySqlDataReader oReader;

    oReader = oCommand.ExecuteReader(CommandBehavior.CloseConnection);

    oCommand.Dispose();
    return oReader;
}

At the while loop its just counting 7 FieldCount, and totally in the table are 21 fields

答案1

得分: 1

你做得不对,因为你在完成从读取器中读取之前就处置了命令。

你需要在 using 中同时包含连接、命令和读取器。

public List<clsEmployee> GetEmployees()
{
    var employees = GetEmployeesFromDb(configuration.GetConnectionString("da_adminsys"));

    foreach (var item in employees)
    {
        clsSpeciality speciality = GetSpeciality(item.SpecialityID);
        item.Speciality.Add(speciality);
    }

    return employees;
}

public static List<clsEmployee> GetEmployeesFromDb(string connectionString)
{
    const string strSQL = @"
SELECT
  e.EmployeeID,
  e.FirstName,
  e.LastName,
  e.email,
  e.dni,
  e.phone,
  e.SpecialityID
FROM Employee e;
";

    using var oConnection = new MySqlConnection(connectionString);
    using var oCommand = new MySqlCommand(strSQL, oConnection);
    oConnection.Open();
    using var oReader = oCommand.ExecuteReader();
    var employees = new List<clsEmployee>();

    while (oReader.Read())
    {
        clsEmployee emp = new clsEmployee
        {
            EmployeeID = Convert.ToInt32(oReader[0]),
            FirstName = oReader["firstname"].ToString(),
            LastName = oReader["lastname"].ToString(),
            Email = oReader["email"].ToString(),
            Phone = oReader["phone"].ToString(),
            DNI = oReader["dni"].ToString(),
            SpecialityID = Convert.ToInt32(oReader["SpecialityID"]),
        };

        employees.Add(emp);
    }
    return employees;
}

我不知道你的 GetSpeciality 做什么,但如果它是一个数据库调用,那么最好使用一个带有联接的单个数据库调用。

另外,考虑使用 async,特别是因为这是在 ASP.Net HTTP 请求的中间。这将如下所示:

public async Task<List<clsEmployee>> GetEmployees()
{
    var employees = await GetEmployeesFromDb(configuration.GetConnectionString("da_adminsys"));

    foreach (var item in employees)
    {
        clsSpeciality speciality = GetSpeciality(item.SpecialityID);
        item.Speciality.Add(speciality);
    }

    return employees;
}

public static async Task<List<clsEmployee>> GetEmployeesFromDb(string connectionString)
{
    const string strSQL = @"
SELECT
  e.EmployeeID,
  e.FirstName,
  e.LastName,
  e.email,
  e.dni,
  e.phone,
  e.SpecialityID
FROM Employee e;
";

    using var oConnection = new MySqlConnection(connectionString);
    using var oCommand = new MySqlCommand(strSQL, oConnection);
    await oConnection.OpenAsync();
    using var oReader = await oCommand.ExecuteReaderAsync();
    var employees = new List<clsEmployee>();

    while (await oReader.ReadAsync())
    {
        clsEmployee emp = new clsEmployee
        {
            EmployeeID = Convert.ToInt32(oReader[0]),
            FirstName = oReader["firstname"].ToString(),
            LastName = oReader["lastname"].ToString(),
            Email = oReader["email"].ToString(),
            Phone = oReader["phone"].ToString(),
            DNI = oReader["dni"].ToString(),
            SpecialityID = Convert.ToInt32(oReader["SpecialityID"]),
        };

        employees.Add(emp);
    }
    return employees;
}
英文:

You are doing this wrong, as you are disposing the command before finshing reading from the reader.

You need to have the conenction, command and reader all in using.

public List&lt;clsEmployee&gt; GetEmployees()
{
    var employees = GetEmployeesFromDb(configuration.GetConnectionString(&quot;da_adminsys&quot;));

    foreach (var item in employees)
    {
        clsSpeciality speciality = GetSpeciality(item.SpecialityID);
        item.Speciality.Add(speciality);
    }

    return employees;
}

public static List&lt;clsEmployee&gt; GetEmployeesFromDb(string connectionString)
{
    const string strSQL = @&quot;
SELECT
  e.EmployeeID,
  e.FirstName,
  e.LastName,
  e.email,
  e.dni,
  e.phone,
  e.SpecialityID
from Employee e;
&quot;;

    using var oConnection = new MySqlConnection(connectionString);
    using var oCommand = new MySqlCommand(strQuery, oConnection);
    oConnection.Open();
    using var oReader = oCommand.ExecuteReader();
    var employees = new List&lt;clsEmployee&gt;();

    while (reader.Read())
    {
        clsEmployee emp = new clsEmployee
        {
            EmployeeID = Convert.ToInt32(reader[0]),
            FirstName = reader[&quot;firstname&quot;].ToString(),
            LastName = reader[&quot;lastname&quot;].ToString(),
            Email = reader[&quot;email&quot;].ToString(),
            Phone = reader[&quot;phone&quot;].ToString(),
            DNI = reader[&quot;dni&quot;].ToString(),
            SpecialityID = Convert.ToInt32(reader[&quot;SpecialityID&quot;]),
        };

        employees.Add(emp);
    }
    return employees;
}

I have no idea what your GetSpeciality does, but if it's a database call then you are much better off using a single database call with a join.


Also consider using async especially given that this is in the middle of an ASP.Net HTTP request. That would look like this

public async Task&lt;List&lt;clsEmployee&gt;&gt; GetEmployees()
{
    var employees = await GetEmployeesFromDb(configuration.GetConnectionString(&quot;da_adminsys&quot;));

    foreach (var item in employees)
    {
        clsSpeciality speciality = GetSpeciality(item.SpecialityID);
        item.Speciality.Add(speciality);
    }

    return employees;
}

public static async Task&lt;List&lt;clsEmployee&gt;&gt; GetEmployeesFromDb(string connectionString)
{
    const string strSQL = @&quot;
SELECT
  e.EmployeeID,
  e.FirstName,
  e.LastName,
  e.email,
  e.dni,
  e.phone,
  e.SpecialityID
from Employee e;
&quot;;

    using var oConnection = new MySqlConnection(connectionString);
    using var oCommand = new MySqlCommand(strQuery, oConnection);
    await oConnection.OpenAsync();
    using var oReader = await oCommand.ExecuteReaderAsync();
    var employees = new List&lt;clsEmployee&gt;();

    while (await reader.ReadAsync())
    {
        clsEmployee emp = new clsEmployee
        {
            EmployeeID = Convert.ToInt32(reader[0]),
            FirstName = reader[&quot;firstname&quot;].ToString(),
            LastName = reader[&quot;lastname&quot;].ToString(),
            Email = reader[&quot;email&quot;].ToString(),
            Phone = reader[&quot;phone&quot;].ToString(),
            DNI = reader[&quot;dni&quot;].ToString(),
            SpecialityID = Convert.ToInt32(reader[&quot;SpecialityID&quot;]),
        };

        employees.Add(emp);
    }
    return employees;
}

huangapple
  • 本文由 发表于 2023年5月28日 14:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定