SQLHELPER Help Class

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace reading of the database
{
Using System.Data;
Using System.Data.SqlClient;
Class SQLHelper
{

static string sqlconn = system.configuration.configurationmanager.connectionstrings["Sqlconn"]. ConnectionString;

<summary>
Executes the query result returns the first column value of the first column
</summary>
public static Object ExecuteScalar (String sql, params sqlparameter[] sp)
{
using (SqlConnection conn =new SqlConnection (sqlconn))
{
Conn. Open ();
SqlCommand comm = new SqlCommand (SQL, conn);
Comm. Parameters.addrange (SP);
Return COMM. ExecuteScalar ();
}

}

<summary>
return table Default
</summary>
<param name= "SQL" ></param>
<param name= "SP" ></param>
<returns></returns>
public static DataTable GetTable (String sql, sqlparameter[] sp)
{
using (SqlConnection conn =new SqlConnection (sqlconn))
{
Conn. Open ();
SqlDataAdapter da = new SqlDataAdapter (SQL, conn);
Da. SelectCommand.Parameters.AddRange (SP);
DataTable table = new DataTable ();
Da. Fill (table);
return table;
}
}
<summary>
return table
</summary>
<param name= "SQL" ></param>
<param name= "SP" ></param>
<returns></returns>
public static DataTable GetTable (String sql, CommandType type, sqlparameter[] sp)
{
using (SqlConnection conn = new SqlConnection (sqlconn))//can automatically release Conn with adapter
//{
Conn. Open ();
SqlDataAdapter da = new SqlDataAdapter (sql,new SqlConnection (sqlconn));
Da.SelectCommand.CommandType = type;
Da. SelectCommand.Parameters.AddRange (SP);
DataTable table = new DataTable ();
Da. Fill (table);

return table;
//}
}

<summary>
Create reader
</summary>
<returns></returns>
public static SqlDataReader Getsqldataread (String sql, sqlparameter[] sp)
{
Create reader cannot be closed
SqlConnection conn = new SqlConnection (sqlconn);
Conn. Open ();
SqlCommand comm = new SqlCommand (SQL, conn);
Comm. Parameters.addrange (SP);
Return COMM. ExecuteReader (commandbehavior.closeconnection);//When closing the reader closes the relative connection
}

Execute SQL statement
public static int ExecuteNonQuery (String sql, sqlparameter[] sp)
{
using (SqlConnection conn =new SqlConnection (sqlconn))
{
Conn. Open ();
SqlCommand comm = new SqlCommand (SQL, conn);
Comm. Parameters.addrange (SP);
Return COMM. ExecuteNonQuery ();
}

}


}
}

SQLHELPER Help Class

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.