英文:
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("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;
}
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<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(strQuery, oConnection);
oConnection.Open();
using var oReader = oCommand.ExecuteReader();
var employees = new List<clsEmployee>();
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);
}
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<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(strQuery, oConnection);
await oConnection.OpenAsync();
using var oReader = await oCommand.ExecuteReaderAsync();
var employees = new List<clsEmployee>();
while (await reader.ReadAsync())
{
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);
}
return employees;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论