SqlHelper
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace SmartLockerWS.Class
{
/// <summary>
/// https://www.cnblogs.com/694579350liuq/p/7080677.html
/// OleDbConnection 支持连接sql、oracle、excel等多种数据源
/// </summary>
class sqlHelper1
{
public static string GetSqlConnectionString()
{
return ConfigurationManager.AppSettings[“SQLConn”].ToString();
}
//适合增删改操作,返回影响条数
public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(GetSqlConnectionString()))
{
using (SqlCommand comm = conn.CreateCommand())
{
try
{
conn.Open();
comm.CommandText = sql;
if(parameters!=null)
comm.Parameters.AddRange(parameters);
return comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
}
//查询操作,返回查询结果中的第一行第一列的值
public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(GetSqlConnectionString()))
{
using (SqlCommand comm = conn.CreateCommand())
{
try
{
conn.Open();
comm.CommandText = sql;
if(parameters!=null)
comm.Parameters.AddRange(parameters);
return comm.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
}
}
//Adapter调整,查询操作,返回DataTable
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
{
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, GetSqlConnectionString()))
{
DataTable dt = new DataTable();
if(parameters!=null)
adapter.SelectCommand.Parameters.AddRange(parameters);
adapter.Fill(dt);
return dt;
}
}
//dataset
public static DataSet ExecuteDataset(string sql, params SqlParameter[] parameters)
{
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, GetSqlConnectionString()))
{
DataSet ds = new DataSet();
if(parameters!=null)
adapter.SelectCommand.Parameters.AddRange(parameters);
adapter.Fill(ds);
return ds;
////表集合
//DataTableCollection table = ds.Tables;
}
}
//datareader
public static SqlDataReader ExecuteReader(string sqlText, params SqlParameter[] parameters)
{
//SqlDataReader要求,它读取数据的时候有,它独占它的SqlConnection对象,而且SqlConnection必须是Open状态
SqlConnection conn = new SqlConnection(GetSqlConnectionString());//不要释放连接,因为后面还需要连接打开状态
SqlCommand cmd = conn.CreateCommand();
conn.Open();
cmd.CommandText = sqlText;
if(parameters!=null)
cmd.Parameters.AddRange(parameters);
//CommandBehavior.CloseConnection当SqlDataReader释放的时候,顺便把SqlConnection对象也释放掉
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name=”SQLStringList”>多条SQL语句</param>
public static int ExecuteSqlTran(List<String> SQLStringList)
{
using (SqlConnection conn = new SqlConnection(GetSqlConnectionString()))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
int count = 0;
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n];
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
count += cmd.ExecuteNonQuery();
}
}
tx.Commit();
return count;
}
catch
{
tx.Rollback();
return 0;
}
}
}
/// <summary>
/// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
/// </summary>
/// <param name=”storedProcName”>存储过程名</param>
/// <param name=”parameters”>存储过程参数</param>
/// <returns>SqlDataReader</returns>
public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
{
SqlConnection connection = new SqlConnection(GetSqlConnectionString());
SqlDataReader returnReader;
connection.Open();
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}
private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
if (parameter != null)
{
// 检查未分配值的输出参数,将其分配以DBNull.Value.
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
command.Parameters.Add(parameter);
}
}
return command;
}
}
}