属性:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data
{
public class cdata
{
private string Cno;
public string Cno1
{
get { return Cno; }
set { Cno = value; }
}
private string Cname;
public string Cname1
{
get { return Cname; }
set { Cname = value; }
}
private string Tno;
public string Tno1
{
get { return Tno; }
set { Tno = value; }
}
}
}
方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data;
using System.Data.SqlClient;
namespace Da
{
public class cda
{
private SqlConnection conn;
private SqlCommand cmd;
public cda()
{
conn = new SqlConnection("server=.;database=kl;user=sa;pwd=123");
cmd = conn.CreateCommand();
}
public void insert(cdata dt)
{
cmd.CommandText = "insert into course values(@Cno,@Cname,@Tno)";
cmd.Parameters.Clear();
cmd.Parameters.Add("@Cno", dt.Cno1);
cmd.Parameters.Add("@Cname", dt.Cname1);
cmd.Parameters.Add("@Tno", dt.Tno1);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
public void update(cdata dt)
{
cmd.CommandText = "update course set Cname=@Cname,Tno=@Tno where Cno=@Cno";
cmd.Parameters.Clear();
cmd.Parameters.Add("@Cno", dt.Cno1);
cmd.Parameters.Add("@Cname", dt.Cname1);
cmd.Parameters.Add("@Tno", dt.Tno1);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
public void delete(string Cno)
{
cmd.CommandText = "delete from course where Cno=@Cno";
cmd.Parameters.Add("@Cno", Cno);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
//-----------------------------------------------查询
public List<cdata> select()
{
List<cdata> list = null;
cmd.CommandText = "select *from course";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
list = new List<cdata>();
while (dr.Read())
{
cdata dt = new cdata();
dt.Cno1 = dr["Cno"].ToString();
dt.Cname1 = dr["Cname"].ToString();
dt.Tno1 = dr["Tno"].ToString();
list.Add(dt);
}
}
cmd.Dispose();
conn.Close();
return list;
}
public cdata select(String Cno)
{
cdata list = null;
cmd.CommandText = "select *from course where Cno=@Cno";
cmd.Parameters.Clear();
cmd.Parameters.Add("@Cno", Cno);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
list = new cdata();
if (dr.Read())
{
cdata dt = new cdata();
dt.Cno1 = dr["Cno"].ToString();
dt.Cname1 = dr["Cname"].ToString();
dt.Tno1 = dr["Tno"].ToString();
}
}
conn.Close();
cmd.Dispose();
return list;
}
public List<cdata> select(string Cno,string Cname,string Tno)
{
List<cdata> list = null;
cmd.CommandText = "select *from course where Cno like '%" + Cno + "%' and Cname like '%" + Cname + "%' and Tno like '%" + Tno + "%'";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
list = new List<cdata>();
while (dr.Read())
{
cdata dt = new cdata();
dt.Cno1 = dr["Cno"].ToString();
dt.Cname1 = dr["Cname"].ToString();
dt.Tno1 = dr["Tno"].ToString();
list.Add(dt);
}
}
cmd.Dispose();
conn.Close();
return list;
}
}
}
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 Da;
using Data;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//private string Cno;
//public Form1(string cno)
//{
// InitializeComponent();
// Cno = cno;
//}
private void button1_Click(object sender, EventArgs e)//查询
{
List<cdata> list = new cda().select();
bindListView(list);
}
//查看首先获取显示数据
public void bindListView(List<cdata> list)
{
listView1.Columns.Clear();
listView1.Columns.Add("Cno");
listView1.Columns.Add("Cname");
listView1.Columns.Add("Tno");
listView1.Items.Clear();
foreach (cdata dt in list)
{
ListViewItem l = new ListViewItem();
l.Text = dt.Cno1;
l.SubItems.Add(dt.Cname1);
l.SubItems.Add(dt.Tno1);
listView1.Items.Add(l);
}
}
private void button4_Click(object sender, EventArgs e)//删除
{
if (listView1.SelectedItems.Count>0)
{
string Cno = listView1.SelectedItems[0].Text;
new cda().delete(Cno);
//删除后重新绑定数据
List<cdata> list = new cda().select();
bindListView(list);
}
else
{
MessageBox.Show("请选中要删除的内容");
}
}
private void button7_Click(object sender, EventArgs e)//条件查询
{
List<cdata> list = new cda().select(textBox4.Text, textBox5.Text, textBox6.Text);
bindListView(list);
}
private void button3_Click(object sender, EventArgs e)//增加
{
textBox1.Enabled = true;
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
private void button5_Click(object sender, EventArgs e)//确认增加
{
cdata dt = new cdata();
dt.Cno1 = textBox1.Text;
dt.Cname1 = textBox2.Text;
dt.Tno1 = textBox3.Text;
new cda().insert(dt);
}
private void button2_Click(object sender, EventArgs e)//修改
{
if (listView1.SelectedItems.Count > 0)
{
string Cno = listView1.SelectedItems[0].SubItems[0].Text;
string Cname = listView1.SelectedItems[0].SubItems[1].Text;
string Tno = listView1.SelectedItems[0].SubItems[2].Text;
textBox1.Text = Cno;
textBox2.Text = Cname;
textBox3.Text = Tno;
}
else
{
MessageBox.Show("请选中要修改的内容");
}
}
private void button6_Click(object sender, EventArgs e)//确认修改
{
cdata dt = new cdata();
dt.Cno1 = textBox1.Text;
dt.Cname1 = textBox2.Text;
dt.Tno1 = textBox3.Text;
new cda().update(dt);
}
}
}