• 数据库查找,修改,删除


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;

    namespace ConnSqlForm
    {
    public partial class Form4 : Form
    {
    private SqlConnection Conn;
    private SqlCommand Cmd;
    public Form4()
    {
    InitializeComponent();
    Conn = new SqlConnection("server=.;database=snewdata;user=sa;pwd=");
    }

    public DataTable selectStudent()
    {
    DataTable dt = new DataTable();
    Cmd = Conn.CreateCommand();
    //查询那个表
    Cmd.CommandText = "select *From student";
    Conn.Open();
    SqlDataReader dr = Cmd.ExecuteReader();
    for (int i = 0; i < dr.FieldCount; i++)
    {
    dt.Columns.Add(dr.GetName(i));
    }

    while (dr.Read())
    {
    DataRow row = dt.NewRow();
    row["xuehao"] = dr["xuehao"];
    row["name"] = dr["name"];
    row["sex"] = dr["sex"];
    row["birthday"] = dr["birthday"];
    dt.Rows.Add(row);
    }
    Conn.Close();
    return dt;
    }

    private void button1_Click(object sender, EventArgs e)
    {
    DataTable dt = selectStudent();
    listView1.Columns.Clear();
    listView1.Columns.Add("学号");
    listView1.Columns.Add("姓名");
    listView1.Columns.Add("性别");
    listView1.Columns.Add("生日");
    listView1.Items.Clear();
    foreach (DataRow row in dt.Rows)
    {
    ListViewItem lt = new ListViewItem();
    lt.Text = row["xuehao"].ToString();
    lt.SubItems.Add(row["name"].ToString());
    lt.SubItems.Add(row["sex"].ToString());
    lt.SubItems.Add(row["birthday"].ToString());
    listView1.Items.Add(lt);
    }
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {//选中的索引改变的时候触发selectedindexchanged当选中时触发

    }

    private void listView1_DoubleClick(object sender, EventArgs e)
    {
    if (listView1.SelectedItems.Count > 0)
    {
    string xuehao = listView1.SelectedItems[0].SubItems[0].Text;
    string name = listView1.SelectedItems[0].SubItems[1].Text;
    string sex = listView1.SelectedItems[0].SubItems[2].Text;
    string birthday = listView1.SelectedItems[0].SubItems[3].Text;
    Form2 f2 = new Form2(xuehao, name, sex, birthday);
    f2.Show();
    }
    else
    {
    MessageBox.Show("请选择后双击进行修改");
    }
    }

    private void button2_Click(object sender, EventArgs e)
    {
    if (listView1.SelectedItems.Count > 0)
    {
    string xuehao = listView1.SelectedItems[0].SubItems[0].Text;
    string name = listView1.SelectedItems[0].SubItems[1].Text;
    string sex = listView1.SelectedItems[0].SubItems[2].Text;
    string birthday = listView1.SelectedItems[0].SubItems[3].Text;
    Form2 f2 = new Form2(xuehao,name,sex,birthday);
    f2.Show();
    }
    else
    {
    MessageBox.Show("请先选择再点击修改");
    }
    }

    private void button3_Click(object sender, EventArgs e)
    {
    if (listView1.SelectedItems.Count > 0)
    {
    SqlCommand cmd = Conn.CreateCommand();
    cmd.CommandText = "delete from student where xuehao=@xuehao";
    cmd.Parameters.Clear();

    cmd.Parameters.Add("@xuehao", listView1.SelectedItems[0].Text);
    Conn.Open();
    int count = cmd.ExecuteNonQuery();
    if (count > 0)
    {
    MessageBox.Show("删除成功,受影响行数为:" + count.ToString());
    }
    else
    {
    MessageBox.Show("删除失败!");
    }
    Conn.Close();
    }
    }
    }
    }

  • 相关阅读:
    eclipse报错:发现了以元素 'd:skin' 开头的无效内容。此处不应含有子元素
    深入解析_Android的自定义布局
    RSA算法加密解密
    android版本
    TabHost+RadioGroup搭建基础布局
    android横竖屏控制
    一大波静态方法
    有时候
    简单的dialog菜单
    mongodb学习(三)——函数使用的小技巧
  • 原文地址:https://www.cnblogs.com/liuyuwen900326/p/4249821.html
Copyright © 2020-2023  润新知