C#、WinForm、ASP.NET - SQLHelper.cs

來源:互聯網
上載者:User

標籤:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.Windows.Forms;namespace Car.Command{    public static class SQLHelper    {        //擷取連接字串        public static string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;        #region ExecuteTable        /// <summary>        /// 返回相應的資料表(無參數)        /// </summary>        /// <param name="text">要執行的語句,SQL語句或者是預存程序</param>        /// <param name="type">要執行的類型,SQL語句或者是預存程序</param>        /// <returns></returns>        public static DataTable ExecuteTable(string text, CommandType type)        {            DataTable dt = new DataTable();            using (SqlConnection con = new SqlConnection(ConString))            {                try                {                    con.Open();                    SqlDataAdapter adp = new SqlDataAdapter(text, con);                    adp.SelectCommand.CommandType = type;                    adp.Fill(dt);                }                catch (Exception ex)                {                    MessageBox.Show(ex.Message, "提示");                }            }            return dt;        }        /// <summary>        /// 返回相應的資料表(重載,使用參數)        /// </summary>        /// <param name="text">要執行的語句,SQL語句或者是預存程序</param>        /// <param name="para">使用的參數</param>        /// <param name="type">要執行的類型,SQL語句或者是預存程序</param>        /// <returns></returns>        public static DataTable ExecuteTable(string text, SqlParameter[] para, CommandType type)        {            DataTable dt = new DataTable();            using (SqlConnection con = new SqlConnection(ConString))            {                try                {                    con.Open();                    SqlDataAdapter adp = new SqlDataAdapter(text, con);                    adp.SelectCommand.CommandType = type;                    adp.SelectCommand.Parameters.AddRange(para);                    adp.Fill(dt);                }                catch (Exception ex)                {                    MessageBox.Show(ex.Message, "提示");                }            }            return dt;        }         #endregion        #region ExecuteNonQuery        /// <summary>        /// 返回受影響的行數(不使用參數)        /// </summary>        /// <param name="text">要執行的SQL語句或者是預存程序</param>        /// <param name="type">執行SQL語句或者是預存程序</param>        /// <returns></returns>        public static int ExecuteNonQuery(string text, CommandType type)        {            int i = 0;            using (SqlConnection con = new SqlConnection(ConString))            {                try                {                    con.Open();                    SqlCommand cmd = new SqlCommand();                    cmd.Connection = con;                    cmd.CommandText = text;                    cmd.CommandType = type;                    i = cmd.ExecuteNonQuery();                }                catch (Exception ex)                {                    MessageBox.Show(ex.Message, "提示");                }            }            return i;        }        /// <summary>        /// 返回受影響的行數(重載,使用參數)        /// </summary>        /// <param name="text">要執行的SQL語句或者是預存程序</param>        /// <param name="para">要使用的參數</param>        /// <param name="type">執行SQL語句或者是預存程序</param>        /// <returns></returns>        public static int ExecuteNonQuery(string text, SqlParameter[] para, CommandType type)        {            int i = 0;            using (SqlConnection con = new SqlConnection(ConString))            {                try                {                    con.Open();                    SqlCommand cmd = new SqlCommand();                    cmd.Connection = con;                    cmd.CommandText = text;                    cmd.Parameters.AddRange(para);                    cmd.CommandType = type;                    i = cmd.ExecuteNonQuery();                }                catch (Exception ex)                {                    MessageBox.Show(ex.Message, "提示");                }            }            return i;        }        #endregion        #region ExcuteReader        /// <summary>        /// 返回SqlDataReader資料集(不使用參數)        /// </summary>        /// <param name="text"></param>        /// <param name="type"></param>        /// <returns></returns>        public static SqlDataReader ExcuteReader(string text, CommandType type)        {            SqlConnection con = new SqlConnection(ConString);            using (SqlCommand cmd = new SqlCommand(text, con))            {                con.Open();                cmd.CommandType = type;                SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);                return sdr;            }        }        /// <summary>        /// 重載,返回SqlDataReader資料集(使用參數)        /// </summary>        /// <param name="text"></param>        /// <param name="para"></param>        /// <param name="type"></param>        /// <returns></returns>        public static SqlDataReader ExcuteReader(string text, SqlParameter[] para, CommandType type)        {            SqlConnection con = new SqlConnection(ConString);            using (SqlCommand cmd = new SqlCommand(text, con))            {                cmd.CommandType = type;                cmd.Parameters.AddRange(para);                SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);                return sdr;            }        }         #endregion        #region ExcuteScalar        /// <summary>        /// 返回資料集第一行第一列的值        /// </summary>        /// <param name="text"></param>        /// <param name="type"></param>        /// <returns></returns>        public static object ExcuteScalar(string text, CommandType type)        {            object obj = null;            try            {                using (SqlConnection con = new SqlConnection(ConString))                {                    con.Open();                    SqlCommand cmd = new SqlCommand(text, con);                    cmd.CommandType = type;                    obj = cmd.ExecuteScalar();                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "提示");            }            return obj;        }        /// <summary>        /// 重載(返回資料集合第一行第一列的值)        /// </summary>        /// <param name="text"></param>        /// <param name="para"></param>        /// <param name="type"></param>        /// <returns></returns>        public static  object ExcuteScalar(string text, SqlParameter[] para, CommandType type)        {            object obj = null;            try            {                using (SqlConnection con = new SqlConnection(ConString))                {                    con.Open();                    SqlCommand cmd = new SqlCommand(text, con);                    cmd.CommandType = type;                    cmd.Parameters.AddRange(para);                    obj = cmd.ExecuteScalar();                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "提示");            }            return obj;        }         #endregion    }}

 

C#、WinForm、ASP.NET - SQLHelper.cs

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.