这个类大家肯定很熟悉了,但是知其然一定要知其所以然,希望该系列能让我回顾一下ADO.NET
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Food.DAL
{
public class DBHelper
{
private static string connectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
public static int ExecuteCommand(string safeSql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(safeSql, con))
{
con.Open();
int result = cmd.ExecuteNonQuery();
return result;
}
}
}
public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
}
}
public static int GetScalar(string safeSql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(safeSql, con))
{
con.Open();
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
}
}
public static int GetScalar(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
}
}
/// <summary>
/// 查看新的数量
/// </summary>
/// <param name="sql"></param>
/// <param name="values"></param>
/// <returns></returns>
public static object GetScalarWithNewCon(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(values);
return cmd.ExecuteScalar();
}
}
}
public static string GetScalarString(string sql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
string result = cmd.ExecuteScalar().ToString();
return result;
}
}
}
public static SqlDataReader GetReader(string safeSql)
{
SqlConnection con = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand(safeSql, con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
}
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlConnection con = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
}
public static DataTable GetTable(string safeSql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(safeSql, con))
{
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
}
public static DataTable GetTable(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
DataSet ds = new DataSet();
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
}
public static DataSet GetDataSet(string sql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
}
}
public static DataSet GetDataSet(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
DataSet ds = new DataSet();
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
}
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
{
SqlConnection con = new SqlConnection(connectionString);
SqlDataReader returnReader;
con.Open();
SqlCommand command = BuildQueryCommand(con, storedProcName, parameters);
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader();
return returnReader;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static DataTable RunProcedureTable(string storedProcName, IDataParameter[] parameters)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
DataSet dsSet = new DataSet();
SqlDataAdapter sqlDa = new SqlDataAdapter(storedProcName, con);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDa.SelectCommand.Parameters.AddRange(parameters);
sqlDa.Fill(dsSet);
return dsSet.Tables[0];
}
}
/// <summary>
/// 为存储过程添加参数
/// </summary>
/// <param name="connection"></param>
/// <param name="storedProcName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
using (SqlCommand command = new SqlCommand(storedProcName, connection))
{
command.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
return command;
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Food.DAL
{
public class DBHelper
{
private static string connectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
public static int ExecuteCommand(string safeSql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(safeSql, con))
{
con.Open();
int result = cmd.ExecuteNonQuery();
return result;
}
}
}
public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
}
}
public static int GetScalar(string safeSql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(safeSql, con))
{
con.Open();
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
}
}
public static int GetScalar(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
}
}
/// <summary>
/// 查看新的数量
/// </summary>
/// <param name="sql"></param>
/// <param name="values"></param>
/// <returns></returns>
public static object GetScalarWithNewCon(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(values);
return cmd.ExecuteScalar();
}
}
}
public static string GetScalarString(string sql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
string result = cmd.ExecuteScalar().ToString();
return result;
}
}
}
public static SqlDataReader GetReader(string safeSql)
{
SqlConnection con = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand(safeSql, con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
}
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlConnection con = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
}
public static DataTable GetTable(string safeSql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(safeSql, con))
{
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
}
public static DataTable GetTable(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
DataSet ds = new DataSet();
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
}
public static DataSet GetDataSet(string sql)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
}
}
public static DataSet GetDataSet(string sql, params SqlParameter[] values)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
DataSet ds = new DataSet();
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
}
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
{
SqlConnection con = new SqlConnection(connectionString);
SqlDataReader returnReader;
con.Open();
SqlCommand command = BuildQueryCommand(con, storedProcName, parameters);
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader();
return returnReader;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static DataTable RunProcedureTable(string storedProcName, IDataParameter[] parameters)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
DataSet dsSet = new DataSet();
SqlDataAdapter sqlDa = new SqlDataAdapter(storedProcName, con);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDa.SelectCommand.Parameters.AddRange(parameters);
sqlDa.Fill(dsSet);
return dsSet.Tables[0];
}
}
/// <summary>
/// 为存储过程添加参数
/// </summary>
/// <param name="connection"></param>
/// <param name="storedProcName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
using (SqlCommand command = new SqlCommand(storedProcName, connection))
{
command.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
return command;
}
}
}
}