首页 > 代码库 > 实体类,数据访问类应用

实体类,数据访问类应用

一,表中某个数据进行重新排序

(1)数据访问类

技术分享
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace ConsoleApplication2.App_Code
{
    public class StudentsData
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;

        public StudentsData()
        {
            conn = new SqlConnection("server=.;database=Data0216;user=sa;pwd=123");
            cmd = conn.CreateCommand();
        }

        public void ResetNumber()
        {
            List<Students> slist = new List<Students>();
            cmd.CommandText = "select *from Students";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Students s = new Students();
                s.Ids = Convert.ToInt32(dr["Ids"]);
                s.Scode = dr["Scode"].ToString();
                s.Sname = dr["Sname"].ToString();

                slist.Add(s);
            }
            conn.Close();

            int count = 1;
            foreach (Students sss in slist)
            {
                sss.Scode = "S" + count.ToString("000");
                count++;
            }

            conn.Open();
            foreach (Students ss in slist)
            {
                cmd.CommandText = "Update Students set Scode=@a where Ids = @b";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@a", ss.Scode);
                cmd.Parameters.AddWithValue("@b", ss.Ids);
                cmd.ExecuteNonQuery();
            }
            conn.Close();

        }
数据访问类

(2)实体类

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2.App_Code
{
    public class Students
    {
        private int _Ids;

        public int Ids
        {
            get { return _Ids; }
            set { _Ids = value; }
        }
        private string _Scode;

        public string Scode
        {
            get { return _Scode; }
            set { _Scode = value; }
        }
        private string _Sname;

        public string Sname
        {
            get { return _Sname; }
            set { _Sname = value; }
        }


    }
}
实体类

(3)主界面

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication2.App_Code;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            new StudentsData().ResetNumber();
           Console.ReadKey();
        }
    }
}
主界面

二.比较完善的增删改查

实体类,数据访问类应用