Database 2_sqlHelper and sqlhelper access the database

Source: Internet
Author: User

Database 2_sqlHelper and sqlhelper access the database

Encapsulate an affected row

1 public static int ExcuteNonQuery (string sqlText, params SqlParameter [] parameters) 2 {3 using (SqlConnection conn = new SqlConnection (GetSqlConnectionString ())) 4 {5 using (SqlCommand cmd = conn. createCommand () 6 {7 conn. open (); 8 cmd. commandText = sqlText; 9 cmd. parameters. addRange (parameters); // Add the parameter to the cmd command. 10 return cmd. ExecuteNonQuery (); 11} 12} 13}

Execute SQL. Returns the value of the first column in the first row of the query result.

1 # region Execute SQL. Returns the value of 2 3 public static object ExcuteScalar (string sqlText, params SqlParameter [] parameters) in the first column of the First row in the query result) 4 {5 using (SqlConnection conn = new SqlConnection (GetSqlConnectionString () 6 {7 using (SqlCommand cmd = conn. createCommand () 8 {9 conn. open (); 10 cmd. commandText = sqlText; 11 cmd. parameters. addRange (parameters); 12 return cmd. executeScalar (); 13} 14} 15} 16 17 public static T ExcuteScalar <T> (string sqlText, params SqlParameter [] parameters) 18 // where T: UserInfo must inherit a certain type 19 // where T: class // must be class 20 // where T: new () // requires T to have a default constructor 21 22 // where can be of the T type, must be a class, A constructor is required. A class or interface must be inherited or implemented. 23 {24 using (SqlConnection conn = new SqlConnection (GetSqlConnectionString () 25 {26 using (SqlCommand cmd = conn. createCommand () 27 {28 conn. open (); 29 cmd. commandText = sqlText; 30 cmd. parameters. addRange (parameters); 31 return (T) cmd. executeScalar (); 32 // int? I = 0; 33 // object num = I; 34 // I = num as int ?; 35} 36} 37} 38 # endregion

Execute SQL to return a DataTable

 1   public static DataTable ExcuteDataTable(string sqlText, params SqlParameter[] parameters) 2         { 3             using (SqlDataAdapter adapter = new SqlDataAdapter(sqlText, GetSqlConnectionString())) 4             { 5                 DataTable dt =new DataTable(); 6                 adapter.SelectCommand.Parameters.AddRange(parameters); 7                 adapter.Fill(dt); 8                 return dt; 9             }10         }

Execute the SQL script and return a SqlDataReader

1 public static SqlDataReader ExucteReader (string sqlText, params SqlParameter [] parameters) 2 {3 // SqlDataReader requires that it exclusively occupies its SqlConnection object when reading data, sqlConnection must be in the Open state. 4 SqlConnection conn = new SqlConnection (GetSqlConnectionString (); // do not release the connection because the connection must be enabled later. 5 SqlCommand cmd = conn. createCommand (); 6 conn. open (); 7 cmd. commandText = sqlText; 8 cmd. parameters. addRange (parameters); 9 // CommandBehavior. closeConnection: indicates that when SqlDataReader is released, the SqlConnection object is also released. 10 return cmd. ExecuteReader (CommandBehavior. CloseConnection );

 

Also assigned this piece is also can be referred to, the above is based on EF to determine the http://www.cnblogs.com/lishuangzhe/archive/2013/08/26/SqlHelper.html

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.