天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

444 lines
17 KiB

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
namespace Common.Data.SqlServer
{
/// <summary>
/// 功能:Oracle操作类
/// 作者:王昊昇
/// 时间:2012.2.8
/// </summary>
public class SqlServerHelper : IDataHelper
{
/// <summary>
/// 处理命令
/// </summary>
/// <param name="conn">数据库连接</param>
/// <param name="cmd">数据库命令对象</param>
/// <param name="trans">事务对象</param>
/// <param name="txt">命令</param>
/// <param name="param">参数</param>
private void PrepareCommand(SqlConnection conn, SqlCommand cmd,
SqlTransaction trans, string txt, SqlParameter[] param)
{
cmd.Connection = conn;
cmd.CommandText = txt;
if (trans != null)
{
cmd.Transaction = trans;
}
cmd.CommandType = System.Data.CommandType.Text;
if (param != null)
{
foreach (SqlParameter parm in param)
{
cmd.Parameters.Add(parm);
}
}
}
/// <summary>
/// 打开连接
/// </summary>
/// <param name="connString">连接字符串</param>
/// <returns>连接对象</returns>
public System.Data.IDbConnection OpenConnection(string connString)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connString;
conn.Open();
return conn;
}
/// <summary>
/// 打开连接
/// </summary>
/// <param name="connStrBuilder">连接字符串生成器</param>
/// <returns>连接对象</returns>
public System.Data.IDbConnection OpenConnection(System.Data.Common.DbConnectionStringBuilder connStrBuilder)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connStrBuilder.ToString();
conn.Open();
return conn;
}
/// <summary>
/// 关闭连接
/// </summary>
/// <param name="conn">连接对象</param>
public void CloseConnection(System.Data.IDbConnection conn)
{
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
conn = null;
}
/// <summary>
/// 读取数据
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="sql">查询语句</param>
/// <returns>数据读取器</returns>
public System.Data.IDataReader ExecuteReader(System.Data.IDbConnection conn, string sql)
{
using (SqlCommand cmd = new SqlCommand(sql, (SqlConnection)conn))
{
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
}
/// <summary>
/// 读取数据,支持参数
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="sql">查询语句</param>
/// <param name="param">参数</param>
/// <returns>数据读取器</returns>
public System.Data.IDataReader ExecuteReader(System.Data.IDbConnection conn, string sql,
params System.Data.IDataParameter[] param)
{
using (SqlCommand cmd = new SqlCommand())
{
PrepareCommand((SqlConnection)conn, cmd, null, sql,
(SqlParameter[])param);
SqlDataReader reader = cmd.ExecuteReader();
cmd.Parameters.Clear();
return reader;
}
}
/// <summary>
/// 执行带参数的更新语句,返回受影响行数
/// </summary>
/// <param name="conn">数据库连接</param>
/// <param name="strSql">更新语句</param>
/// <param name="param">参数列表</param>
/// <returns>受影响行数</returns>
public int ExecuteSql(System.Data.IDbConnection conn, string sql, params System.Data.IDataParameter[] param)
{
using (SqlCommand cmd = new SqlCommand())
{
PrepareCommand((SqlConnection)conn, cmd, null, sql,
(SqlParameter[])param);
int r = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return r;
}
}
/// <summary>
/// 执行更新语句,返回受影响行数
/// </summary>
/// <param name="conn">数据库连接</param>
/// <param name="strSql">更新语句</param>
/// <returns>受影响行数</returns>
public int ExecuteSql(System.Data.IDbConnection conn, string sql)
{
using (SqlCommand cmd = new SqlCommand(sql, (SqlConnection)conn))
{
int r = cmd.ExecuteNonQuery();
return r;
}
}
/// <summary>
/// 执行数据处理事务
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="list">执行语句列表</param>
public void ExecuteSqlTran(System.Data.IDbConnection conn, List<string> list)
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = (SqlConnection)conn;
using (SqlTransaction trans = (SqlTransaction)conn.BeginTransaction())
{
cmd.Transaction = trans;
foreach(var sql in list)
{
if (sql.Trim().Length > 0)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
trans.Commit();
}
}
}
/// <summary>
/// 执行数据处理事务
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="table">执行语句列表</param>
public void ExecuteSqlTran(System.Data.IDbConnection conn, System.Collections.Hashtable table)
{
using (SqlCommand cmd = new SqlCommand())
using (SqlTransaction trans = ((SqlConnection)conn).BeginTransaction())
{
foreach (System.Collections.DictionaryEntry de in table)
{
string txt = de.Key.ToString();
SqlParameter[] param = (SqlParameter[])de.Value;
PrepareCommand((SqlConnection)conn, cmd, trans, txt, param);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
trans.Commit();
}
}
}
/// <summary>
/// 执行查询语句,返回查询结果的第一行第一列的数据
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="sql">查询语句</param>
/// <returns>结果对象</returns>
public object GetSingle(System.Data.IDbConnection conn, string sql)
{
using (SqlCommand cmd = new SqlCommand(sql, (SqlConnection)conn))
{
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
}
/// <summary>
/// 执行带参数的查询语句,返回查询结果的第一行第一列的数据
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="sql">查询语句</param>
/// <param name="param">参数</param>
/// <returns>结果对象</returns>
public object GetSingle(System.Data.IDbConnection conn, string sql, params System.Data.IDataParameter[] param)
{
using (SqlCommand cmd = new SqlCommand())
{
PrepareCommand((SqlConnection)conn, cmd, null, sql,
(SqlParameter[])param);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
}
/// <summary>
/// 执行带参数的数据查询
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="sql">查询语句</param>
/// <param name="param">参数</param>
/// <returns>数据集</returns>
public System.Data.DataSet Query(System.Data.IDbConnection conn, string sql,
params System.Data.IDataParameter[] param)
{
using (SqlCommand cmd = new SqlCommand())
{
PrepareCommand((SqlConnection)conn, cmd, null, sql,
(SqlParameter[])param);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
System.Data.DataSet ds = new System.Data.DataSet();
da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds;
}
}
}
/// <summary>
/// 执行数据查询
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="sql">查询语句</param>
/// <returns>数据集</returns>
public System.Data.DataSet Query(System.Data.IDbConnection conn, string sql)
{
using (SqlCommand cmd = new SqlCommand(sql, (SqlConnection)conn))
using (SqlDataAdapter command = new SqlDataAdapter(sql, (SqlConnection)conn))
{
System.Data.DataSet ds = new System.Data.DataSet();
command.Fill(ds, "ds");
return ds;
}
}
/// <summary>
/// 执行数据查询
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="sql">查询语句</param>
/// <returns>数据表</returns>
public System.Data.DataTable QueryReturnDataTable(System.Data.IDbConnection conn, string sql)
{
using (SqlCommand cmd = new SqlCommand(sql, (SqlConnection)conn))
using (SqlDataAdapter command = new SqlDataAdapter(sql, (SqlConnection)conn))
{
System.Data.DataTable dt = new System.Data.DataTable();
command.Fill(dt);
return dt;
}
}
/// <summary>
/// 执行数据查询
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="sql">查询语句</param>
/// <param name="param">参数</param>
/// <returns>数据表</returns>
public System.Data.DataTable QueryReturnDataTable(System.Data.IDbConnection conn, string sql,
params System.Data.IDataParameter[] param)
{
using (SqlCommand cmd = new SqlCommand())
{
PrepareCommand((SqlConnection)conn, cmd, null, sql,
(SqlParameter[])param);
using (SqlDataAdapter command = new SqlDataAdapter(cmd))
{
System.Data.DataTable dt = new System.Data.DataTable();
command.Fill(dt);
cmd.Parameters.Clear();
return dt;
}
}
}
/// <summary>
/// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns></returns>
private SqlCommand BuildQueryCommand(System.Data.IDbConnection conn, string storedProcName, IDataParameter[] param)
{
SqlCommand command = new SqlCommand(storedProcName, (SqlConnection)conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
foreach (SqlParameter parameter in param)
{
command.Parameters.Add(parameter);
}
return command;
}
/// <summary>
/// 执行带返回值存储过程
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="RowsAffected">受影响行数</param>
/// <returns></returns>
private DataTable ExecutePageStoredProcedure(System.Data.IDbConnection conn, string storedProcName,
IDataParameter[] parameters, out int rowsAffected)
{
SqlParameter OutParameters = new SqlParameter("@RowsAffected", SqlDbType.Int, 4, ParameterDirection.Output,
false, 0, 0, string.Empty, DataRowVersion.Default, null);
using (DataSet dataSet = new DataSet())
{
using (SqlCommand command = BuildQueryCommand(conn, storedProcName, parameters))
{
command.Parameters.Add(OutParameters);
using (SqlDataAdapter sqlDA = new SqlDataAdapter())
{
sqlDA.SelectCommand = command;
sqlDA.Fill(dataSet);
}
rowsAffected = Convert.ToInt32(command.Parameters["@RowsAffected"].Value);
}
return dataSet.Tables[0].Copy();
}
}
/// <summary>
/// 分页处理,取DataTable
/// 使用PagingQuery分页存储过程
/// </summary>
/// <param name="conn">连接对象</param>
/// <param name="tableName">表名</param>
/// <param name="keyFieldName">以哪几列作为排序依据,多列以","分隔</param>
/// <param name="columnList">字段列表,取表中的列,以","分隔,取全部数据使用"*"</param>
/// <param name="where">查询条件</param>
/// <param name="orderBy">排序方式</param>
/// <param name="rowsAmountPerPage">每页记录数</param>
/// <param name="pageIndex">指定页索引</param>
/// <param name="pageConut">返回总页数</param>
/// <param name="rowsAffected">返回总记录数</param>
/// <returns></returns>
public System.Data.DataTable PagingQuery(System.Data.IDbConnection conn, string tableName, string keyFieldName, string columnList, string where,
OrderBy orderBy, int rowsAmountPerPage, int pageIndex, out int pageConut, out int rowsAffected)
{
int intRowsAffected = 0;
SqlParameter[] Parameters = { new SqlParameter("@TableName", SqlDbType.VarChar),
new SqlParameter("@FieldName", SqlDbType.VarChar),
new SqlParameter("@OrderBy", SqlDbType.Bit),
new SqlParameter("@ColList", SqlDbType.VarChar),
new SqlParameter("@StrWhere", SqlDbType.VarChar),
new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int)};
Parameters[0].Value = tableName; // 表名
Parameters[1].Value = keyFieldName; // 按该列来分页
Parameters[2].Value = orderBy.GetHashCode(); // 排序
Parameters[3].Value = columnList; // 每页记录数
Parameters[4].Value = where; // 查询条件
Parameters[5].Value = rowsAmountPerPage; // 每页记录数
Parameters[6].Value = pageIndex; // 指定页
DataTable dt = ExecutePageStoredProcedure(conn, "P_Paging", Parameters, out intRowsAffected);
if (intRowsAffected % rowsAmountPerPage == 0)
{
pageConut = intRowsAffected / rowsAmountPerPage;
}
else
{
pageConut = intRowsAffected / rowsAmountPerPage + 1;
}
rowsAffected = intRowsAffected;
return dt;
}
}
}