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 OtherSqlServerHelper
{
#region [ 连接串相关 ]
///
/// 数据中心DB的连接字符串
///
public static string ConnectionString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
///
/// 获取同步服务器的连接
///
///
private static SqlConnection GetConnectionString()
{
return new SqlConnection(ConnectionString);
}
///
/// 根据连接串获取连接
///
///
private static void SetConnByString(string conn)
{
ConnectionString = ConfigurationManager.ConnectionStrings[conn].ConnectionString;
}
#endregion
#region 数据分页
///
/// 摘要:
/// 数据分页
/// 参数:
/// sql:传入要执行sql语句
/// param:参数化
/// orderField:排序字段
/// orderType:排序类型
/// pageIndex:当前页
/// pageSize:页大小
/// count:返回查询条数
///
public static DataTable GetPageTable(string sql, string connectstr, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
{
SetConnByString(connectstr);
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:传入要执行sql语句
/// param:参数化
/// orderField:排序字段
/// orderType:排序类型
/// pageIndex:当前页
/// pageSize:页大小
/// count:返回查询条数
///
public static DataTable GetPageTable_EXISTSTemp(string sql, string sqlTempName,string Droptable, string connectstr, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
{
SetConnByString(connectstr);
//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(sql+@" Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
//strSql.Append(" As rowNum, * From " + sqlTempName + " ) As N Where rowNum > " + num + " And rowNum <= " + num1 + " " + Droptable);
//count = Convert.ToInt32(ExecuteScalar(CommandType.Text, sql + " Select Count(1) From " + sqlTempName +" "+ Droptable, param));
//IDataReader dr = ExecuteReader(CommandType.Text, strSql.ToString(), param);
//return DatabaseReader.ReaderToDataTable(dr);
using (SqlConnection conn = GetConnectionString())
{
conn.Open();
sql += " Select Count(1) From " + sqlTempName;
sql += " Select * From (Select ROW_NUMBER() Over (" + OrderBy + ") ";
sql += " As rowNum, * From " + sqlTempName + " ) As N Where rowNum > " + num + " And rowNum <= " + num1 + " " ;
sql += Droptable;
SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
count = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
return ds.Tables[1];
}
}
///
/// 摘要:
/// 数据分页
/// 参数:
/// sql:传入要执行sql语句
/// param:参数化
/// orderField:排序字段
/// orderType:排序类型
/// pageIndex:当前页
/// pageSize:页大小
/// count:返回查询条数
///
public static DataTable GetPageTable_EXISTSTempNew(string sql, string sqlTempName, string Droptable, string connectstr, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
{
SetConnByString(connectstr);
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(sql + @" Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
strSql.Append(" As rowNum, * From " + sqlTempName + " ) As N Where rowNum > " + num + " And rowNum <= " + num1 + " " + Droptable);
count = Convert.ToInt32(ExecuteScalar(CommandType.Text, sql + " Select Count(1) From " + sqlTempName + " " + Droptable, param));
IDataReader dr = ExecuteReader(CommandType.Text, strSql.ToString(), param);
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 根据sql语句和参数,返回DataTable
///
/// sql语句
/// 可变参数
/// DataTable
public static DataTable GetDataTableBySql(string sql, string connectstr, params SqlParameter[] spArr)
{
SetConnByString(connectstr);
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();
cmd.CommandTimeout = 180;
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();
cmd.CommandTimeout = 180;
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
///
/// 执行 SQL 语句,并返回受影响的行数。
///
/// 执行命令的类型(存储过程或T-SQL,等等)
/// 存储过程名称或者T-SQL命令行
///
public static int ExecuteNonQuery_Other(CommandType cmdType, string cmdText, string Connect)
{
int num = 0;
SetConnByString(Connect);
try
{
DbCommand cmd = DbFactory.CreateDbCommand();
using (DbConnection conn = DbFactory.CreateDbConnection(ConnectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
num = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
catch (Exception ex)
{
num = -1;
//log.Error(ex.Message);
}
return num;
}
}
}