264 lines
9.8 KiB
Plaintext
Executable File
264 lines
9.8 KiB
Plaintext
Executable File
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));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 单例模式
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static DBHelpe GetInstance()
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = new DBHelpe();
|
|
}
|
|
return _instance;
|
|
}
|
|
|
|
#region 执行查询,返回要查询的结果集 DataTable
|
|
|
|
/// <summary>
|
|
/// 执行查询,返回要查询的结果集
|
|
/// </summary>
|
|
/// <param name="sql">sql语句</param>
|
|
/// <returns></returns>
|
|
public DataTable ExecuteTable(string par_SqlStr)
|
|
{
|
|
return ExecuteTable(par_SqlStr, CommandType.Text, null);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 创建一个DataTable对象
|
|
/// </summary>
|
|
/// <param name="M_str_par_SqlStr">SQL语句</param>
|
|
/// <param name="M_str_table">表名</param>
|
|
/// <returns>返回DataSet对象</returns>
|
|
/// <summary>
|
|
/// 执行查询,返回要查询的结果集
|
|
/// </summary>
|
|
/// <param name="sql">SQL语句</param>
|
|
/// <param name="par_CommandType">SQL语句的类型(SQL文本命令或存储过程)</param>
|
|
/// <returns></returns>
|
|
public DataTable ExecuteTable(string par_SqlStr, CommandType par_CommandType = CommandType.Text)
|
|
{
|
|
return ExecuteTable(par_SqlStr, par_CommandType, null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行查询,返回要查询的结果集
|
|
/// </summary>
|
|
/// <param name="sql">SQL语句</param>
|
|
/// <param name="par_CommandType">SQL语句的类型(SQL文本命令或存储过程)</param>
|
|
/// <param name="parameters">参数列表</param>
|
|
/// <returns></returns>
|
|
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 执行增、删、改操作,返回受影响的行数
|
|
/// <summary>
|
|
/// 执行增、删、改操作,返回受影响的行数
|
|
/// </summary>
|
|
/// <param name="sql">存储过程名</param>
|
|
/// <returns></returns>
|
|
public int ExecuteNonQuery(string par_sql)
|
|
{
|
|
return ExecuteNonQuery(par_sql, CommandType.Text, null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行增、删、改操作,返回受影响的行数
|
|
/// </summary>
|
|
/// <param name="sql">SQL语句</param>
|
|
/// <param name="par_CommandType">SQL语句的类型(SQL文本命令或存储过程)</param>
|
|
/// <returns></returns>
|
|
public int ExecuteNonQuery(string par_sql, CommandType par_CommandType)
|
|
{
|
|
return ExecuteNonQuery(par_sql, par_CommandType, null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行增、删、改操作,返回受影响的行数
|
|
/// </summary>
|
|
/// <param name="sql">SQL语句</param>
|
|
/// <param name="par_CommandType">SQL语句的类型(SQL文本命令或存储过程)</param>
|
|
/// <param name="parameters">参数列表</param>
|
|
/// <returns></returns>
|
|
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
|
|
|
|
/// <summary>
|
|
/// 防止数据库参数为NULL
|
|
/// </summary>
|
|
/// <param name="param">参数名</param>
|
|
/// <param name="value">参数值</param>
|
|
/// <returns>数据库参数</returns>
|
|
public static SqlParameter SetParam(string param, object value)
|
|
{
|
|
if (value == null)
|
|
return new SqlParameter(param, "");
|
|
return new SqlParameter(param, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断存储过程是否存在
|
|
/// </summary>
|
|
/// <param name="storeProcedureName">存储过程名</param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
} |