程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 分享C#中幾個可用的類

分享C#中幾個可用的類

編輯:C#入門知識

分享C#中幾個可用的類。本站提示廣大學習愛好者:(分享C#中幾個可用的類)文章只能為提供參考,不一定能成為您想要的結果。以下是分享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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved