SQL Server database Access Help class

Source: Internet
Author: User

1 /// <summary>2 ///database Access Help class3 /// </summary>4  Public Static classDBHelper5 {6 Private Const stringConnStr ="server=127.0.0.1;database=db_test; Uid=user1; pwd=ps123321;";7 8 9 /// <summary>Ten ///implementation of the increase, deletion, change One /// </summary> A /// <param name= "SQL" ></param> - /// <param name= "Sqlparams" ></param> - /// <returns></returns> the  Public Static intExecuteNonQuery (stringSqlparamssqlparameter[] sqlparams) - { -     using(SqlConnection conn =NewSqlConnection (connstr)) -     { + Conn. Open (); -         using(SqlCommand comm =Conn. CreateCommand ()) +         { AComm.commandtext =SQL; atComm.commandtype =CommandType.Text; -             if(Sqlparams! =NULL) - Comm. Parameters.addrange (sqlparams); -             returnComm. ExecuteNonQuery (); -         } -     } in } -  to /// <summary> + ///1 result (s) returned - /// </summary> the /// <param name= "SQL" ></param> * /// <param name= "Sqlparams" ></param> $ /// <returns></returns>Panax Notoginseng  Public Static ObjectExecuteScalar (stringSqlparamssqlparameter[] sqlparams) - { the     using(SqlConnection conn =NewSqlConnection (connstr)) +     { A Conn. Open (); the         using(SqlCommand comm =Conn. CreateCommand ()) +         { -Comm.commandtext =SQL; $Comm.commandtype =CommandType.Text; $             if(Sqlparams! =NULL) - Comm. Parameters.addrange (sqlparams); -             returnComm. ExecuteScalar (); the         } -     }Wuyi } the  - /// <summary> Wu ///return query, return result set - /// </summary> About /// <param name= "SQL" ></param> $ /// <param name= "Sqlparams" ></param> - /// <returns></returns> -  Public StaticDataTable ExecuteQuery (stringSqlparamssqlparameter[] sqlparams) - { A     using(SqlConnection conn =NewSqlConnection (connstr)) +     { the Conn. Open (); -         using(SqlCommand comm =Conn. CreateCommand ()) $         { theComm.commandtext =SQL; theComm.commandtype =CommandType.Text; the             if(sqlparams!=NULL) the Comm. Parameters.addrange (sqlparams); -             using(SqlDataAdapter ADP =NewSqlDataAdapter (Comm)) in             { theDataSet ds =NewDataSet (); the ADP. Fill (DS); About                 returnDs. tables[0]; the             } the         } the     } + } -}

SQL Server database Access Help class

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.