怎样将数据库中的表用C#打印出来
我用MySQL操作 将数据库的表 放在listview中显示出来
下面是3个函数 一个是连接mysql, 一个是查询显示表的
private void bOpen_Click(object sender, EventArgs e)
{
try
{
string newysql= string.Format("Database=test;Data Source=127.0.0.1;UserId={0};"
+ "Password={1};pooling=false;CharSet=utf8;port=3306", tID.Text, tPassword.Text);
mysql = new MySqlConnection(newysql);
mysql.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("链接失败");
}
}
private MySqlCommand SendComd(string cmd)
{
MySqlCommand mysqlcmd = new MySqlCommand(cmd, mysql);
return mysqlcmd;
}
private void bSeletc_Click(object sender, EventArgs e)
{
ListViewItem lv;
string table="person";
string cmd=string.Format("select *from {0}",table);
MySqlDataReader reader = SendComd(cmd).ExecuteReader();
if (table == "person")
{
listView1.Clear();
listView1.Columns.Add("姓名");
listView1.Columns.Add("年龄");
listView1.Columns.Add("学号");
}
try
{
while (reader.Read())
{
if (reader.HasRows)
{
lv=listView1.Items.Add(reader.GetString(0));
lv.SubItems.Add(reader.GetDouble(1).ToString());
lv.SubItems.Add(reader.GetString(2));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
reader.Close();
}
}
效果如图