我正在学习如何使用C#访问MySQL数据库 通过关注this guide
在这些代码中,它使用List<string>[4]
来存储Selected数据,因为它有4列
经过一些实验,我发现这些代码以如下格式存储数据:
//List[0] = Name1, Name2, Name3, ...
//List[1] = Age1, Age2, Age3, ...
//...
我希望逐行访问数据,并将其显示在ListView
,
但如果我们使用foreach
,则会按列列出数据列
foreach (List<string> entry in dataEntries){
ListViewItem lvi = new ListViewItem(entry[0]); //refer to Name1 during first loop
lvi.SubItems.Add(entry[1]); //Name2
lvi.SubItems.Add(entry[2]); //Name3
lvi.SubItems.Add(entry[3]); //and so on
listView1.Items.Add(lvi);
}
有没有办法逐行显示?或者有更简单的解决方法吗?
答案 0 :(得分:2)
是的,这篇文章非常糟糕。我把文章中的代码放在最后。我将如何做到这一点:
注意:我把所有东西都变成了一个字符串,这似乎是错的。另外,我没有测试,所以可能存在拼写错误。
class DataItem
{
public string id { get; set; }
public string name { get; set; }
public string age { get; set; }
}
//Select statement
public List<DataITem> Select()
{
string query = "SELECT * FROM tableinfo";
//Create a list to store the result
List<DataItem> list = new List<DateItem>();
//Open connection
if (this.OpenConnection())
{
//Create Command
using (MySqlCommand cmd = new MySqlCommand(query, connection))
{
//Create a data reader and Execute the command
using (MySqlDataReader dataReader = cmd.ExecuteReader())
{
//Read the data and store them in the list
while (dataReader.Read())
{
list.Add(new DataItem() { id = dataReader["id"] + "",
name = dataReader["name"] + "",
age = dataReader["age"] + "" });
}
}
}
this.CloseConnection();
}
return list;
}
文章代码:
//Select statement
public List< string >[] Select()
{
string query = "SELECT * FROM tableinfo";
//Create a list to store the result
List< string >[] list = new List< string >[3];
list[0] = new List< string >();
list[1] = new List< string >();
list[2] = new List< string >();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
list[0].Add(dataReader["id"] + "");
list[1].Add(dataReader["name"] + "");
list[2].Add(dataReader["age"] + "");
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return list;
}
else
{
return list;
}
}
答案 1 :(得分:1)
本教程的这一部分:
public List< string >[] Select()
{
string query = "SELECT * FROM tableinfo";
//Create a list to store the result
List< string >[] list = new List< string >[3];
list[0] = new List< string >();
list[1] = new List< string >();
list[2] = new List< string >();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
list[0].Add(dataReader["id"] + "");
list[1].Add(dataReader["name"] + "");
list[2].Add(dataReader["age"] + "");
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return list;
}
else
{
return list;
}
}
我想,是你所指的。这不是一种存储结果的好方法,而且它的写入相当差。你可以轻松地做到:
public List<object[]> Select() {
string query = "SELECT * FROM tableinfo";
// Create a list to store the result
var result = new List<object[]>();
// Open connection
if (this.OpenConnection()) {
// Create Command
var cmd = new MySqlCommand(query, connection);
// Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
// Read the data and store them in the list
while(dataReader.Read()) {
object[] items = new object[dataReader.FieldCount];
dataReader.GetValues(items);
result.Add(items);
}
// Close Data Reader
dataReader.Close();
// Close Connection
this.CloseConnection();
// Return list to be displayed
return list;
} else {
// This is a bad thing.
throw new ApplicationException("Could not connect to database.");
}
}
如果我正确地回忆起数据库访问,那么你应该这样做:
public DataSet Select() {
if(!this.OpenConnection()) {
throw new ApplicationException("Could not connect to database.");
}
using(var query = new MySqlDataAdapter("SELECT * FROM tableinfo", this.connection)) {
var data = new DataSet();
query.Fill(data);
return data;
}
}