winfrom连接数据库增删改查写法



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data;
using System.Data.SqlClient;

namespace Da
{
   public  class studentda
    {
       private SqlConnection conn;
       private SqlCommand cmd;
       public studentda()
       {
           conn=new SqlConnection("server=.;database=xuanke;user=sa;pwd=123");
           cmd=conn.CreateCommand();
       }
       public void insert(studentdata data)
       {
           cmd.CommandText = "insert into student values (@sno,@sname,@ssex,@sbirthday,@c)";
           cmd.Parameters.Clear();
           cmd.Parameters.Add("@sno", data.Sno);
           cmd.Parameters.Add("@sname", data.Sname);
           cmd.Parameters.Add("@ssex", data.Ssex);
           cmd.Parameters.Add("@sbirthday", data.Sbirthday);
           cmd.Parameters.Add("@c", data.C);
           conn.Open();
           cmd.ExecuteNonQuery();
           conn.Close();
       }
       public void update(studentdata data)
       {
           cmd.CommandText = "update student set sname=@sname,ssex=@ssex,sbirthday=@sbirthday,class=@c where sno=@sno";
           cmd.Parameters.Clear();
           cmd.Parameters.Add("@sno", data.Sno);
           cmd.Parameters.Add("@sname", data.Sname);
           cmd.Parameters.Add("@ssex", data.Ssex);
           cmd.Parameters.Add("@sbirthday", data.Sbirthday);
           cmd.Parameters.Add("@c", data.C);
           conn.Open();
           cmd.ExecuteNonQuery();
           conn.Close();
       }
       public void delete(string sno)
       {
           cmd.CommandText="delete from student where @sno=sno";
           cmd.Parameters.Add("@sno",sno);
           conn.Open();
           cmd.ExecuteNonQuery();
           conn.Close();
       }
       public List select()
   {
       List list = null;
       cmd.CommandText = "select * from student";
       conn.Open();
       SqlDataReader dr = cmd.ExecuteReader();
       if (dr.HasRows)
       {
           list =new List();
           while (dr.Read())
           {
               studentdata data = new studentdata();
               data.Sno = dr["sno"].ToString();
               data.Sname = dr["sname"].ToString();
               data.Ssex = dr["ssex"].ToString();
               data.Sbirthday = dr["sbirthday"].ToString();
               data.C =int.Parse( dr["class"].ToString());
               list.Add(data);
           }
       }
       cmd.Dispose();
       conn.Close();
       return list;
   }
       public studentdata select(string sno)
       {
           studentdata data = null;
           cmd.CommandText = "select * from student where sno=@sno";
           cmd.Parameters.Clear();
           cmd.Parameters.Add("@sno", sno);
           conn.Open();
           SqlDataReader dr = cmd.ExecuteReader();
           if (dr.HasRows)
           {
              data = new studentdata();
               while (dr.Read())
               {
                
                   data.Sno = dr["sno"].ToString();
                   data.Sname = dr["sname"].ToString();
                   data.Ssex = dr["ssex"].ToString();
                   data.Sbirthday = dr["sbirthday"].ToString();
                   data.C = int.Parse(dr["class"].ToString());
               
               }
           }
           cmd.Dispose();
           conn.Close();
           return data ;
       }
       public List select(string sno,string sname,string ssex)
       {
           List list = null;
           cmd.CommandText = "select * from student where sno like '%"+sno+"%' and sname like '%"+sname+"%' and ssex like '%"+ssex+"%' ";
           conn.Open();
           SqlDataReader dr = cmd.ExecuteReader();
           if (dr.HasRows)
           {
               list = new List();
               while (dr.Read())
               {
                   studentdata data = new studentdata();
                   data.Sno = dr["sno"].ToString();
                   data.Sname = dr["sname"].ToString();
                   data.Ssex = dr["ssex"].ToString();
                   data.Sbirthday = dr["sbirthday"].ToString();
                   data.C = int.Parse(dr["class"].ToString());
                   list.Add(data);
               }
           }
           cmd.Dispose();
           conn.Close();
           return list;
       }
    }
}

你可能感兴趣的:(winform)