分享C#中幾個(gè)可用的類
本文實(shí)例為大家介紹了幾個(gè)可用的類,供大家參考,具體內(nèi)容如下
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 { //用靜態(tài)的方法調(diào)用的時(shí)候不用創(chuàng)建SQLHelper的實(shí)例 //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> /// 執(zhí)行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); } //可以使用存儲(chǔ)過(guò)程的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存儲(chǔ)過(guò)程方法 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的行為加進(jìn)來(lái)。當(dāng)reader釋放資源的時(shí)候,con也被一塊關(guān)閉 return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } } public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parames) { return ExecuteDataTable(sql, CommandType.Text, parames); } //調(diào)用存儲(chǔ)過(guò)程的類,關(guān)于(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">第一個(gè)參數(shù),SQLServer語(yǔ)句</param> /// <param name="parames">第二個(gè)參數(shù),傳遞0個(gè)或者多個(gè)參數(shù)</param> /// <returns></returns> public static object ExecuteScalar(string cmdTxt, params SqlParameter[] parames) { return ExecuteScalar(cmdTxt, CommandType.Text, parames); } //可使用存儲(chǔ)過(guò)程的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(); } } } //調(diào)用存儲(chǔ)過(guò)程的DBHelper類(關(guān)于ExeceutScalar,包含事務(wù),只能處理Int類型,返回錯(cuò)誤號(hào)) 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轉(zhuǎn)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">轉(zhuǎn)換類型</typeparam> /// <param name="dataSet">數(shù)據(jù)源</param> /// <param name="tableIndex">需要轉(zhuǎn)換表的索引</param> /// <returns></returns> public List<T> DataTableToList<T>(DataTable dt) { //確認(rèn)參數(shù)有效 if (dt == null ) return null; List<T> list = new List<T>(); for (int i = 0; i < dt.Rows.Count; i++) { //創(chuàng)建泛型對(duì)象 T _t = Activator.CreateInstance<T>(); //獲取對(duì)象所有屬性 PropertyInfo[] propertyInfo = _t.GetType().GetProperties(); for (int j = 0; j < dt.Columns.Count; j++) { foreach (PropertyInfo info in propertyInfo) { //屬性名稱和列名相同時(shí)賦值 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) { //每一個(gè)row代表表中的一行,所以一行對(duì)應(yīng)一個(gè)年級(jí)對(duì)象 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> /// 消息條回調(diào)函數(shù)委托 /// </summary> public delegate void DGMsgDiv(); /// <summary> /// 消息條類 帶Timer計(jì)時(shí) /// </summary> public class MsgDiv : System.Windows.Forms.Label { private Timer timerLable = new Timer(); /// <summary> /// 消息回調(diào) 委托對(duì)象 /// </summary> private DGMsgDiv dgCallBack = null; #region 計(jì)時(shí)器 /// <summary> /// 計(jì)時(shí)器 /// </summary> public Timer TimerMsg { get { return timerLable; } set { timerLable = value; } } #endregion #region MsgDiv構(gòu)造函數(shù) /// <summary> /// MsgDiv構(gòu)造函數(shù) /// </summary> public MsgDiv() { InitallMsgDiv(7, 7); } /// <summary> /// MsgDiv構(gòu)造函數(shù) /// </summary> /// <param name="x">定位x軸坐標(biāo)</param> /// <param name="y">定位y軸坐標(biāo)</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 消息顯示 的相關(guān)參數(shù)們 hiddenClick,countNumber,constCountNumber /// <summary> /// 當(dāng)前顯示了多久的秒鐘數(shù) /// </summary> int hiddenClick = 0; /// <summary> /// 要顯示多久的秒鐘數(shù) 可變參數(shù) /// </summary> int countNumber = 3; /// <summary> /// 要顯示多久的秒鐘數(shù) 固定參數(shù) /// </summary> int constCountNumber = 3; #endregion #region 計(jì)時(shí)器 顯示countNumber秒鐘后自動(dòng)隱藏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 隱藏消息框 并停止計(jì)時(shí) +void MsgDivHidden() /// <summary> /// 隱藏消息框 并停止計(jì)時(shí) /// </summary> public void MsgDivHidden() { this.Text = ""; this.Visible = false; this.hiddenClick = 0; //this.tslblRemainSecond.Text = ""; if (this.timerLable.Enabled == true) this.timerLable.Stop(); //調(diào)用 委托 然后清空委托 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;//默認(rèn)設(shè)置顯示秒數(shù)為10; this.hiddenClick = 0;//重置倒數(shù)描述 this.timerLable.Start(); } #endregion #region 在消息框中顯示消息字符串 并在消息消失時(shí) 調(diào)用回調(diào)函數(shù) +void MsgDivShow(string msg, DGMsgDiv callback) /// <summary> /// 在消息框中顯示消息字符串 并在消息消失時(shí) 調(diào)用回調(diào)函數(shù) /// </summary> /// <param name="msg">要顯示的字符串</param> /// <param name="callback">回調(diào)函數(shù)</param> public void MsgDivShow(string msg, DGMsgDiv callback) { MsgDivShow(msg); dgCallBack = callback; } #endregion #region 在消息框中顯示消息字符串 并在指定時(shí)間消息消失時(shí) 調(diào)用回調(diào)函數(shù) +void MsgDivShow(string msg, int seconds, DGMsgDiv callback) /// <summary> /// 在消息框中顯示消息字符串 并在消息消失時(shí) 調(diào)用回調(diào)函數(shù) /// </summary> /// <param name="msg">要顯示的字符串</param> /// <param name="seconds">消息顯示時(shí)間</param> /// <param name="callback">回調(diào)函數(shù)</param> public void MsgDivShow(string msg, int seconds, DGMsgDiv callback) { MsgDivShow(msg, seconds); dgCallBack = callback; } #endregion #region 在消息框中顯示消息字符串,并指定消息框顯示秒數(shù) +void MsgDivShow(string msg, int seconds) /// <summary> /// 在消息框中顯示消息字符串,并指定消息框顯示秒數(shù) /// </summary> /// <param name="msg">要顯示的字符串</param> /// <param name="seconds">消息框顯示秒數(shù)</param> public void MsgDivShow(string msg, int seconds) { this.Text = msg; this.Visible = true; this.countNumber = seconds; this.hiddenClick = 0;//重置倒數(shù)描述 this.timerLable.Start(); } #endregion //--------------------------------------------------------------------------- #region 事件們~~~! msgDIV_MouseHover,msgDIV_MouseLeave,msgDIV_DoubleClick //當(dāng)鼠標(biāo)停留在div上時(shí) 停止計(jì)時(shí) private void msgDIV_MouseHover(object sender, EventArgs e) { if (this.timerLable.Enabled == true) this.timerLable.Stop(); } //當(dāng)鼠標(biāo)從div上移開(kāi)時(shí) 繼續(xù)及時(shí) private void msgDIV_MouseLeave(object sender, EventArgs e) { //當(dāng)消息框正在顯示、回復(fù)框沒(méi)顯示、計(jì)時(shí)器正停止的時(shí)候,重新啟動(dòng)計(jì)時(shí)器 if (this.Visible == true && this.timerLable.Enabled == false) this.timerLable.Start(); } //雙擊消息框時(shí)關(guān)閉消息框 private void msgDIV_DoubleClick(object sender, EventArgs e) { MsgDivHidden(); } #endregion }
例如:
private void Form1_Load(object sender, EventArgs e) { //首先顯示“呵呵”,3秒后 調(diào)用Test方法消息框顯示“哈哈” msgDiv1.MsgDivShow("呵呵",3,Test); } public void Test() { MessageBox.Show("哈哈"); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
欄 目:C#教程
下一篇:C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)
本文標(biāo)題:分享C#中幾個(gè)可用的類
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6509.html
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開(kāi)的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
- 01-10C#中DataGridView常用操作實(shí)例小結(jié)
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(shí)的邊框效果
- 01-10C#中查找Dictionary中的重復(fù)值的方法
- 01-10C#中TreeView實(shí)現(xiàn)適合兩級(jí)節(jié)點(diǎn)的選中節(jié)點(diǎn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?