窗体
实现增删查功能
一,主页面
二,查询窗口
代码连接
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();
搜索功能代码展示
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("请输入内容!");
}
右键删除功能代码展示
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();