数据展示中使用ListView
listView1.Items.Clear();
string Connstr = "Server=172.*.*.150;database=pubs;uid=sa;pwd=sa;";
SqlConnection Conn = new SqlConnection(Connstr);
SqlCommand Com = new SqlCommand();
try
{
Conn.Open();
//MessageBox.Show("连接成功");
string sql = "SELECT [job_id],[job_desc],[min_lvl],[max_lvl] FROM [pubs].[dbo].[jobs]";
Com.Connection = Conn;
Com.CommandText = sql;
SqlDataReader reader = Com.ExecuteReader();
while (reader.Read())
{
listView1.BeginUpdate();
ListViewItem lv = new ListViewItem();
lv.Checked = true;
lv.SubItems[0].Text = reader[0].ToString();
lv.SubItems.Add(reader[1].ToString());
lv.SubItems.Add(reader[2].ToString());
lv.SubItems.Add(reader[3].ToString());
listView1.Items.Add(lv);
listView1.EndUpdate();
}
}
catch (Exception exp)
{
MessageBox.Show("无法连接到指定的PC数据库!\n" + exp.ToString());
}
finally
{
Conn.Close();
}