我想从MySQL数据库中的一个表中选择并显示(比如在调试控制台中)所有数据。
我可以使用列名或索引读取数据,但只能在一个读取的行中读取数据。 有人可以告诉我如何循环行?所以我可以阅读它的数据..
感谢您的帮助。
public class DatabaseHandler
{
private bool query_status;
private int? count;
private DataTable data_table;
private static MySqlConnection connection;
private MySqlDataReader data_reader;
private MySqlCommand command;
private static DatabaseHandler databasehandler;
public void insert(string table, Dictionary<string, string> data)...
public void select(string columns, string table, string where)
{
try
{
connection.Open();
this.command = connection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "SELECT * FROM user";
this.data_reader = command.ExecuteReader();
if (this.data_reader.HasRows) {
this.count = data_reader.FieldCount;
do
{
this.data_reader.NextResult();
for (var i = 0; i < count; i++)
{
Debug.WriteLine(this.data_reader.GetValue(i));
//only returns one row. How to view all data?
}
}
while (this.data_reader.Read());
}
connection.Close();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
public void delete(string table, string where)...
public void update(string table, string new_data, string where)...
public bool get_query_status()...
public string get_custom_string()...
public int? get_query_count()...
public DataTable get_result()...
答案 0 :(得分:2)
FieldCount()返回表格中列的数量。我认为你的表只有1列,这就是为什么它不会迭代其余的记录。
我建议你删除count变量的用法,然后使用Read()方法返回的布尔值。
答案 1 :(得分:1)
您正在使用NextResult,这将移至下一个结果集。
请参阅Difference between SqlDataReader.Read and SqlDataReader.NextResult
在您的示例中,如果您将代码更改为
this.data_reader = command.ExecuteReader();
if (this.data_reader.HasRows) {
this.count = data_reader.FieldCount;
while (this.data_reader.Read());
{
for (var i = 0; i < count; i++)
{
Debug.WriteLine(this.data_reader.GetValue(i));
}
}
this.data_reader.Close();
}
答案 2 :(得分:1)
您可以使用DataReader
try
{
connection.Open();
MySqlCommand command = new MySqlCommand("SELECT * FROM User", connection);
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// access your record colums by using reader
Console.WriteLine(reader["COLUMN_NAME"]);
}
}
}
catch (Exception ex)
{
// handle exception here
}
finally
{
connection.Close();
}
或者您可以使用DataAdapter
填充本地DataSet
或DataTable
并从那里循环您的行:
try
{
connection.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM User", connection);
DataTable data = new DataTable();
adapter.Fill(data);
foreach (DataRow row in data.Rows)
{
Console.WriteLine(row["COLUMN_NAME"]);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
还要记住确保关闭所有与数据库的活动连接。
答案 3 :(得分:1)
也许尝试使用MySqlDataAdapter:
DataTable temp = new DataTable();
adapter = new MySqlDataAdapter(command);
adapter.Fill(temp);
foreach(DataColumn column in temp.Columns)
{
foreach(DataRow row in temp.Rows)
{
Console.WriteLine(row[column]);
}
}
答案 4 :(得分:1)
try
{
listView1.Items.Clear();
MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("server=localhost;port=3306;uid=sql3305088;pwd=KwxUiLHnKt;database=sql3305088;");
con.Open();
MySqlCommand com = con.CreateCommand();
com.CommandType = System.Data.CommandType.Text;
com.CommandText = "SELECT * From eslamtarek";
MySql.Data.MySqlClient.MySqlDataReader reader = com.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
ListViewItem lv = new ListViewItem();
lv.Text = reader.GetString(0);
lv.SubItems.Add(reader.GetString(1));
lv.SubItems.Add(reader.GetString(2));
lv.SubItems.Add(reader.GetString(3));
lv.SubItems.Add(reader.GetString(4));
listView1.Items.Add(lv);
}
reader.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
答案 5 :(得分:0)
您也可以尝试这种编码方式
try
{
DataTable data = new DataTable();
connection.Open();
MySqlCommand command = new MySqlCommand("SELECT * FROM User", connection);
data.load(command.ExecuteReader());
foreach(Datarow row in data.rows)
{
// access your record colums by using reader
Console.WriteLine(row["COLUMN NAME"]);
}
}
catch (Exception ex)
{
// handle exception here
}
finally
{
connection.Close();
}