标签:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _2015_1_25 { public partial class Formsql : Form { public Formsql() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //第一种 string sqls = "server=.;database=作业;user=sa;pwd=123"; SqlConnection conn = new SqlConnection(sqls);//数据库连接类 SqlCommand cmd = conn.CreateCommand();//数据库访问类 cmd.CommandText = "select *from student";//书写t-sql语句 conn.Open();//开启连接 SqlDataReader dr = cmd.ExecuteReader();//执行访问返回数据 while (dr.Read())//通过read下移指针一行行读取数据 { ListViewItem lt = new ListViewItem(); lt.Text = dr["sno"].ToString(); lt.SubItems.Add(dr["sname"].ToString()); lt.SubItems.Add(dr["ssex"].ToString()); lt.SubItems.Add(dr["class"].ToString()); listView1.Items.Add(lt); } conn.Close();//关闭连接 //第二种 string a = "server=.;database=作业;user=sa;pwd=123"; SqlConnection b = new SqlConnection(a); SqlCommand c = b.CreateCommand(); c.CommandText = "select*from teacher"; b.Open(); SqlDataReader d = c.ExecuteReader(); while (d.Read()) { ListViewItem f = new ListViewItem(); f.Text = d["tno"].ToString(); f.SubItems.Add(d["tname"].ToString()); f.SubItems.Add(d["tsex"].ToString()); f.SubItems.Add(d["tbirthday"].ToString()); f.SubItems.Add(d["prof"].ToString()); f.SubItems.Add(d["depart"].ToString()); listView1.Items.Add(f); } b.Close(); } } }
标签:
原文地址:http://www.cnblogs.com/XMH1217423419/p/4298652.html