2023年6月21日发(作者:)
数据库作业⼗⼋-使⽤visualstudio连接sqlserver实现对⼀张表的增删改查界⾯外观先附上数据库中某⼀张表的数据开始运⾏先根据学⽣信息,抽象出相应的对象using System;using c;using ;using ;using ;namespace Model{ public class StudentInfo { public string Sno { get; set; } public string Sname { get; set; } public string Ssex { get; set; } public int Sage { get; set; } public string Sdept { get; set; } }}明确功能后,开始写数据访问层1.先添加⼀个类,封装好访问数据层的⽅法using System;using c;using uration;using ;using ent;using ;using ;using ;namespace {{ public static class SqlHelper { //从配置⽂件⾥读取连接字符串 private static readonly string conStr = tionStrings["mssql"].ConnectionString; public static int ExecuteNonquery(string sql, CommandType cmdType, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(conStr)) { using (SqlCommand com = new SqlCommand(sql, con)) { dType = cmdType; if (pms != null) { ge(pms); } (); return eNonQuery(); } } } public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(conStr)) { using (SqlCommand com = new SqlCommand(sql, con)) { dType = cmdType; if (pms != null) { ge(pms); } (); return eScalar(); } } } public static SqlDataReader ExecuteReader(string sql, CommandType cmdType, params SqlParameter[] pms) { SqlConnection con = new SqlConnection(conStr); using (SqlCommand com = new SqlCommand(sql, con)) { dType = cmdType; if (pms != null) { ge(pms); } try { (); return eReader(onnection); } catch { (); e(); throw; } } } public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] pms) { DataTable dt = new DataTable(); using (SqlDataAdapter adapter = new SqlDataAdapter(sql, conStr)) { dType = cmdType; if (pms != null) { ge(pms); } (dt); } return dt; } }}2.开始写数据访问层的代码using Model;using System;using c;using ;using ent;using ;using ;using ;namespace { public class StudentDAL { ///
获取学⽣信息 /// ///
增加学⽣ /// /// 学⽣对象 ///
更新学⽣信息 /// /// 学⽣对象 ///
根据学⽣编号删除学⽣的数据 /// /// 学⽣编号 ///
返回全部学⽣的数据 /// ///
增加学⽣信息 /// /// 学⽣对象 ///
更新学⽣信息 /// /// 学⽣对象 ///
删除学⽣信息 /// /// 学号 ///
urce = Students(); if (>0) { [0].Selected = false; } = new Font("微软雅⿊", 10);
} private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { DataGridViewRow currentRow = [ex]; StudentInfo student = undItem as StudentInfo; if (student!=null) { = ; = ; = ; = ng(); = ; } } //关闭窗体 private void ucBtnExt4_BtnClick(object sender, EventArgs e) { (); } //增加学⽣信息 private void ucBtnExt1_BtnClick(object sender, EventArgs e) { if (Check()) { StudentInfo student = new StudentInfo(); = (); = (); = 32(()); = (); = (); string msg = dentInfo(student) ? "操作成功" : "操作失败"; (msg); LoadStudentInfo(); } else { ("请将学⽣的信息全部输⼊"); } } private bool Check() { if (OrEmpty(())) { return false; } if (OrEmpty(())) { return false; } if (OrEmpty(())) { return false; } if (OrEmpty(())) { return false; } if (OrEmpty(())) { return false; } return true; } //修改学⽣信息 private void ucBtnExt2_BtnClick(object sender, EventArgs e) { if (Check()) { StudentInfo student = new StudentInfo(); = (); = (); = 32(()); = (); = (); string msg = StudentInfo(student) ? "操作成功" : "操作失败"; (msg); LoadStudentInfo(); } else { ("请将学⽣的信息全部输⼊"); } } //删除学⽣信息 private void ucBtnExt3_BtnClick(object sender, EventArgs e) { if (Check()) { string msg = StudentInfo(()) ? "操作成功" : "操作失败"; (msg); LoadStudentInfo(); } else { ("请选中要删除的⾏"); } } }}在⽹上看到了⼀个开源控件库(请点开链接,给作者点个☆),界⾯外观确实⽐⾃带的控件美观。该图是作者⽂档⾥展⽰的,虽然是再进⼀步扩展后得到的,但是界⾯已经做的挺好了。于是下载下来后,发现不怎么会⽤。加⼊作者建⽴的交流群⾥,询问了⼀下,才做到了展⽰数据这⼀步。然后发现,该控件库外观确实⽐⾃带的控件美观,但是,相应的属性和事件⽐较少,需要⾃⼰写。由于技术⽐较菜,折腾了⼀下午,也没做成想要的事件,于是⼜使⽤⾃带的DataGridView展⽰数据。(不过该控件库也没完全没发挥作⽤,下⾯⼏个按钮就是该控件库⾥的)。初步简单使⽤了⼀下,没发现什么⼤问题。⼀些功能还需要完善,因时间问题,就先写到这,毕竟这⼏天,该做什么,已经基本安排好了,时间⽐较紧,就先到这,等五⼀假期有空了,再完善。
2023年6月21日发(作者:)
数据库作业⼗⼋-使⽤visualstudio连接sqlserver实现对⼀张表的增删改查界⾯外观先附上数据库中某⼀张表的数据开始运⾏先根据学⽣信息,抽象出相应的对象using System;using c;using ;using ;using ;namespace Model{ public class StudentInfo { public string Sno { get; set; } public string Sname { get; set; } public string Ssex { get; set; } public int Sage { get; set; } public string Sdept { get; set; } }}明确功能后,开始写数据访问层1.先添加⼀个类,封装好访问数据层的⽅法using System;using c;using uration;using ;using ent;using ;using ;using ;namespace {{ public static class SqlHelper { //从配置⽂件⾥读取连接字符串 private static readonly string conStr = tionStrings["mssql"].ConnectionString; public static int ExecuteNonquery(string sql, CommandType cmdType, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(conStr)) { using (SqlCommand com = new SqlCommand(sql, con)) { dType = cmdType; if (pms != null) { ge(pms); } (); return eNonQuery(); } } } public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(conStr)) { using (SqlCommand com = new SqlCommand(sql, con)) { dType = cmdType; if (pms != null) { ge(pms); } (); return eScalar(); } } } public static SqlDataReader ExecuteReader(string sql, CommandType cmdType, params SqlParameter[] pms) { SqlConnection con = new SqlConnection(conStr); using (SqlCommand com = new SqlCommand(sql, con)) { dType = cmdType; if (pms != null) { ge(pms); } try { (); return eReader(onnection); } catch { (); e(); throw; } } } public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] pms) { DataTable dt = new DataTable(); using (SqlDataAdapter adapter = new SqlDataAdapter(sql, conStr)) { dType = cmdType; if (pms != null) { ge(pms); } (dt); } return dt; } }}2.开始写数据访问层的代码using Model;using System;using c;using ;using ent;using ;using ;using ;namespace { public class StudentDAL { ///
获取学⽣信息 /// ///
增加学⽣ /// /// 学⽣对象 ///
更新学⽣信息 /// /// 学⽣对象 ///
根据学⽣编号删除学⽣的数据 /// /// 学⽣编号 ///
返回全部学⽣的数据 /// ///
增加学⽣信息 /// /// 学⽣对象 ///
更新学⽣信息 /// /// 学⽣对象 ///
删除学⽣信息 /// /// 学号 ///
urce = Students(); if (>0) { [0].Selected = false; } = new Font("微软雅⿊", 10);
} private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { DataGridViewRow currentRow = [ex]; StudentInfo student = undItem as StudentInfo; if (student!=null) { = ; = ; = ; = ng(); = ; } } //关闭窗体 private void ucBtnExt4_BtnClick(object sender, EventArgs e) { (); } //增加学⽣信息 private void ucBtnExt1_BtnClick(object sender, EventArgs e) { if (Check()) { StudentInfo student = new StudentInfo(); = (); = (); = 32(()); = (); = (); string msg = dentInfo(student) ? "操作成功" : "操作失败"; (msg); LoadStudentInfo(); } else { ("请将学⽣的信息全部输⼊"); } } private bool Check() { if (OrEmpty(())) { return false; } if (OrEmpty(())) { return false; } if (OrEmpty(())) { return false; } if (OrEmpty(())) { return false; } if (OrEmpty(())) { return false; } return true; } //修改学⽣信息 private void ucBtnExt2_BtnClick(object sender, EventArgs e) { if (Check()) { StudentInfo student = new StudentInfo(); = (); = (); = 32(()); = (); = (); string msg = StudentInfo(student) ? "操作成功" : "操作失败"; (msg); LoadStudentInfo(); } else { ("请将学⽣的信息全部输⼊"); } } //删除学⽣信息 private void ucBtnExt3_BtnClick(object sender, EventArgs e) { if (Check()) { string msg = StudentInfo(()) ? "操作成功" : "操作失败"; (msg); LoadStudentInfo(); } else { ("请选中要删除的⾏"); } } }}在⽹上看到了⼀个开源控件库(请点开链接,给作者点个☆),界⾯外观确实⽐⾃带的控件美观。该图是作者⽂档⾥展⽰的,虽然是再进⼀步扩展后得到的,但是界⾯已经做的挺好了。于是下载下来后,发现不怎么会⽤。加⼊作者建⽴的交流群⾥,询问了⼀下,才做到了展⽰数据这⼀步。然后发现,该控件库外观确实⽐⾃带的控件美观,但是,相应的属性和事件⽐较少,需要⾃⼰写。由于技术⽐较菜,折腾了⼀下午,也没做成想要的事件,于是⼜使⽤⾃带的DataGridView展⽰数据。(不过该控件库也没完全没发挥作⽤,下⾯⼏个按钮就是该控件库⾥的)。初步简单使⽤了⼀下,没发现什么⼤问题。⼀些功能还需要完善,因时间问题,就先写到这,毕竟这⼏天,该做什么,已经基本安排好了,时间⽐较紧,就先到这,等五⼀假期有空了,再完善。
发布评论