using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NFine.Data.Extensions
{
public class ERPSqlServerHelper
{
#region [ 连接串相关 ]
///
/// 数据中心DB的连接字符串
///
public static string ConnectionString = ConfigurationManager.ConnectionStrings["ERPconnstr"].ConnectionString;
///
/// 获取同步服务器的连接
///
///
private static SqlConnection GetConnectionString()
{
return new SqlConnection(ConnectionString);
}
///
/// 根据连接串获取连接
///
///
private static SqlConnection GetConnByString(string conn)
{
return new SqlConnection(conn);
}
#endregion
#region 数据分页
///
/// 摘要:
/// 数据分页
/// 参数:
/// sql:传入要执行sql语句
/// param:参数化
/// orderField:排序字段
/// orderType:排序类型
/// pageIndex:当前页
/// pageSize:页大小
/// count:返回查询条数
///
public static DataTable GetPageTable(string sql, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
{
StringBuilder strSql = new StringBuilder();
if (pageIndex == 0)
{
pageIndex = 1;
}
int num = (pageIndex - 1) * pageSize;
int num1 = (pageIndex) * pageSize;
string OrderBy = "";
if (!string.IsNullOrEmpty(orderField))
OrderBy = "Order By " + orderField + " " + orderType + "";
else
OrderBy = "order by (select 0)";
strSql.Append("Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
strSql.Append(" As rowNum, * From (" + sql + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
count = Convert.ToInt32(ExecuteScalar(CommandType.Text, "Select Count(1) From (" + sql + ") As t", param));
IDataReader dr = ExecuteReader(CommandType.Text, strSql.ToString(), param);
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 根据sql语句和参数,返回DataTable
///
/// sql语句
/// 可变参数
/// DataTable
public static DataTable GetDataTableBySql(string sql, params SqlParameter[] spArr)
{
using (SqlConnection conn = GetConnectionString())
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
if (spArr != null && spArr.Length > 0)
cmd.Parameters.AddRange(spArr.SetDBNull());
DataTable dt = cmd.ExecuteDataTable();
return dt;
}
}
///
/// 使用提供的参数,执行有结果集返回的数据库操作命令、并返回SqlDataReader对象
///
/// 执行命令的类型(存储过程或T-SQL,等等)
/// 存储过程名称或者T-SQL命令行<
/// 执行命令所需的sql语句对应参数
/// 返回SqlDataReader对象
private static IDataReader ExecuteReader(CommandType cmdType, string cmdText, params DbParameter[] parameters)
{
DbCommand cmd = DbFactory.CreateDbCommand();
DbConnection conn = DbFactory.CreateDbConnection(ConnectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters);
IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception ex)
{
conn.Close();
cmd.Dispose();
//log.Error(ex.Message);
throw;
}
}
///
/// 依靠数据库连接字符串connectionString,
/// 使用所提供参数,执行返回首行首列命令
///
/// 执行命令的类型(存储过程或T-SQL,等等)
/// 存储过程名称或者T-SQL命令行
/// 执行命令所需的sql语句对应参数
/// 返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。
private static object ExecuteScalar(CommandType cmdType, string cmdText, params DbParameter[] parameters)
{
try
{
DbCommand cmd = DbFactory.CreateDbCommand();
using (DbConnection connection = DbFactory.CreateDbConnection(ConnectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, parameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
catch (Exception ex)
{
//log.Error(ex.Message);
throw;
}
}
///
/// 为即将执行准备一个命令
///
/// SqlCommand对象
/// SqlConnection对象
/// DbTransaction对象
/// 执行命令的类型(存储过程或T-SQL,等等)
/// 存储过程名称或者T-SQL命令行, e.g. Select * from Products
/// SqlParameters to use in the command
private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction isOpenTrans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (isOpenTrans != null)
cmd.Transaction = isOpenTrans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
cmd.Parameters.AddRange(cmdParms);
}
}
#endregion
}
}