Okay, knock code-sqlhelper.

Source: Internet
Author: User

Idle to Nothing, and SqlHelper knocked again.

In general, each programmer will prefer to write with their own sqlhelper, use it handy, hehe.

 Public classSqlHelper { Public Static ReadOnly stringstr = configurationmanager.connectionstrings["Constr"].               ConnectionString; /// <summary>        ///This method can be used with SQL statements or stored procedures///executes Transact-SQL statements on the connection and returns the number of rows affected. /// </summary>        /// <param name= "SQL" ></param>        /// <param name= "ComType" >CommandType</param>        /// <param name= "Para" >variable Parameters</param>        /// <returns>the number of rows affected. </returns>         Public Static intExecuteNonQuery (stringSQL, CommandType ComType,paramsSqlparameter[]para) {            using(SqlConnection con=NewSqlConnection (str)) {                using(SqlCommand cmd=NewSqlCommand (Sql,con)) {con.                    Open (); if(para!=NULL) {cmd.                    Parameters.addrange (para); } Cmd.commandtype=ComType; returncmd.                ExecuteNonQuery (); }            }        }        /// <summary>        ///This method can be used with SQL statements or stored procedures///executes the query and returns the first column of the first row in the result set returned by the query.        Other columns or rows are ignored. /// </summary>        /// <param name= "SQL" ></param>        /// <param name= "ComType" ></param>        /// <param name= "para" ></param>        /// <returns>The first column of the first row in the result set, or a null reference if the result set is empty (Nothing in Visual Basic). The maximum number of characters returned is 2033 characters. </returns>         Public Static ObjectExecuteScalar (stringSQL, CommandType ComType,paramssqlparameter[] para) {            using(SqlConnection con =NewSqlConnection (str)) {                using(SqlCommand cmd =NewSqlCommand (sql, con)) {con.                    Open (); if(Para! =NULL) {cmd.                    Parameters.addrange (para); } Cmd.commandtype=ComType; returncmd.                ExecuteScalar (); }            }        }        /// <summary>        ///executes the query and returns the first column of the first row in the result set returned by the query.        Other columns or rows are ignored. ///send the System.Data.SqlClient.SqlCommand.CommandText to System.Data.SqlClient.SqlCommand.Connection and use the/// </summary>        /// <param name= "SQL" ></param>        /// <param name= "ComType" ></param>        /// <param name= "para" ></param>        /// <returns>The first column of the first row in the result set, or a null reference if the result set is empty (Nothing in Visual Basic). The maximum number of characters returned is 2033 characters. A System.Data.SqlClient.SqlDataReader object. </returns>         Public StaticSqlDataReader ExecuteReader (stringSQL, CommandType ComType,paramssqlparameter[] para) {SqlConnection con=NewSqlConnection (str); using(SqlCommand cmd=NewSqlCommand (Sql,con)) {                if(Para! =NULL) {cmd.                Parameters.addrange (para); }                Try{con.                    Open (); Cmd.commandtype=ComType; returncmd.                ExecuteReader (commandbehavior.closeconnection); }                Catch(Exception) {con.                    Close (); Con.                    Dispose (); Throw; }            }                  }        /// <summary>        ///Query by Saldataadapter/// </summary>        /// <param name= "SQL" ></param>        /// <param name= "param" ></param>        /// <returns>The return is a table</returns>        Public StaticDataTable GetTable (stringSqlparamssqlparameter[] param) {DataTable dt=NewDataTable (); using(SqlConnection con =NewSqlConnection (str)) {                using(SqlCommand cmd =NewSqlCommand (sql, con)) {cmd.                    Parameters.addrange (param); using(SqlDataAdapter SDA =NewSqlDataAdapter (CMD)) {SDA.                    Fill (DT); }                }            }            returnDT; }        /// <summary>        ///Query by Saldataadapter/// </summary>        /// <param name= "SQL" ></param>        /// <param name= "param" ></param>        /// <returns>The return is a table</returns>         Public StaticDataTable GetTable (stringSqlparamssqlparameter[] param) {DataTable dt=NewDataTable (); using(SqlConnection con =NewSqlConnection (str)) {                using(SqlCommand cmd =NewSqlCommand (sql, con)) {cmd.                    Parameters.addrange (param); using(SqlDataAdapter SDA =NewSqlDataAdapter (CMD)) {SDA.                    Fill (DT); }                }            }            returnDT; }    }

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.