我创建了一个包含1个表的数据库" emp"并有一些数据。现在,每当我启动应用程序时,我都需要一个列表来从数据库中获取数据并将其保存在列表中,因为我想在运行时对数据执行一些计算,如税和工资总额(仅供显示)(不要将其保存在db)中。我尝试了很多次,但我无法理解如何做到这一点。这是我的代码:
主类:
static void Main(string[] args)
{
empDB empDB1 = new empDB();
List<emplyee> empLST1 = new List<emplyee>();
if (empLST1 == null)
{
empDB1.loadLST(out empLST1);
}
}
empDB类:
class empDB
{
private string ConnectionString = @"server=localhost;DATABASE=hris;uid=root;Password=123456;";
internal void loadLST(out List<emplyee> loadedLST)
{
string query = "select name, grade from emp";
try
{
MySqlConnection con = new MySqlConnection(ConnectionString);
con.Open();
MySqlDataReader rdr = null;
MySqlCommand cmd = new MySqlCommand(query, con);
rdr = cmd.ExecuteReader();
while(rdr.Read())
{
List<employee> returnedLst = new List<employee>();
returnedLst.Add(rdr["name"].ToString(), rdr["grade"].ToString());
}
loadedLst = returnedLst;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
即使我的方法是对的,我也不知道。我用Google搜索过几次,但几天前我刚刚开始在.net工作,所以我不明白该怎么做。
好的,我尝试了这个,而且 dosn
internal void GetDatabaseList()
{
List<employee> databases = new List<employee>();
MySqlConnection con = new MySqlConnection(ConnectionString);
{
con.Open();
DataTable tbl = con.GetSchema("Databases");
con.Close();
foreach (DataRow row in tbl.Rows)
{
databases.Add(row["hris"].ToString());
}
}
}
答案 0 :(得分:1)
假设employee
类看起来像这样:
class employee
{
public string Name { get; set; }
public string Grade { get; set; }
}
我像这样重写loadLST
:
internal List<employee> loadLST()
{
string query = "select name, grade from emp";
// we should dispose IDisposable implementations:
// connection, command and data reader
using (var con = new MySqlConnection(ConnectionString))
{
con.Open();
using (var cmd = new MySqlCommand(query, con))
using (var rdr = cmd.ExecuteReader())
{
// it is hard to maintain manual mapping
// between query results and objects;
// let's use helper like Automapper to make this easier
Mapper.CreateMap<IDataReader, employee>();
Mapper.AssertConfigurationIsValid();
return Mapper.Map<List<employee>>(rdr);
}
}
}
改进:
IDisposable
个实施(请参阅this和this)employee
实例)的结果,代码使用Automapper package out
参数被丢弃。除非您正在编写out
之类的方法,否则此处不需要异常处理和TryToDoSomething
参数(即使在这种情况下,您的方法也必须返回bool
来指示操作状态,只捕获特定的异常而不是Exception
)。另请注意,您的代码与naming guidelines不匹配(例如,employee
应为Employee
)。
答案 1 :(得分:1)
static void Main(string[] args)
{
empDB empDB1 = new empDB();
List<emplyee> empLST1 = new List<emplyee>();
**if (empLST1 == null)
{
empDB1.loadLST(out empLST1);
}**
}
这将始终为false,因为您将empLST1定义为新的List,这意味着它不为空
试试这个
public class Employee
{
public string Name { get; set; }
public string Grade { get; set; }
}
static void Main(string[] args)
{
empDB empDB1 = new empDB();
List<Employee> empLST1 = new List<Employee>();
empDB1.loadLST(ref empLST1);
}
public class empDB
{
public void loadLst(ref List<Employee> loadedLST)
{
string query = "select name, grade from emp";
try
{
MySqlConnection con = new MySqlConnection(ConnectionString);
con.Open();
MySqlDataReader rdr = null;
MySqlCommand cmd = new MySqlCommand(query, con);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee emp = new Employee();
emp.Name = rdr["name"].ToString();
emp.Grade = rdr["grade"].ToString();
loadedLST.Add(emp);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}