![]()
private string link = "server=.;database=list;user=sa;pwd=123";
public void chaxun() //创建一个查询函数
{
SqlConnection coon = new SqlConnection(link); //连接数据库
coon.Open();//打开数据库
SqlCommand cmd = coon.CreateCommand();//创建命令
cmd.CommandText = "select * from listview";//写命令内容
SqlDataReader dr = cmd.ExecuteReader();//执行命令
int index = 0; //定义listview.Items的索引
listView1.Items.Clear(); //先将listView1.Items清空;
while (dr.Read()) //循环条件,里面有数据
{ //将数据一条一条增加到listView.Items的集合中
listView1.Items.Add(dr["code"].ToString());//Items代表的是每列
listView1.Items[index].SubItems.Add(dr["name"].ToString());//SubItems其他列
listView1.Items[index].SubItems.Add(dr["pass"].ToString());
index++;
}
cmd.Dispose();
coon.Close();
}
private void button1_Click(object sender, EventArgs e) //查询
{
SqlConnection coon = new SqlConnection(link);//连接数据库
coon.Open();//打开数据库
SqlCommand cmd = coon.CreateCommand();//创建命令
cmd.CommandText = "select * from listview";//写命令内容
SqlDataReader dr = cmd.ExecuteReader();//执行命令
int index = 0;//定义listview.Items的索引
listView1.Items.Clear();//先将listView1.Items清空;
while (dr.Read())//循环条件,里面有数据
{ //将数据一条一条增加到listView.Items的集合中
listView1.Items.Add(dr["code"].ToString());//Items代表的是每列
listView1.Items[index].SubItems.Add(dr["name"].ToString());//SubItems其他列
listView1.Items[index].SubItems.Add(dr["pass"].ToString());
index++;//索引++ ,再循环
}
cmd.Dispose();//命令清空
coon.Close(); //数据库关闭
}
private void button2_Click(object sender, EventArgs e) //删除
{
if(listView1.SelectedItems.Count>0) //如果选中的要删除数据的数量大于0才可以执行删除,否则不执行
{ string scode = listView1.SelectedItems[0].Text.ToString(); //创建一个变量来接收第一列索引的文本内容
SqlConnection coon = new SqlConnection(link);//连接数据库
coon.Open();//打开数据库
SqlCommand cmd = coon.CreateCommand();//创建命令
cmd.CommandText = "delete from listview where code='" + scode + "'";//写命令内容
cmd.ExecuteNonQuery(); //执行命令
cmd.Dispose();//命令清空
coon.Close();//数据库关闭
chaxun(); //执行查询函数
}
}
private void button3_Click(object sender, EventArgs e) //主页增加按钮
{
add f = new add();
f.Show(); //弹出增加框
}
private void button1_Click(object sender, EventArgs e) //增加
{ if(textBox2.Text!=null&&textBox3.Text!=null) //只有当文本栏不为空才能执行增加,否则点增加没反应
{
string code=textBox1.Text; //第一栏的文本
string name=textBox2.Text; //第二栏的文本
string pass=textBox3.Text; //第三栏的文本
SqlConnection coon = new SqlConnection("server=.;database=list;user=sa;pwd=123");//连接数据库
coon.Open();//打开数据库
SqlCommand cmd = coon.CreateCommand();//创建命令
cmd.CommandText = "insert into listview values('"+name+"','"+pass+"')";//写命令语句
cmd.ExecuteNonQuery();//执行命令
cmd.Dispose();//清空命令
coon.Close();//关闭数据库
MessageBox.Show("增加成功");
}
}
private void button4_Click(object sender, EventArgs e) //主页修改按钮
{
if(listView1.SelectedItems.Count>0) //选中要修改的内容才能执行修改
{
string code = listView1.SelectedItems[0].Text; //将所选中文本的第一列的内容赋给code
update f = new update(code);//将code扔进去 是为了让修改窗体接收这个code值 (鼠标右键 转到定义)
f.Show();
}
}
private string Code;
public update(string code) //将主页面所选中内容的code值传给窗体update,并且显示在窗体update的第一栏文本内
{
InitializeComponent();
Code = code;//传值
textBox1.Text = Code;//显示
}
private void button1_Click(object sender, EventArgs e) //修改
{
if(textBox2.Text!=null&&textBox3.Text!=null) //只有当文本栏内容不为空才会执行修改
{
string name = textBox2.Text;//第二栏文本
string pass = textBox3.Text;//第三栏文本
SqlConnection coon = new SqlConnection("server=.;database=list;user=sa;pwd=123");//连接数据库
coon.Open();//打开数据库
SqlCommand cmd = coon.CreateCommand();//创建命令
cmd.CommandText = "update listview set name='"+name+"',pass='"+pass+"' where code="+Code+"";//写命令语句
cmd.ExecuteNonQuery();//执行命令
cmd.Dispose();//清空命令
coon.Open();//关闭数据库
MessageBox.Show("修改成功");
}
}