C # generic class DBHelper
web.config:
<connectionStrings>
<add name="dh_web" connectionString="Data Source=xxx.com;Initial Catalog=xx_db;User ID=xx;Password=**;
pooling=true;max pool size=200" providerName="System.Data.SqlClient"/>
</connectionStrings>
DBHelper categories:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class SQLHelper
{
//连接字符串dh_web
static string strConn = ConfigurationManager.ConnectionStrings["dh_web"].ToString();
#region execute the query and returns the DataTable object -----------------------
public static DataTable GetTable(string strSQL)
{
return GetTable(strSQL, null);
}
public static DataTable GetTable(string strSQL, SqlParameter[] pas)
{
return GetTable(strSQL, pas, CommandType.Text);
}
/// <summary>
/// 执行查询,返回DataTable对象
/// </summary>
/// <param name="strSQL">sql语句</param>
/// <param name="pas">参数数组</param>
/// <param name="cmdtype">Command类型</param>
/// <returns>DataTable对象</returns>
public static DataTable GetTable(string strSQL, SqlParameter[] pas, CommandType cmdtype)
{
DataTable dt = new DataTable(); ;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
da.SelectCommand.CommandType = cmdtype;
if (pas != null)
{
da.SelectCommand.Parameters.AddRange(pas);
}
da.Fill(dt);
}
return dt;
}
#endregion
#region execute the query returns a DataSet object -------------------------
public static DataSet GetDataSet(string strSQL)
{
return GetDataSet(strSQL,null);
}
public static DataSet GetDataSet(string strSQL, SqlParameter[] pas)
{
return GetDataSet(strSQL,pas,CommandType.Text);
}
/// <summary>
/// 执行查询,返回DataSet对象
/// </summary>
/// <param name="strSQL">sql语句</param>
/// <param name="pas">参数数组</param>
/// <param name="cmdtype">Command类型</param>
/// <returns>DataSet对象</returns>
public static DataSet GetDataSet(string strSQL, SqlParameter[] pas, CommandType cmdtype)
{
DataSet dt = new DataSet(); ;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
da.SelectCommand.CommandType = cmdtype;
if (pas != null)
{
da.SelectCommand.Parameters.AddRange(pas);
}
da.Fill(dt);
}
return dt;
}
#endregion
#region perform a non-query SQL statements and stored procedures -----------------------------
public static int ExcuteProc(string ProcName)
{
return ExcuteSQL(ProcName, null, CommandType.StoredProcedure);
}
public static int ExcuteProc(string ProcName, SqlParameter[] pars)
{
return ExcuteSQL(ProcName, pars, CommandType.StoredProcedure);
}
public static int ExcuteSQL(string strSQL)
{
return ExcuteSQL(strSQL, null);
}
public static int ExcuteSQL(string strSQL, SqlParameter[] paras)
{
return ExcuteSQL(strSQL, paras, CommandType.Text);
}
/// execute non-query SQL statements and stored procedures
/// add, delete, change
/// </ the Summary>
/// <param name = "strSQL"> SQL statements to be executed </ param>
/// < param name = "paras"> parameter list, the parameter is not filled null </ param>
/// <param name = "cmdType"> the Command type </ param>
/// <returns> returns the number of rows Effect </ returns>
static int ExcuteSQL public (String strSQL, the SqlParameter [] Paras, the CommandType cmdType)
{
int I = 0;
the using (Conn the SqlConnection the SqlConnection new new = (the strConn))
{
the SqlCommand the SqlCommand cmd = new new (strSQL, Conn);
cmd.CommandType = cmdType ;
IF (Paras!= null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
i = cmd.ExecuteNonQuery();
conn.Close();
}
return i;
}
#endregion
#region execute the query returns the first row, first column ---------------------------------
public static int ExcuteScalarSQL(string strSQL)
{
return ExcuteScalarSQL(strSQL, null);
}
static int ExcuteScalarSQL public (String strSQL, the SqlParameter [] Paras)
{
return ExcuteScalarSQL (strSQL, Paras, CommandType.Text);
}
public static int ExcuteScalarProc (String strSQL, the SqlParameter [] Paras)
{
return ExcuteScalarSQL (strSQL, Paras, the CommandType. the StoredProcedure);
}
/// <Summary>
/// SQL statement is executed, return to the first row, first column
/// </ Summary>
/// <param name = "strSQL"> SQL statement to be executed </ param>
/// <param name = "Paras"> parameter list, the parameter is not filled null </ param>
/// <returns> returns the number of rows Effect </ returns>
public static int ExcuteScalarSQL (String strSQL,SqlParameter[] paras, CommandType cmdType)
{
int i = 0;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = cmdType;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
i =Convert.ToInt32( cmd.ExecuteScalar());
conn.Close();
}
return i;
}
#endregion
#region query to retrieve a single value ------------------------------------
/// <Summary>
/// stored procedure calls with no parameters to obtain a single value
/// </ Summary>
/// <param name = "ProcName"> </ param>
/// <Returns> </ Returns >
public static Object GetObjectByProc (String ProcName)
{
return GetObjectByProc (ProcName, null);
}
/// <Summary>
/// call parameters stored procedure to get a single value
/// </ Summary>
/// <param name = "ProcName"> </ param>
/// <param name = "Paras"> </ param>
/// <Returns> </ Returns>
public static Object GetObjectByProc (String ProcName,SqlParameter[] paras)
{
return GetObject(ProcName, paras, CommandType.StoredProcedure);
}
/// <Summary>
/// Gets a single value in accordance with the sql statement
/// </ Summary>
/// <param name = "strSQL"> </ param>
/// <Returns> </ Returns>
public static the GetObject Object (String strSQL)
{
return the GetObject (strSQL, null);
}
/// <Summary>
/// Gets the value of a single parameter array according to sql statement and
/// </ Summary>
/// <param name = "strSQL "> </ param>
/// <param name =" Paras "> </ param>
/// <Returns> </ Returns>
public static Object the GetObject (String strSQL,SqlParameter[] paras)
{
return GetObject(strSQL, paras, CommandType.Text);
}
/// <the Summary>
/// execute SQL statements, returns the first line of the first column
/// </ the Summary>
/// <param name = "strSQL"> SQL statements to be executed </ param>
/// <param name = "paras"> parameter list, the parameter is not filled null </ param>
/// <returns> returns the first row first column </ returns>
public static Object the GetObject (String strSQL, the SqlParameter [] Paras, the CommandType cmdtype)
{
Object O = null;
the using (Conn the SqlConnection the SqlConnection new new = (the strConn))
{
the SqlCommand the SqlCommand cmd = new new (strSQL, Conn);
cmd.CommandType = cmdtype;
IF (! Paras = null)
{
cmd.Parameters.AddRange (best);
}
conn.Open();
o = cmd.ExecuteScalar();
conn.Close();
}
return o;
}
#endregion
#region query gets DataReader ------------------------------------
/// <Summary>
/// stored procedure calls with no parameters and returns DataReader object
/// </ Summary>
/// <param name = "procName"> stored procedure names </ param>
/// <Returns > DataReader object </ returns>
public static GetReaderByProc the SqlDataReader (String procName)
{
return GetReaderByProc (procName, null);
}
/// <Summary>
/// call a stored procedure with parameters, return the DataReader object
/// </ Summary>
/// <param name = "procName"> stored procedure name </ param>
/// <param name = "Paras"> parameter array </ param>
/// <Returns> the DataReader Object </ returns>
public static SqlDataReader GetReaderByProc(string procName, SqlParameter[] paras)
{
getReader return (procName, Paras, CommandType.StoredProcedure);
}
/// <Summary>
/// Returns an sql statement DataReader object
/// </ Summary>
/// <param name = "strSQL"> sql statement </ param>
/// <returns> DataReader object </ returns>
public static getReader the SqlDataReader (String strSQL)
{
return getReader (strSQL, null);
}
/// <Summary>
/// returns an sql statement DataReader object parameters and
/ // </ Summary>
/// <param name = "strSQL"> SQL statement </ param>
/// <param name = "Paras"> parameter array </ param>
/// <Returns>DataReader object </ returns>
getReader the SqlDataReader static public (String strSQL, the SqlParameter [] Paras)
{
return getReader (strSQL, Paras, CommandType.Text);
}
/// <Summary>
/// Get the SQL query the DataReader
/// </ Summary>
// / <param name = "strSQL" > SQL </ param> statement query
/// <param name = "paras" > parameter list, no argument fill null </ param>
/// <returns a> query to the DataReader (when the object is closed, automatically close the connection) </ Returns>
public static getReader the SqlDataReader (String strSQL, the SqlParameter [] Paras, the CommandType cmdtype)
{
the SqlDataReader sqldr = null;
the SqlConnection the SqlConnection Conn new new = (the strConn);
Cmd = new new the SqlCommand the SqlCommand (strSQL, Conn);
cmd.CommandType = cmdtype;
IF (Paras = null!)
{
Cmd.Parameters.AddRange (Paras);
}
conn.Open ();
//CommandBehavior.CloseConnection action if DataReader object associated off, the connection is automatically closed
sqldr = cmd.ExecuteReader (the CommandBehavior.CloseConnection);
return sqldr;
}
#endregion
#region bulk insert data ---------------------------------------------
/// <Summary>
/// bulk insert data into the database
/// </ Summary>
/// <param name = "sourceDt"> data source table </ param>
/// <param name = "targetTable" > destination table on the server </ param>
public static void BulkToDB (sourceDt the DataTable, String targetTable)
{
the SqlConnection the SqlConnection Conn new new = (the strConn);
the SqlBulkCopy bulkcopy the SqlBulkCopy new new = (Conn); // data from other sources with effective bulk load sql table server
bulkCopy.DestinationTableName = targetTable; // server name of the destination table
bulkCopy.BatchSize = sourceDt.Rows.Count; // number of rows in each batch of
the try
{
conn.Open ();
IF (sourceDt =! null && sourceDt.Rows.Count != 0)
bulkCopy.WriteToServer (sourceDt); // all rows will provide the source data is copied into the target table
}
the catch (Exception EX)
{
the throw EX;
}
the finally
{
conn.Close ();
IF (! = null bulkcopy)
bulkcopy .close ();
}
}
#endregion
}
}