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<studentdata> select()
   {
       List<studentdata> list = null;
       cmd.CommandText = "select * from student";
       conn.Open();
       SqlDataReader dr = cmd.ExecuteReader();
       if (dr.HasRows)
       {
           list =new List<studentdata>();
           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<studentdata> select(string sno,string sname,string ssex)
       {
           List<studentdata> 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<studentdata>();
               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
想对作者说点什么? 我来说一句

三层winfrom增删改查

2014年04月28日 234KB 下载

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭