Write your own public class to manipulate SQL

Source: Internet
Author: User

    /*/' \/'.   .      .‘ :  `.              `.        \\.‘              ,  `.`  `.     `. ,___/|\.   `. :             . \, .‘.  / ‘ ‘\ , ‘ .\ .    \_.~ _; ;             \/‘.   `\ ..._`.   :   /.. .. / /‘ _._ \.   ~. ' ' \:/'. '    @   `    .---.          `.  .‘           : ' @ '. \. /   ./`..      _./ ~ .   :\    `.   __ .‘  /(\ .....)  . '/'.    /‘‘‘\ .‘  `.    / \ : ;‘ .‘   ..:.‘ ;       `\;   : : : : .‘    :  ; ::     `\.    `\.   ;     : \.‘ " ‘ ;      `.   `.   \/S.     /       `.  .‘   `    .  `.     `\ `.  ;   /‘ ;___ ;   `.  `.   `.          `         ;     ;:__..‘   `. `.     `.    : ': _. '   .‘ ;     :       `.    `.   . \x./-'--...../'; :         `. .. -:..                     -' (: '---'.   `;  :             `.                   `,..    :    :               `.                  `. `.___;    `.                   `.    `.                     `;        '-., ' goddess bless never bug*/public static class SQLHelper {private static SqlConnection Con;        private static SqlCommand cmd;        private static DataTable DT;        private static DataSet DS;        private Static SqlDataAdapter da;        private static string sqlstr;        private static void SQLHelper () {sqlstr = ""; }///<summary>///execute an SQL statement that returns the first value of the first row///</summary>//<param name= "SQL"            ;</param>//<returns></returns> public static Object Getfirstvalue (String sql) {                using (Con = new SqlConnection (sqlstr)) {Con.open ();                cmd = new SqlCommand (sql, Con); return CMD.            ExecuteScalar ();        }}///<summary>///execute an SQL statement that returns the number of rows affected///</summary><param name= "SQL" ></param>///<returns></returns> public static int Getnonquery        (String sql)                {using (Con = new SqlConnection (sqlstr)) {Con.open ();                cmd = new SqlCommand (sql, Con); return CMD.            ExecuteNonQuery (); }}///<summary>///Offline mode populate DataTable and return, data can be used this method///</summary>//<p Aram name= "SQL" ></param>///<returns></returns> public static DataTable Getdatatablea ( String sql) {using (Con = new SqlConnection (sqlstr)) {da = new sqldataadapte                R (SQL, Con);                DT = new DataTable (); Da.                Fill (DT);            return DT; }}///<summary>///based on SQL, populate DataTable and return. You can use this method when you have more data.//</summary>//<param name= "SQL" ></param>//<returns></returns> public static DataTable Getdatatableb (String sql) {using (Con = new Sqlco                Nnection (SQLSTR)) {Con.open ();                cmd = new SqlCommand (sql, Con);                DT = new DataTable (); SqlDataReader reader = cmd.                ExecuteReader (); Dt.                Load (reader);                Con.close ();            return DT; }}///<summary>///Incoming SQL statement set, return a DataSet///</summary>//<param Nam E= "Sqllist" ></param>///<returns></returns> public static DataSet GetDataSet (list<s                Tring> sqllist) {using (Con = new SqlConnection (sqlstr)) {Con.open ();                ds = new DataSet ();                    foreach (string C in sqllist) {cmd = new SqlCommand (c, Con);                    DT = new DataTable (); SqldatareaDer reader = cmd.                    ExecuteReader (); Dt.                    Load (reader); Ds.                Tables.add (DT);                } con.close ();            return DS; }}////<summary> Execute multiple SQL statements in a transaction///</summary>//<param name= "list "></param>///<returns></returns> public static bool Execsqlslist (list<string> l                IST) {using (Con = new SqlConnection (sqlstr)) {Con.open ();                SqlTransaction tran = Con.begintransaction ();  try {foreach (String sql in list) {cmd = new                        SqlCommand (SQL, Con); Cmd.                    ExecuteNonQuery ();                    } tran.commit ();                    Con.close ();                return true; } catch {TRan.                    Rollback ();                return false;                    } finally {con.close ();                               Con.dispose (); }}}///<summary> execute a stored procedure with no parameters, return datable//&lt         ;/summary>//<param name= "StoredProcedureName" ></param>///<returns></returns>  public static DataTable Getdatatablebystoredprocedure (string storedprocedurename) {using (Con =                New SqlConnection (SQLSTR)) {Con.open ();                cmd = new SqlCommand ();                Cmd.commandtext = StoredProcedureName;                Cmd.commandtype = CommandType.StoredProcedure;                DT = new DataTable (); Dt. Load (cmd.                ExecuteReader ());            return DT; }        }    }

Spare ~

Write your own public class to manipulate SQL

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.