分享C#中几个可用的类
作者:天尽头的那片海 发布时间:2023-08-10 10:47:17
标签:C#,类
本文实例为大家介绍了几个可用的类,供大家参考,具体内容如下
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("哈哈");
}


猜你喜欢
- 1.封装什么是封装,谈谈自己对封装的理解,封装就是将类的信息(比如说类的属性)隐藏在类的内部,不允许外部程序直接访问。此时就要提到一个关键字
- java 二分法算法的实例1、前提:二分查找的前提是需要查找的数组必须是已排序的,我们这里的实现默认为升序2、原理:将数组分为三部分,依次是
- 一、@RequestMapping@RequestMapping注解的源码:@Target({ElementType.TYPE, Eleme
- 笔者上次用C#写.Net代码差不多还是10多年以前,由于当时Java已经颇具王者风范,Net几乎被打得溃不成军。因此当时笔者对于这个.Net
- 1. Text日常最常用的应该就是显示文字,所以有必要说一下Text控件。首先源码如下:@Composablefun Text(  
- Json 是一个用于 Java 对象 和 Json 文本 相互转换的工具类。安装下载源码g
- Mybatis log printf工具网页地址: http://www.feedme.ltd/log.htmlMybatis执行的sql的
- 博主第一次安装Android Studio 3.6版本的时候就找不到R.java文件,于是在网上找个各种方法,但是都没能解决问题。注意:本博
- 本文实例讲述了Java Swing组件编程之JTable表格用法。分享给大家供大家参考,具体如下:表格是GUI编程中使用较多,但也是最麻烦的
- 在类中自定义的“函数”称为“方法”,由于C#是完全面向对象的
- 0.介绍预览针对需要在IOS手机上接入原生微信支付场景,调用微信进行支付。如图:1.资料准备1.1 账号注册打开https://open.w
- spring in action第三版读书笔记spring3.0引入了spring expression language(spel)语言,
- 1. 新建项目引入web和security包完整的pom.xml文件如下<?xml version="1.0" e
- 刚学完JDBC不久,做了一个简单的学生管理系统,可能还有不完善的地方,望各路大神见谅。废话不多说,我先贴个图让大家让大家瞅瞅,觉得是你想要的
- 介绍主要使用了goole的zxing包,下面给出了示例代码,很方便大家的理解和学习,代码都属于初步框架,功能有了,需要根据实际使用情况完善优
- Eclipse 中的 GitEclipse 附带了一个名为 Egit 的插件,它提供了一个非常完善的 Git 操作接口。 这个插件可以通过切
- Main方法如下:static void Main(string[] args){ dynamic st
- CollapsingToolbarLayout作用是提供了一个可以折叠的Toolbar,它继承至FrameLayout,给它设置layout
- Servlet简介servlet是Server Applet的简称,翻译过来就是服务程序.好吧,这么说你可能还是不太懂,简单的讲,这个ser
- 本文实例为大家分享了OpenCV Java实现人脸识别和裁剪的具体代码,供大家参考,具体内容如下安装及配置1.首先安装OpenCV,地址这里