ADO.NET操作封裝(SqlHelper)
阿新 • • 發佈:2019-02-08
private static readonly SqlConnection SqlConn = new SqlConnection(ConnStr);
private static readonly SqlConnection SqlConnExtend = new SqlConnection(ConnStrExtend );
private SqlHelper()
{
}
/// <summary>
/// 封裝ado.net的ExecQuery
/// </summary>
/// <param name="sql"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static DataTable ExecQuery(string sql, SqlParameter[] paramers)
{
try
{
var sqlDataAdapter = new SqlDataAdapter(sql, SqlConn);
if (paramers != null)
{
sqlDataAdapter.SelectCommand.Parameters.AddRange(paramers);
}
SqlConn.Open();
var dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
return dataTable;
}
catch (Exception e)
{
return null;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的ExecScalar
/// </summary>
/// <param name="sql"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static object ExecScalar(string sql, SqlParameter[] paramers)
{
try
{
var sqlCommand = new SqlCommand(sql, SqlConn);
if (paramers != null)
{
sqlCommand.Parameters.AddRange(paramers);
}
sqlCommand.CommandType = CommandType.Text;
SqlConn.Open();
return sqlCommand.ExecuteScalar();
}
catch (Exception e)
{
return null;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的ExecNonQuery
/// </summary>
/// <param name="sql"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static int ExecNonQuery(string sql, SqlParameter[] paramers)
{
try
{
var sqlCommand = new SqlCommand(sql, SqlConn);
if (paramers != null)
{
sqlCommand.Parameters.AddRange(paramers);
}
sqlCommand.CommandType = CommandType.Text;
SqlConn.Open();
return sqlCommand.ExecuteNonQuery();
}
catch(Exception e)
{
return int.MinValue;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的儲存過程的更新操作
/// </summary>
/// <param name="name"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static int ExecStoredProcedureAsUpdate(string name, SqlParameter[] paramers)
{
try
{
var sqlCommand = new SqlCommand(name, SqlConn);
sqlCommand.Parameters.AddRange(paramers);
sqlCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
sqlCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlConn.Open();
sqlCommand.ExecuteNonQuery();
return int.Parse(sqlCommand.Parameters["@return"].Value.ToString());
}
catch (Exception e)
{
return int.MinValue;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的儲存過程的查詢操作
/// </summary>
/// <param name="name"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static DataTable ExecStoredProcedureAsQuery(string name, SqlParameter[] paramers)
{
try
{
var sqlDataAdapter = new SqlDataAdapter(name, SqlConn);
sqlDataAdapter.SelectCommand.Parameters.AddRange(paramers);
sqlDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
sqlDataAdapter.SelectCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
sqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlConn.Open();
var dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
return dataTable;
}
catch (Exception e)
{
return null;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的事務的更新操作,不能跨越多個數據庫連線
/// </summary>
/// <param name="sql"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static int[] ExecTransactionAsUpdate(string[] sql, List<SqlParameter[]> paramers)
{
if (sql.Length == paramers.Count)
{
private static readonly SqlConnection SqlConnExtend = new SqlConnection(ConnStrExtend );
private SqlHelper()
{
}
/// <summary>
/// 封裝ado.net的ExecQuery
/// </summary>
/// <param name="sql"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static DataTable ExecQuery(string sql, SqlParameter[] paramers)
{
try
{
var sqlDataAdapter = new SqlDataAdapter(sql, SqlConn);
if (paramers != null)
{
sqlDataAdapter.SelectCommand.Parameters.AddRange(paramers);
}
SqlConn.Open();
var dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
return dataTable;
}
catch (Exception e)
{
return null;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的ExecScalar
/// </summary>
/// <param name="sql"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static object ExecScalar(string sql, SqlParameter[] paramers)
{
try
{
var sqlCommand = new SqlCommand(sql, SqlConn);
if (paramers != null)
{
sqlCommand.Parameters.AddRange(paramers);
}
sqlCommand.CommandType = CommandType.Text;
SqlConn.Open();
return sqlCommand.ExecuteScalar();
}
catch (Exception e)
{
return null;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的ExecNonQuery
/// </summary>
/// <param name="sql"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static int ExecNonQuery(string sql, SqlParameter[] paramers)
{
try
{
var sqlCommand = new SqlCommand(sql, SqlConn);
if (paramers != null)
{
sqlCommand.Parameters.AddRange(paramers);
}
sqlCommand.CommandType = CommandType.Text;
SqlConn.Open();
return sqlCommand.ExecuteNonQuery();
}
catch(Exception e)
{
return int.MinValue;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的儲存過程的更新操作
/// </summary>
/// <param name="name"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static int ExecStoredProcedureAsUpdate(string name, SqlParameter[] paramers)
{
try
{
var sqlCommand = new SqlCommand(name, SqlConn);
sqlCommand.Parameters.AddRange(paramers);
sqlCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
sqlCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlConn.Open();
sqlCommand.ExecuteNonQuery();
return int.Parse(sqlCommand.Parameters["@return"].Value.ToString());
}
catch (Exception e)
{
return int.MinValue;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的儲存過程的查詢操作
/// </summary>
/// <param name="name"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static DataTable ExecStoredProcedureAsQuery(string name, SqlParameter[] paramers)
{
try
{
var sqlDataAdapter = new SqlDataAdapter(name, SqlConn);
sqlDataAdapter.SelectCommand.Parameters.AddRange(paramers);
sqlDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
sqlDataAdapter.SelectCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
sqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlConn.Open();
var dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
return dataTable;
}
catch (Exception e)
{
return null;
}
finally
{
SqlConn.Close();
}
}
/// <summary>
/// 封裝ado.net的事務的更新操作,不能跨越多個數據庫連線
/// </summary>
/// <param name="sql"></param>
/// <param name="paramers"></param>
/// <returns></returns>
public static int[] ExecTransactionAsUpdate(string[] sql, List<SqlParameter[]> paramers)
{
if (sql.Length == paramers.Count)
{