首页上一页 1 下一页尾页 5 条记录 1/1页
C# 程序开发范例宝典 94页 代码有问题没?
发表在C#图书答疑
2015-02-04
是否精华
是
否
版块置顶:
是
否
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace listview控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
String sq = " Data Source=localhost; Initial Catalog=zyf_student; User ID=sa; pwd=zyf";
SqlConnection conn = new SqlConnection(sq);
try
{
conn.Open();
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandText = " select * from student ";
SqlDataReader read = com.ExecuteReader();
listView2.View = View.Details;
listView2.FullRowSelect = true;
while(read.Read())
{
ListViewItem lv = new ListViewItem(read[0].ToString());
lv.SubItems.Add(read[1].ToString());
lv.SubItems.Add(read[2].ToString());
listView2.Items.Add(lv);
}
conn.Close();
}
catch(SqlException ex)
{
MessageBox.Show("数据库异常");
}
}
private void button2_Click(object sender, EventArgs e)
{
listView2.Items.Clear();
}
}
}[img src=null/img]
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace listview控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
String sq = " Data Source=localhost; Initial Catalog=zyf_student; User ID=sa; pwd=zyf";
SqlConnection conn = new SqlConnection(sq);
try
{
conn.Open();
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandText = " select * from student ";
SqlDataReader read = com.ExecuteReader();
listView2.View = View.Details;
listView2.FullRowSelect = true;
while(read.Read())
{
ListViewItem lv = new ListViewItem(read[0].ToString());
lv.SubItems.Add(read[1].ToString());
lv.SubItems.Add(read[2].ToString());
listView2.Items.Add(lv);
}
conn.Close();
}
catch(SqlException ex)
{
MessageBox.Show("数据库异常");
}
}
private void button2_Click(object sender, EventArgs e)
{
listView2.Items.Clear();
}
}
}[img src=null/img]