分享C#中几个可用的类
程序员文章站
2022-06-05 14:14:55
本文实例为大家介绍了几个可用的类,供大家参考,具体内容如下
1.sqlhelper类
using system;
using system.collectio...
本文实例为大家介绍了几个可用的类,供大家参考,具体内容如下
1.sqlhelper类
using system; using system.collections.generic; using system.linq; using system.text; using system.data.sqlclient; using system.data; using system.configuration; namespace myschool.dal { public static class sqlhelper { //用静态的方法调用的时候不用创建sqlhelper的实例 //execetenonquery // public static string constr = "server=happypig\\sqlmodel;database=shooltest;uid=sa;"; public static string constr = configurationmanager.connectionstrings["constr"].connectionstring; public static int id; /// <summary> /// 执行nonquery命令 /// </summary> /// <param name="cmdtxt"></param> /// <param name="parames"></param> /// <returns></returns> public static int executenonquery(string cmdtxt, params sqlparameter[] parames) { return executenonquery(cmdtxt, commandtype.text, parames); } //可以使用存储过程的executenonquery public static int executenonquery(string cmdtxt, commandtype cmdtype, params sqlparameter[] parames) { //判断脚本是否为空 ,直接返回0 if (string.isnullorempty(cmdtxt)) { return 0; } using (sqlconnection con = new sqlconnection(constr)) { using (sqlcommand cmd = new sqlcommand(cmdtxt, con)) { if (parames != null) { cmd.commandtype = cmdtype; cmd.parameters.addrange(parames); } con.open(); return cmd.executenonquery(); } } } public static sqldatareader executedatareader(string cmdtxt, params sqlparameter[] parames) { return executedatareader(cmdtxt, commandtype.text, parames); } //sqldatareader存储过程方法 public static sqldatareader executedatareader(string cmdtxt, commandtype cmdtype, params sqlparameter[] parames) { if (string.isnullorempty(cmdtxt)) { return null; } sqlconnection con = new sqlconnection(constr); using (sqlcommand cmd = new sqlcommand(cmdtxt, con)) { cmd.commandtype = cmdtype; if (parames != null) { cmd.parameters.addrange(parames); } con.open(); //把reader的行为加进来。当reader释放资源的时候,con也被一块关闭 return cmd.executereader(system.data.commandbehavior.closeconnection); } } public static datatable executedatatable(string sql, params sqlparameter[] parames) { return executedatatable(sql, commandtype.text, parames); } //调用存储过程的类,关于(executedatatable) public static datatable executedatatable(string sql, commandtype cmdtype, params sqlparameter[] parames) { if (string.isnullorempty(sql)) { return null; } datatable dt = new datatable(); using (sqldataadapter da = new sqldataadapter(sql, constr)) { da.selectcommand.commandtype = cmdtype; if (parames != null) { da.selectcommand.parameters.addrange(parames); } da.fill(dt); return dt; } } /// <summary> /// executescalar /// </summary> /// <param name="cmdtxt">第一个参数,sqlserver语句</param> /// <param name="parames">第二个参数,传递0个或者多个参数</param> /// <returns></returns> public static object executescalar(string cmdtxt, params sqlparameter[] parames) { return executescalar(cmdtxt, commandtype.text, parames); } //可使用存储过程的executescalar public static object executescalar(string cmdtxt, commandtype cmdtype, params sqlparameter[] parames) { if (string.isnullorempty(cmdtxt)) { return null; } using (sqlconnection con = new sqlconnection(constr)) { using (sqlcommand cmd = new sqlcommand(cmdtxt, con)) { cmd.commandtype = cmdtype; if (parames != null) { cmd.parameters.addrange(parames); } con.open(); return cmd.executescalar(); } } } //调用存储过程的dbhelper类(关于execeutscalar,包含事务,只能处理int类型,返回错误号) public static object executescalar(string cmdtxt, commandtype cmdtype,sqltransaction sqltran, params sqlparameter[] parames) { if (string.isnullorempty(cmdtxt)) { return 0; } using (sqlconnection con = new sqlconnection(constr)) { int sum = 0; using (sqlcommand cmd = new sqlcommand(cmdtxt, con)) { cmd.commandtype=cmdtype; if (parames != null) { cmd.parameters.addrange(parames); } con.open(); sqltran = con.begintransaction(); try { cmd.transaction = sqltran; sum=convert.toint32( cmd.executescalar()); sqltran.commit(); } catch (sqlexception ex) { sqltran.rollback(); } return sum; } } } } }
例如:
//以返回表的方式加载下拉框 public datatable loadcombox() { string sql = "select * from grade"; datatable dt = sqlhelper.executedatatable(sql); return dt; }
2.mytool类(datatable转list<>)
using system; using system.collections.generic; using system.data; using system.data.sqlclient; using system.linq; using system.reflection; using system.text; using system.threading.tasks; namespace myschool.dal { public class mytool { /// <summary> /// datasettolist /// </summary> /// <typeparam name="t">转换类型</typeparam> /// <param name="dataset">数据源</param> /// <param name="tableindex">需要转换表的索引</param> /// <returns></returns> public list<t> datatabletolist<t>(datatable dt) { //确认参数有效 if (dt == null ) return null; list<t> list = new list<t>(); for (int i = 0; i < dt.rows.count; i++) { //创建泛型对象 t _t = activator.createinstance<t>(); //获取对象所有属性 propertyinfo[] propertyinfo = _t.gettype().getproperties(); for (int j = 0; j < dt.columns.count; j++) { foreach (propertyinfo info in propertyinfo) { //属性名称和列名相同时赋值 if (dt.columns[j].columnname.toupper().equals(info.name.toupper())) { if (dt.rows[i][j] != dbnull.value) { info.setvalue(_t, dt.rows[i][j], null); } else { info.setvalue(_t, null, null); } break; } } } list.add(_t); } return list; } } }
例如:
public list<grade> loadcombox2() { string sql = "select * from grade"; datatable dt = sqlhelper.executedatatable(sql); //方法一: foreach (datarow row in dt.rows) { //每一个row代表表中的一行,所以一行对应一个年级对象 grade grade = new grade(); grade.gradeid = convert.toint32(row["gradeid"]); grade.gradename = row["gradename"].tostring(); list.add(grade); } //方法二:(使用mytool类) mytool tool=new mytool(); list = tool.datatabletolist<grade>(dt); return list; }
3.dgmsgdiv类(可生成自己的控件)
using system; using system.collections.generic; using system.text; using system.windows.forms; /// <summary> /// 消息条回调函数委托 /// </summary> public delegate void dgmsgdiv(); /// <summary> /// 消息条类 带timer计时 /// </summary> public class msgdiv : system.windows.forms.label { private timer timerlable = new timer(); /// <summary> /// 消息回调 委托对象 /// </summary> private dgmsgdiv dgcallback = null; #region 计时器 /// <summary> /// 计时器 /// </summary> public timer timermsg { get { return timerlable; } set { timerlable = value; } } #endregion #region msgdiv构造函数 /// <summary> /// msgdiv构造函数 /// </summary> public msgdiv() { initallmsgdiv(7, 7); } /// <summary> /// msgdiv构造函数 /// </summary> /// <param name="x">定位x轴坐标</param> /// <param name="y">定位y轴坐标</param> public msgdiv(int x, int y) { initallmsgdiv(x, y); } #endregion #region 初始化消息条 /// <summary> /// 初始化消息条 /// </summary> private void initallmsgdiv(int x, int y) { this.autosize = true; this.backcolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); this.borderstyle = system.windows.forms.borderstyle.fixedsingle; //this.contextmenustrip = this.cmslist; this.font = new system.drawing.font("宋体", 11f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134))); this.forecolor = system.drawing.color.red; this.location = new system.drawing.point(x, y); this.maximumsize = new system.drawing.size(980, 525); this.name = "msgdiv"; this.padding = new system.windows.forms.padding(7); this.size = new system.drawing.size(71, 31); this.tabindex = 1; this.text = "消息条"; this.visible = false; //给委托添加事件 this.doubleclick += new system.eventhandler(this.msgdiv_doubleclick); this.mouseleave += new system.eventhandler(this.msgdiv_mouseleave); this.mousehover += new system.eventhandler(this.msgdiv_mousehover); this.timerlable.interval = 1000; this.timerlable.tick += new system.eventhandler(this.timerlable_tick); } #endregion #region 将消息条添加到指定容器上 /// <summary> /// 将消息条添加到指定容器上form /// </summary> /// <param name="form"></param> public void addtocontrol(form form) { form.controls.add(this); } /// <summary> /// 将消息条添加到指定容器上groupbox /// </summary> /// <param name="form"></param> public void addtocontrol(groupbox groupbox) { groupbox.controls.add(this); } /// <summary> /// 将消息条添加到指定容器上panel /// </summary> /// <param name="form"></param> public void addtocontrol(panel panel) { panel.controls.add(this); } #endregion //--------------------------------------------------------------------------- #region 消息显示 的相关参数们 hiddenclick,countnumber,constcountnumber /// <summary> /// 当前显示了多久的秒钟数 /// </summary> int hiddenclick = 0; /// <summary> /// 要显示多久的秒钟数 可变参数 /// </summary> int countnumber = 3; /// <summary> /// 要显示多久的秒钟数 固定参数 /// </summary> int constcountnumber = 3; #endregion #region 计时器 显示countnumber秒钟后自动隐藏div -timerlable_tick(object sender, eventargs e) private void timerlable_tick(object sender, eventargs e) { if (hiddenclick > countnumber - 2) { msgdivhidden(); } else { hiddenclick++; //remaincount(); } } #endregion #region 隐藏消息框 并停止计时 +void msgdivhidden() /// <summary> /// 隐藏消息框 并停止计时 /// </summary> public void msgdivhidden() { this.text = ""; this.visible = false; this.hiddenclick = 0; //this.tslblremainsecond.text = ""; if (this.timerlable.enabled == true) this.timerlable.stop(); //调用 委托 然后清空委托 if (dgcallback != null && dgcallback.getinvocationlist().length > 0) { dgcallback(); dgcallback -= dgcallback; } } #endregion #region 在消息框中显示消息字符串 +void msgdivshow(string msg) /// <summary> /// 在消息框中显示消息字符串 /// </summary> /// <param name="msg">要显示的字符串</param> public void msgdivshow(string msg) { this.text = msg; this.visible = true; this.countnumber = constcountnumber;//默认设置显示秒数为10; this.hiddenclick = 0;//重置倒数描述 this.timerlable.start(); } #endregion #region 在消息框中显示消息字符串 并在消息消失时 调用回调函数 +void msgdivshow(string msg, dgmsgdiv callback) /// <summary> /// 在消息框中显示消息字符串 并在消息消失时 调用回调函数 /// </summary> /// <param name="msg">要显示的字符串</param> /// <param name="callback">回调函数</param> public void msgdivshow(string msg, dgmsgdiv callback) { msgdivshow(msg); dgcallback = callback; } #endregion #region 在消息框中显示消息字符串 并在指定时间消息消失时 调用回调函数 +void msgdivshow(string msg, int seconds, dgmsgdiv callback) /// <summary> /// 在消息框中显示消息字符串 并在消息消失时 调用回调函数 /// </summary> /// <param name="msg">要显示的字符串</param> /// <param name="seconds">消息显示时间</param> /// <param name="callback">回调函数</param> public void msgdivshow(string msg, int seconds, dgmsgdiv callback) { msgdivshow(msg, seconds); dgcallback = callback; } #endregion #region 在消息框中显示消息字符串,并指定消息框显示秒数 +void msgdivshow(string msg, int seconds) /// <summary> /// 在消息框中显示消息字符串,并指定消息框显示秒数 /// </summary> /// <param name="msg">要显示的字符串</param> /// <param name="seconds">消息框显示秒数</param> public void msgdivshow(string msg, int seconds) { this.text = msg; this.visible = true; this.countnumber = seconds; this.hiddenclick = 0;//重置倒数描述 this.timerlable.start(); } #endregion //--------------------------------------------------------------------------- #region 事件们~~~! msgdiv_mousehover,msgdiv_mouseleave,msgdiv_doubleclick //当鼠标停留在div上时 停止计时 private void msgdiv_mousehover(object sender, eventargs e) { if (this.timerlable.enabled == true) this.timerlable.stop(); } //当鼠标从div上移开时 继续及时 private void msgdiv_mouseleave(object sender, eventargs e) { //当消息框正在显示、回复框没显示、计时器正停止的时候,重新启动计时器 if (this.visible == true && this.timerlable.enabled == false) this.timerlable.start(); } //双击消息框时关闭消息框 private void msgdiv_doubleclick(object sender, eventargs e) { msgdivhidden(); } #endregion }
例如:
private void form1_load(object sender, eventargs e) { //首先显示“呵呵”,3秒后 调用test方法消息框显示“哈哈” msgdiv1.msgdivshow("呵呵",3,test); } public void test() { messagebox.show("哈哈"); }
以上就是本文的全部内容,希望对大家的学习有所帮助。
上一篇: 女性朋友经期可以喝普洱茶吗?
下一篇: C#实现Winform版计算器