using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace GuangFu.StringTracing.Common
{
public class DBHelpe
{
// private readonly static string config = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
private string PriConnectionString = string.Empty;
private static DBHelpe _instance = null;//单例
public DBHelpe()
{
//获取连接字符串
try
{
PriConnectionString = ConfigurationManager.ConnectionStrings["GXWMS"].ToString();
}
catch (Exception ex)
{
throw new Exception(string.Format("未配置{0}库:{1}", "GXWMS", ex.Message));
}
}
///
/// 单例模式
///
///
public static DBHelpe GetInstance()
{
if (_instance == null)
{
_instance = new DBHelpe();
}
return _instance;
}
#region 执行查询,返回要查询的结果集 DataTable
///
/// 执行查询,返回要查询的结果集
///
/// sql语句
///
public DataTable ExecuteTable(string par_SqlStr)
{
return ExecuteTable(par_SqlStr, CommandType.Text, null);
}
///
/// 创建一个DataTable对象
///
/// SQL语句
/// 表名
/// 返回DataSet对象
///
/// 执行查询,返回要查询的结果集
///
/// SQL语句
/// SQL语句的类型(SQL文本命令或存储过程)
///
public DataTable ExecuteTable(string par_SqlStr, CommandType par_CommandType = CommandType.Text)
{
return ExecuteTable(par_SqlStr, par_CommandType, null);
}
///
/// 执行查询,返回要查询的结果集
///
/// SQL语句
/// SQL语句的类型(SQL文本命令或存储过程)
/// 参数列表
///
public DataTable ExecuteTable(string sql, CommandType par_CommandType, params System.Data.SqlClient.SqlParameter[] pParameters)
{
// 保存要查询的结果
DataTable table = new DataTable("dt");
//SqlCommand command;
using (SqlConnection Connection = new SqlConnection(PriConnectionString))
{
//command = new SqlCommand(sql, Connection);
using (SqlCommand command = new SqlCommand(sql, Connection))
{
try
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
// 设置SQL语句的类型
command.CommandType = par_CommandType;
command.CommandTimeout = 36000;
// 如果传入了参数,则添加到命令对象中
if (pParameters != null)
{
for (int i = command.Parameters.Count - 1; i >= 0; i--)
{
command.Parameters.RemoveAt(i);
}
command.Parameters.AddRange(pParameters);
}
// 创建一个适配器对象,用以将结果填充到DataTable中
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(table);
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
finally
{
command.Parameters.Clear();
command.Dispose();
Connection.Close();
}
}
}
return table;
}
#endregion
#region 执行增、删、改操作,返回受影响的行数
///
/// 执行增、删、改操作,返回受影响的行数
///
/// 存储过程名
///
public int ExecuteNonQuery(string par_sql)
{
return ExecuteNonQuery(par_sql, CommandType.Text, null);
}
///
/// 执行增、删、改操作,返回受影响的行数
///
/// SQL语句
/// SQL语句的类型(SQL文本命令或存储过程)
///
public int ExecuteNonQuery(string par_sql, CommandType par_CommandType)
{
return ExecuteNonQuery(par_sql, par_CommandType, null);
}
///
/// 执行增、删、改操作,返回受影响的行数
///
/// SQL语句
/// SQL语句的类型(SQL文本命令或存储过程)
/// 参数列表
///
public int ExecuteNonQuery(string par_sql, CommandType par_CommandType, params System.Data.SqlClient.SqlParameter[] pParameters)
{
// 受影响的行数
int count = 0;
using (SqlConnection Connection = new SqlConnection(PriConnectionString))
{
using (SqlCommand command = new SqlCommand(par_sql, Connection))
{
try
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
// 设置SQL语句的类型
command.CommandType = par_CommandType;
command.CommandTimeout = 36000;
// 如果传入了参数,则添加到命令对象中
if (pParameters != null)
{
for (int i = command.Parameters.Count - 1; i >= 0; i--)
{
command.Parameters.RemoveAt(i);
}
command.Parameters.AddRange(pParameters);
}
count = command.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
finally
{
command.Parameters.Clear();
command.Dispose();
Connection.Close();
}
}
}
return count;
}
#endregion
///
/// 防止数据库参数为NULL
///
/// 参数名
/// 参数值
/// 数据库参数
public static SqlParameter SetParam(string param, object value)
{
if (value == null)
return new SqlParameter(param, "");
return new SqlParameter(param, value);
}
///
/// 判断存储过程是否存在
///
/// 存储过程名
///
public bool IsStoreProcedureExist(string storeProcedureName)
{
string existSql = string.Format(@"select * from sysobjects where id = object_id(N'[{0}]') and OBJECTPROPERTY(id, N'IsProcedure') = 1", storeProcedureName);
DataTable dt = this.ExecuteTable(existSql);
if (dt.Rows.Count > 0)
{
return true;
}
return false;
}
public static DataTable ConvertDataReaderToDataTable(SqlDataReader reader)
{
try
{
DataTable objDataTable = new DataTable();
int intFieldCount = reader.FieldCount;
for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
{
objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
}
objDataTable.BeginLoadData();
object[] objValues = new object[intFieldCount];
while (reader.Read())
{
reader.GetValues(objValues);
objDataTable.LoadDataRow(objValues, true);
}
reader.Close();
objDataTable.EndLoadData();
return objDataTable;
}
catch (Exception ex)
{
throw new Exception("转换出错!", ex);
}
}
}
}