"ADO" 6, SqlHelper simple Package

Source: Internet
Author: User
Tags connectionstrings

Using System.Data.SqlClient;
Using System.Configuration;
Reference: System.Configuration
The connection string is placed in the configuration file

Create a new class and write the following methods to manipulate the data

//ExecuteNonQuery Construction Public Static intExecuteNonQuery (stringSqlparamssqlparameter[] Parameters) {    stringSTR = configurationmanager.connectionstrings["ConnStr"].    ConnectionString; using(SqlConnection conn =NewSqlConnection (STR)) {Conn.    Open (); using(SqlCommand cmd =Conn. CreateCommand ()) {Cmd.commandtext=SQL; foreach(SqlParameter parinchparameters) {cmd.        Parameters.Add (PAR); }        returncmd.    ExecuteNonQuery (); }    }    }//use of the ExecuteNonQuery methodPrivate voidButton1_Click (Objectsender, EventArgs e)//Click events{    Try{sqlhelper.executenonquery ("INSERT INTO T_age (name,age) VALUES (@n,@a)",NewSqlParameter ("@N", TxtLogin.Text.Trim ()),NewSqlParameter ("@A", TxtAge.Text.Trim ())); MessageBox.Show ("Insert Success! "); }    Catch(Exception ex) {MessageBox.Show (string. Format ("Insert failed! : {0}", ex)); return; }}//-----------------------------------------------------------------------------//ExecuteScalar Construction Public Static ObjectExeccutescalar (stringSqlparamssqlparameter[] Parameters) {    stringSTR = configurationmanager.connectionstrings["ConnStr"].    ConnectionString; using(SqlConnection conn =NewSqlConnection (STR)) {Conn.    Open (); SqlCommand cmd=Conn.    CreateCommand (); Cmd.commandtext=SQL; foreach(SqlParameter parinchparameters) {cmd.     Parameters.Add (PAR); }     returncmd.    ExecuteScalar (); }}//use of the ExecuteScalar methodPrivate voidButton2_Click (Objectsender, EventArgs e)//Click events{   inti = Convert.ToInt32 (Sqlhelper.execcutescalar ("Select COUNT (*) T_phone")); MessageBox.Show (Convert.ToString (i));//-----------------------------------------------------------------------------//ExecuteReader Construction//You cannot use the using Public StaticSqlDataReader SqlDataReader (stringSqlparamssqlparameter[] Parameters) {    stringSTR = configurationmanager.connectionstrings["ConnStr"].    ConnectionString; SqlConnection Conn=NewSqlConnection (STR); Conn.    Open (); SqlCommand cmd=Conn.    CreateCommand (); Cmd.commandtext=SQL; foreach(SqlParameter parinchparameters) {cmd.    Parameters.Add (PAR); } SqlDataReader Dr=cmd.    ExecuteReader (commandbehavior.closeconnection); Cmd.    Parameters.clear (); returnDr;}//use of the ExecuteReader methodPrivate voidButton4_Click (Objectsender, EventArgs e)//Click events{SqlDataReader Dr= Sqlhelper.sqldatareader_using ("SELECT * from T_age");  while(Dr. Read ()) {MessageBox.Show (convert.tostring (dr["Name"]));//read the value of the Name field  }   }//-----------------------------------------------------------------------------//the construction of ExecuteDataset Public StaticDataTable ExecuteDataset (stringSqlparamssqlparameter[] sqlparmeter) {      stringSTR = configurationmanager.connectionstrings["ConnStr"].     ConnectionString; using(SqlConnection conn =NewSqlConnection (STR)) {Conn.     Open (); SqlCommand cmd=Conn.     CreateCommand (); Cmd.commandtext=SQL; foreach(SqlParameter parinchSqlparmeter)//traversing increases the Sqlparmeter parameter{cmd.     Parameters.Add (PAR); } DataSet DataSet=NewDataSet (); SqlDataAdapter Adapter=NewSqlDataAdapter (CMD); Adapter.     Fill (DataSet); returnDataSet. tables[0]; }}//use of the ExecuteDataset methodPrivate voidButton5_click (Objectsender, EventArgs e) {DataTable dt= SqlHelper.ExecuteDataset ("SELECT * from T_age");  for(inti =0; i < dt. Rows.Count; i++) {DataRow row=dt.    Rows[i]; stringName =convert.tostring (row["Name"]);    MessageBox.Show (name); }}//-----------------------------------------------------------------------------Note: SqlDataReader If you useusing, it will not be executed because the error code cannot be read when the data is read by using the following Public StaticSqlDataReader executereader_using (stringSqlparamssqlparameter[] Parameters) {    stringSTR = configurationmanager.connectionstrings["ConnStr"].    ConnectionString; using(SqlConnection conn =NewSqlConnection (STR)) {Conn.    Open (); SqlCommand cmd=Conn.    CreateCommand (); Cmd.commandtext=SQL; foreach(SqlParameter parinchparameters) {cmd.    Parameters.Add (PAR); }    returncmd.    ExecuteReader (); }} //-----------------------------------------------------------------------------

"ADO" 6, SqlHelper simple Package

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.