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