标签:
初始数据库
winform设计图
以面向对象的思想,先封装两个类:userDate类存属性,userDA类存方法
userDate类:
public class userDate
{
private int _code;
public int Code //封装类
{
get { return _code; }
set { _code = value; }
}
private string _uname;
public string Uname
{
get { return _uname; }
set { _uname = value; }
}
private string _upass;
public string Upass
{
get { return _upass; }
set { _upass = value; }
}
}
userDA类:
class userDA
{
private SqlConnection _Conn;
private SqlCommand _Cmd;
public userDA()
{
_Conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=sa");
}
//添加
public void Insert(userDate data)
{
}
//修改
public void Update(userDate data)
{
}
//删除
public void Delete(int code)
{
}
//查询全部
public List<userDate> Select2()
{
}
//按用户名查询
public List<userDate> Select2(string uname)
{
}
}
一、增加:
核心代码:
点击添加时:
private void button1_Click(object sender, EventArgs e)
{
userDate data = new userDate();
data.Uname = textBox2.Text;
data.Upass = textBox1.Text;
new userDA().Insert(data);
}
userDA类中:Insert方法
public void Insert(userDate data)
{
_Conn.Open();
_Cmd = _Conn.CreateCommand();
_Cmd.CommandText = "insert into table1 VALUES (@uname, @upass)";
_Cmd.Parameters.Clear();
_Cmd.Parameters.Add("@uname",data.Uname);
_Cmd.Parameters.Add("@upass",data.Upass);
_Cmd.ExecuteNonQuery();
_Conn.Close();
}
1. 点增加 2.查询一下,添加上了
3.数据库里有了
二、查询:
核心代码:
点击查询时:
private void select2_Click(object sender, EventArgs e)
{
List<userDate> list= new userDA().Select2(textBox1.Text);
Binding(list);
}
//封装数据函数
private void Binding(List<userDate> list)
{
listView1.Columns.Clear();
listView1.Columns.Add("编号");
listView1.Columns.Add("用户名");
listView1.Columns.Add("密码");
listView1.Items.Clear();
foreach (userDate date in list)
{
ListViewItem lt = new ListViewItem();
lt.Text = date.Code.ToString();
lt.SubItems.Add(date.Uname);
lt.SubItems.Add(date.Upass);
listView1.Items.Add(lt);
}
}
userDA类中:两个查询函数(重载)
//查询全部
public List<userDate> Select2()
{
List<userDate> list=new List<userDate>();
_Conn.Open();
_Cmd = _Conn.CreateCommand();
_Cmd.CommandText = "select*from table1";
SqlDataReader dr = _Cmd.ExecuteReader();
while (dr.Read())
{
userDate data = new userDate();
data.Code = int.Parse(dr["code"].ToString());
data.Uname = dr["uname"].ToString();
data.Upass = dr["upass"].ToString();
list.Add(data);
}
return list;
}
//按用户名查询(条件查询)
public List<userDate> Select2(string uname)
{
List<userDate> list = new List<userDate>();
_Conn.Open();
_Cmd = _Conn.CreateCommand();
_Cmd.CommandText = "select * from table1 where uname like ‘%"+uname+"%‘";
SqlDataReader dr = _Cmd.ExecuteReader();
while (dr.Read())
{
userDate data = new userDate();
data.Code = int.Parse(dr["code"].ToString());
data.Uname = dr["uname"].ToString();
data.Upass = dr["upass"].ToString();
list.Add(data);
}
return list;
}
1.全部显示: 2.按用户名模糊查询
三、修改
核心代码:
点击时:
private void button1_Click(object sender, EventArgs e)
{
userDate data = new userDate();
data.Code = int.Parse(textBox3.Text);
data.Uname = textBox2.Text;
data.Upass = textBox1.Text;
new userDA().Update(data);
}
userDA类中:
public void Update(userDate data)
{
_Conn.Open();
_Cmd = _Conn.CreateCommand();
_Cmd.CommandText = "update table1 set uname=‘"+data.Uname+"‘,upass=‘"+data.Upass+"‘ where code="+data.Code;
_Cmd.ExecuteNonQuery();
_Conn.Close();
}
1.点击修改 2.查询后改变了 3。数据库也变了
四、删除
核心代码:
点击时:
private void delete1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0) // SelectedItems选中项
{
int code = int.Parse(listView1.SelectedItems[0].Text);
new userDA().Delete(code);
}
else
{
MessageBox.Show("请先选中,再删除!");
}
List<userDate> list = new userDA().Select2();
Binding(list);
}
userDA类:
public void Delete(int code)
{
_Conn.Open();
_Cmd = _Conn.CreateCommand();
_Cmd.CommandText = "delete from table1 where code="+code;
_Cmd.ExecuteNonQuery();
_Conn.Close();
}
1.点击删除时: 2.点击删除后:
标签:
原文地址:http://www.cnblogs.com/tzq9308/p/4332924.html