窗体
实现增删查功能
一,主页面
在这里插入图片描述
二,查询窗口
在这里插入图片描述
代码连接
SqlConnection conn =new SqlConnection(“Data Source=.;Initial Catalog=dbok;User ID=sa;Password=cnm.040513”);
string sql = "select * from QQcard where qqname = "+textBox1.Text;
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader result = cmd.ExecuteReader();
while(result.Read())
{
string tempName = result["qqname"].ToString();//查询姓名
string tempAge = result["qqnameber"].ToString();//qq号
ListViewItem item = new ListViewItem(tempName);
listView1.Items.Add(item);
item.SubItems.Add(tempAge);
}
conn.Close();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
搜索功能代码展示
listView1.Items.Clear();
string ww = “”;
int ww1 = 0;
try { ww1 = Convert.ToInt32(textBox1.Text); }
catch { ww = textBox1.Text; }
String sql2 = “select * from QQcard where nickname = '”+ww+"’ or qqid= “+ww1+”;";
conn.Open();
SqlCommand cmd = new SqlCommand(sql2, conn);
try
{
SqlDataReader result = cmd.ExecuteReader();
result.Read();
string tempName = result[“qqname”].ToString();//查询姓名
string tempAge = result[“qqnameder”].ToString();//qq号
ListViewItem item = new ListViewItem(tempName);
listView1.Items.Add(item);
item.SubItems.Add(tempAge);
conn.Close();
}
catch {
conn.Close();
MessageBox.Show("请输入内容!");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
右键删除功能代码展示
MessageBox.Show(listView1.SelectedItems[0].SubItems[1].Text);
String sql = String.Format("delete from QQcard where qqnameber = {0}", listView1.SelectedItems[0].SubItems[1].Text);
MessageBox.Show(sql);
//开启对象
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
int n = cmd.ExecuteNonQuery();
MessageBox.Show("受影响的行为"+n);
//关一下
conn.Close();
//清空listview对象中的项
listView1.Items.Clear();
//重新加载load一下load
loadData();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
新增代码展示
String sql = String.Format(“insert into QQCard(qqname,qqnumber) values(‘五一’,22)”);
MessageBox.Show(sql);
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader reader = cmd.ExecuteReader();
conn.Close();
lvCards.Items.Clear();
loadData();