数据库增删改

技术分享

public class method
    {   //成员变量
        private SqlConnection conn; 
        private SqlCommand cmd;
        public method() //通过构造函数给变量赋值
        {
            conn = new SqlConnection("server=.;database=Linkdatabase;user=sa;pwd=123");
            cmd = conn.CreateCommand();
        }
        public List<bianliang> Select() //全部查询
        {
            List<bianliang> list = null; //泛型集合,包含集合的集合
            conn.Open();
            cmd.CommandText = "select * from wwz";
            SqlDataReader dr=cmd.ExecuteReader();
            if(dr.HasRows) //如果反馈的dr里面有值,才会执行下面的代码
            {
                list = new List<bianliang>(); //创建一个新的泛型集合
                while (dr.Read()) //只要返回的结果为true dr.read()即为true
                {   
                    //将数据一条条增加到集合中
                    bianliang s = new bianliang(); 
                    s.Code = dr["Code"].ToString();
                    s.Name = dr["Name"].ToString();
                    s.Class = dr["Class"].ToString();
                    s.Score = dr["Score"].ToString();
                    list.Add(s);
                }
            }
            return list;    
        }
      }



  private void button1_Click(object sender, EventArgs e) //全部查询
        {
           List<bianliang>  x= new method().Select(); //调用函数 将结果反馈给x
           listView1.Items.Clear();//将原来listview1表的内容清空一下
           for (int i = 0; i < x.Count; i++) //一次次将获得的数据显示在listview1中
           {
               listView1.Items.Add(x[i].Code); //items代表的是行
               listView1.Items[i].SubItems.Add(x[i].Name); //Items[i].SubItems代表的是Items这一行剩下的内容
               listView1.Items[i].SubItems.Add(x[i].Class);
               listView1.Items[i].SubItems.Add(x[i].Score);
           }
        }

技术分享
 public bianliang Select(string code)  //根据编号查询(条件查询),是为了以后的修改用的(加载页面时)
        {
            bianliang s = null;
            conn.Open();
            cmd.CommandText = "select * from wwz where Code="+code+"";
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows) //如果反馈的dr里面有值,才会执行下面的代码
            {    
                if (dr.Read()) //只要返回的结果为true才会执行下面的代码。 dr.read()即为true
                { 
                    s = new bianliang();
                    s.Code = dr["Code"].ToString();
                    s.Name = dr["Name"].ToString();
                    s.Class = dr["Class"].ToString();
                    s.Score = dr["Score"].ToString();             
                }
            }
            return s;
        }

        public List<bianliang> Select(string x,string y) //模糊查询(条件查询)
        {
            List<bianliang> list = null; //泛型集合,包含集合的集合
            conn.Open();
            cmd.CommandText = "select * from wwz where Class like ‘%"+x+"%‘ and Score like ‘%"+y+"%‘ ";
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows) //如果反馈的dr里面有值,才会执行下面的代码
            {
                list = new List<bianliang>(); //创建一个新的泛型集合
                while (dr.Read()) //只要返回的结果为true dr.read()即为true
                {
                    //将数据一条条增加到集合中
                    bianliang s = new bianliang();
                    s.Code = dr["Code"].ToString();
                    s.Name = dr["Name"].ToString();
                    s.Class = dr["Class"].ToString();
                    s.Score = dr["Score"].ToString();
                    list.Add(s);
                }
            }
            return list;
        }

 public void Insert(bianliang z) //增加
        {
            conn.Open();
            cmd.CommandText = "insert into wwz values(@code,@name,@class,@score)"; //@code 代位符
            cmd.Parameters.Clear();//清空原来代位符的内容
            cmd.Parameters.Add("@code",z.Code);//给代位符赋值
            cmd.Parameters.Add("@name",z.Name);
            cmd.Parameters.Add("@class",z.Class);
            cmd.Parameters.Add("@score",z.Score);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            conn.Close();
        }

 private void button1_Click(object sender, EventArgs e) //增加
        {
            if (textBox1.Text != null && textBox2.Text != null&&textBox3.Text != null&&textBox4.Text != null)
            {
            bianliang x = new bianliang();
            x.Code = textBox1.Text;
            x.Name = textBox2.Text;
            x.Class = textBox3.Text;
            x.Score = textBox4.Text;
            new method().Insert(x);
            MessageBox.Show("增加成功");
            }
        }
  private void button3_Click(object sender, EventArgs e) //增加
        {   
            add f = new add();
            f.Show();
        }

技术分享

 public void Update(bianliang x) //修改
        {
            conn.Open();
            cmd.CommandText = "update wwz set Name=‘"+x.Name+"‘, Class=‘"+x.Class+"‘,Score=‘"+x.Score+"‘ where Code="+x.Code+"";
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            conn.Close();
        }
public partial class update : Form
    {
        private string S;
        public update(string s)
        {
            InitializeComponent();
            S = s;
        }

        private void update_Load(object sender, EventArgs e) //加载页面时
        {
            bianliang load = new method().Select(S); //将调用Select(string s)函数后获取到的结果放在load里
            textBox1.Text =load.Code;
            textBox2.Text =load.Name;
            textBox3.Text = load.Class;
            textBox4.Text = load.Score;
        }

        private void button1_Click(object sender, EventArgs e) //点击修改窗体上的修改按钮
        {  //创建一个变量,将文本框内的值放在这个变量中
            bianliang update = new bianliang();  
            update.Code = textBox1.Text;
            update.Name = textBox2.Text; 
            update.Class = textBox3.Text;
            update.Score = textBox4.Text;
            new method().Update(update);
            MessageBox.Show("修改成功");   

        }
    }

private void button5_Click(object sender, EventArgs e)//修改
{

if(listView1.SelectedItems.Count>0)
{
string s = listView1.SelectedItems[0].Text;
update f = new update(s);
f.Show();
}
}

 

技术分享

 public void Delete(string code) //删除
        {
            conn.Open();
            cmd.CommandText = "delete from wwz where code="+code+"";
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            conn.Close();
        }

 private void button4_Click(object sender, EventArgs e) //删除
        {
            if(listView1.SelectedItems.Count>0)
            {
            string code = listView1.SelectedItems[0].Text;
            new method().Delete(code);
            //执行完删除后再执行一次全部查询
            List<bianliang> x = new method().Select(); //调用函数 将结果反馈给x
            listView1.Items.Clear();//将原来listview1表的内容清空一下
            for (int i = 0; i < x.Count; i++) //一次次将获得的数据显示在listview1中
            {
                listView1.Items.Add(x[i].Code); //items代表的是行
                listView1.Items[i].SubItems.Add(x[i].Name); //Items[i].SubItems代表的是Items这一行剩下的内容
                listView1.Items[i].SubItems.Add(x[i].Class);
                listView1.Items[i].SubItems.Add(x[i].Score);
            }
            }
        }
技术分享








 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。