C #, WinForm, Asp.net-sqlhelper.cs

Source: Internet
Author: User
Tags static class

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 {//Get connection string public static string constring = Configurationm Anager. connectionstrings["Constring"].        ConnectionString; #region executetable///<summary>///return the corresponding data sheet (no parameters)///</summary>//<param        Name= "text" > statement to execute, SQL statement or stored procedure </param>//<param name= "type" > Type to execute, SQL statement or stored procedure </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, "hint");        }} return DT; }///<summary>///return the corresponding data table (overload, use parameters)///</summary>//<param name= "text" > Statements to execute, SQL statements or stored procedures </param>///<param name= "para" > Parameters used </param>//<param name= "type" > Type to execute, SQL statement or stored procedure </param>//<returns></returns> public static DataTable Executetab            Le (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, "hint");        }} return DT; #endregion #region ExecuteNonQuery//<summary>///////</ summary>//<param name= "text" > SQL statement to execute or stored procedure </param>//<param name= "type" > Execute SQL statement Or a stored procedure </param>///<returns></returns> public static int ExecuteNonQuery (string text, Com            Mandtype 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, "hint");        }} return I; }///<summary>///Returns the number of rows affected (overloaded, using parameters)///</summary>//<param name= "text" > SQL statement to execute or stored procedure </param>///<param name= "para" > Parameters to use </param>//<param name= "type" &G t; Execute SQL statement or stored procedure </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, "hint");        }} return I;  } #endregion #region Excutereader//<summary>////Return SqlDataReader DataSet (without parameters)//        </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>//Reload, return SqlDataReader data set (using parameters)///</summary>//&LT;PA Ram name= "text" ></param>//<param name= "para" ></param>//<param name= "type" >& lt;/param>//<returns></returns> public static SqlDataReader Excutereader (string text, SQLP            Arameter[] 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>///Returns the value of the first column in the first row of the dataset// lt;/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, "hint");        } return obj; }//<summary>///Reload (returns the value of the first column in the first row of the data collection)///</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, "hint");        } return obj; } #endregion}}

C #, WinForm, Asp.net-sqlhelper.cs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.