SQL additions and deletions to seal installation

Source: Internet
Author: User
Tags connectionstrings

App. Config file

1<?xml version="1.0"encoding="Utf-8"?>2<configuration>3<startup>4<supportedruntime version="v4.0"sku=". netframework,version=v4.5"/>5</startup>6<connectionStrings>7<add connectionstring="Data Source =.; Initial Catalog = mysql; Integrated Security = True;"Name="Constr"/>8</connectionStrings>9</configuration>

SqlHelper Encapsulation Class Library code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 //Create a new App.confit file that references the following two namespaces7 usingSystem.Configuration;8 usingSystem.Data.SqlClient;9 Ten namespacePackage One { A      Public classSqlHelper -     { -         //reading the connection string the         Private Static ReadOnly stringstr = configurationmanager.connectionstrings["Constr"]. ConnectionString; -         //three methods, the key is the third method, query, conditional judgment more -         /// <summary> -         ///This method can do additions and deletions to change +         /// </summary> -         /// <param name= "SQL" >SQL statements</param> +         /// <param name= "PS" >parameters in the SQL statement</param> A         /// <returns>returns the number of rows affected, int type</returns> at         Private Static intExecuteNonQuery (stringSqlparamssqlparameter[] PS) -         { -             using(SqlConnection con =NewSqlConnection (str)) -             { -                 using(SqlCommand cmd =NewSqlCommand (sql, con)) -                 { in con. Open (); -                     if(PS! =NULL) to                     { + cmd. Parameters.addrange (PS); -                     } the  *                     returncmd. ExecuteNonQuery (); $                 }Panax Notoginseng             } -         } the         /// <summary> +         ///This method is used on the query A         /// </summary> the         /// <param name= "SQL" >SQL statements</param> +         /// <param name= "PS" >parameters in the SQL statement</param> -         /// <returns>returns the first row of columns, object type</returns> $         Private Static ObjectExecuteScalar (stringSqlparamssqlparameter[] PS) $         { -             using(SqlConnection con =NewSqlConnection (str)) -             { the                 using(SqlCommand cmd =NewSqlCommand (sql, con)) -                 {Wuyi con. Open (); the                     if(PS! =NULL) -                     { Wu cmd. Parameters.addrange (PS); -                     } About                     returncmd. ExecuteScalar (); $                 } -             } -         } -         /// <summary> A         ///This method is used for querying, reading data +         /// </summary> the         /// <param name= "SQL" >SQL statements</param> -         /// <param name= "PS" >parameters in the SQL statement</param> $         /// <returns>returns the SqlDataReader object with data</returns> the         Private StaticSqlDataReader ExecuteReader (stringSqlparamssqlparameter[] PS) the         { the             //This place doesn't have a using, because I don't know when to release theSqlConnection con =NewSqlConnection (str); -             using(SqlCommand cmd =NewSqlCommand (Sql,con)) in             { the                 if(ps!=NULL) the                 { About cmd. Parameters.addrange (PS); the                 } the                 Try the                 { + con. Open (); -                     //This overload will automatically close when DataReader is closed connection the                     returncmd. ExecuteReader (System.Data.CommandBehavior.CloseConnection);Bayi                 } the                 Catch(Exception ex) the                 { - con. Close (); - con. Dispose (); the                     Throwex; the                 } the             } the         } -     } the}

SQL additions and deletions to seal installation

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.