关闭

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

267人阅读 评论(0) 收藏 举报
分类:


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;
       }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2730次
    • 积分:227
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档