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

原创 2015年07月10日 19:04:33


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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

C#Winfrom增删改查例子

  • 2013-04-16 21:43
  • 950KB
  • 下载

三层winfrom的增删改查

  • 2014-04-28 13:44
  • 234KB
  • 下载

Java WEB之JDBC连接数据库的增删改查

基于Myeclipse的数据库操作(非tomcat里导入驱动程序) 1>新建java Web项目  右键点击项目工程new->folder  起名lib(用来存放jdbc驱动程序) 2>找到驱...

JDBC连接数据库键盘录入数据增删改查学生类对象

创建一个main方法类,里面连接数据库(本人小白) package student_t; import java.sql.Connection; import java.sql....

Java连接数据库增删改查

  • 2012-12-06 15:08
  • 234KB
  • 下载

Python3.6 连接mysql 数据库,增删改查,及多线程简单运用

readme:        导入 pymysql 连接数据库,完成数据处理后的增删改查操作。导入到其他Python文件就可以直接调用。后面一个文件是处理曲线拟合和积分的然后数据和数据库交互的运用。 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)