首页 > 代码库 > 实体类,数据访问类应用
实体类,数据访问类应用
一,表中某个数据进行重新排序
(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(); } } }
二.比较完善的增删改查
实体类,数据访问类应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。