使用listview显示数据
如果对您有帮助 请关注我:林新
using System.Data.SqlClient;
string conn = @"server=USER-20170520ES\SQLEXPRESS;database=Music;uid=sa;pwd=sa";
创建SQL语句,支持模糊搜索,如果不输入,就等于全选择
string sql = string.Format("select * from king_MuSic ");
SqlConnection con = null;
try
{
con = new SqlConnection(conn);
//命令对象
SqlCommand comm = new SqlCommand(sql,con);
con.Open();
SqlDataReader reader = comm.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
ListViewItem item = new ListViewItem();
item.Text = reader["singername"].ToString();
item.SubItems.Add(reader["singer"].ToString());
item.SubItems.Add(reader["Album"].ToString());
this.listView1.Items.Add(item);
}
reader.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("出现异常"+ex.Message);
}
放在加载事件里 这是dataGriderView
private void LoadData()
{
string sql = "select * from Sales_Returns1 order by datepurchase desc";
dataGridView1.DataSource = db.Select(sql);
}