我正在我的应用程序中实现一个数据库管理器类,主要是因为有 3 个数据库要连接到一个本地数据库。
但是返回函数不起作用,我知道查询带回了行,但是当它被类返回时它有 0。我错过了什么?
public MySqlDataReader localfetchrows(string query, List<MySqlParameter> dbparams = null)
{
using (var conn = connectLocal())
{
Console.WriteLine("Connecting local : " + conn.ServerVersion);
MySqlCommand sql = conn.CreateCommand();
sql.CommandText = query;
if (dbparams != null)
{
if (dbparams.Count > 0)
{
sql.Parameters.AddRange(dbparams.ToArray());
}
}
MySqlDataReader reader = sql.ExecuteReader();
Console.WriteLine("Reading data : " + reader.HasRows + reader.FieldCount);
return reader;
/*
using (MySqlCommand sql = conn.CreateCommand())
{
sql.CommandText = query;
if (dbparams != null)
{
if (dbparams.Count > 0)
{
sql.Parameters.AddRange(dbparams.ToArray());
}
}
MySqlDataReader reader = sql.ExecuteReader();
Console.WriteLine("Reading data : " + reader.HasRows + reader.FieldCount);
sql.Parameters.Clear();
return reader;
}*/
}
}
以及获取结果的代码
query = @"SELECT jobtypeid, title FROM jobtypes WHERE active = 'Y' ORDER BY title ASC";
//parentfrm.jobtypes = db.localfetchrows(query);
var rows = db.localfetchrows(query);
Console.WriteLine("Reading data : " + rows.HasRows + rows.FieldCount);
while (rows.Read()){
}
这些脚本返回以下内容:
Connecting local : 5.5.16
Reading data : True2
Reading data : False0