using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace ShipTracking.Util
{
/// <summary>
/// Summary description for SQLHelper.
/// </summary>
public class SQLHelper
{
// 连接数据源
private SqlConnection myConnection;
private readonly string RETURNVALUE = "RETURNVALUE";
#region SQLHelper
public SQLHelper()
{
//
// TODO: Add constructor logic here
//
}
#endregion
#region Open
/// <summary>
/// 打开数据库连接.
/// </summary>
private void Open()
{
// 打开数据库连接
if (myConnection == null)
{
myConnection = new SqlConnection(ConfigurationSettings.AppSettings["dbConnection"].ToString());
}
if(myConnection.State == ConnectionState.Closed)
{
try
{
///打开数据库连接
myConnection.Open();
}
catch(Exception ex)
{
//SystemError.SystemLog(ex.Message);
throw ex;
}
finally
{
///关闭已经打开的数据库连接
Close();
}
}
}
#endregion
#region Close
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
///判断连接是否已经创建
if(myConnection != null)
{
///判断连接的状态是否打开
if(myConnection.State == ConnectionState.Open)
{
myConnection.Close();
}
}
}
#endregion
#region Dispose
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
// 确认连接是否已经关闭
if (myConnection != null)
{
myConnection.Dispose();
myConnection = null;
}
}
#endregion
#region DBconn返回数据库连接
/// <summary>
/// 公开方法DBConn,返回数据库连接
/// </summary>
/// <returns></returns>
public SqlConnection DBconn()
{
string strConn = ConfigurationSettings.AppSettings["dbConnection"].ToString();
try
{
return new SqlConnection(strConn);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region Select
/// <summary>
/// 根据查询语句从数据库检索数据
/// </summary>
/// <param name="strSelect">查询语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>有数据则返回DataSet对象,否则返回null</returns>
public DataSet Select(string SelectString)
{
try
{
///打开数据库连接
Open();
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
SqlCommand selectCommand = new SqlCommand(SelectString, myConnection);
selectCommand.CommandType = CommandType.Text;
mySqlDataAdapter.SelectCommand = selectCommand;
DataSet myDS = new DataSet();
mySqlDataAdapter.Fill(myDS);
return myDS;
}
catch (Exception ex)
{
throw ex;
}
finally
{
Close();
}
}
#endregion
#region Update
/// <summary>
/// 更新数据库
/// </summary>
/// <param name="UpdateString">Update Sql语句</param>
/// <returns>返回受影响行数</returns>
public int Update(string UpdateString)
{
try
{
return udiDataBase(UpdateString);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region Delete
/// <summary>
/// 从数据库中删除数据
/// </summary>
/// <param name="DeleteString">Delete Sql语句</param>
/// <returns>返回受影响行数</returns>
public int Delete(string DeleteString)
{
try
{
return udiDataBase(DeleteString);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region Insert
/// <summary>
/// 把数据插入数据库
/// </summary>
/// <param name="InsertString">Insert Sql语句</param>
/// <returns>返回受影响行数</returns>
public int Insert(string InsertString)
{
try
{
return udiDataBase(InsertString);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region udiDataBase
/// <summary>
/// 根据Sql语句更新数据库
/// </summary>
/// <param name="UDIString">更新语句</param>
/// <returns>返回受影响行数</returns>
public int udiDataBase(string UDIString)
{
try
{
///打开数据库连接
Open();
SqlCommand cmd = new SqlCommand(UDIString, myConnection);
cmd.CommandType = CommandType.Text;
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
///关闭数据库的连接
Close();
}
}
#endregion
//以下为存储过程处理
#region RunProc执行存储过程
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <returns>返回存储过程返回值</returns>
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
try
{
///执行存储过程
cmd.ExecuteNonQuery();
///返回存储过程的参数值
return (int)cmd.Parameters[RETURNVALUE].Value;
}
catch(Exception ex)
{
///记录错误日志
//SystemError.SystemLog(ex.Message);
throw ex;
}
finally
{
///关闭数据库的连接
Close();
}
}
#endregion
#region RunProc执行存储过程
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <returns>返回存储过程返回值</returns>
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
try
{
///执行存储过程
cmd.ExecuteNonQuery();
///返回存储过程的参数值
return (int)cmd.Parameters[RETURNVALUE].Value;
}
catch(Exception ex)
{
///记录错误日志
//SystemError.SystemLog(ex.Message);
throw ex;
}
finally
{
///关闭数据库的连接
Close();
}
}
#endregion
#region RunProc执行存储过程
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="dataReader">返回存储过程返回值</param>
public void RunProc(string procName, out SqlDataReader dataReader)
{
try
{
///创建Command
SqlCommand cmd = CreateCommand(procName, null);
///读取数据
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
///记录错误日志
//SystemError.SystemLog(ex.Message);
throw ex;
}
finally
{
///关闭数据库的连接
Close();
}
}
#endregion
#region RunProc执行存储过程
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <param name="dataReader">存储过程所需参数</param>
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
try
{
///创建Command
SqlCommand cmd = CreateCommand(procName, prams);
///读取数据
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
///记录错误日志
//SystemError.SystemLog(ex.Message);
throw ex;
}
finally
{
///关闭数据库的连接
Close();
}
}
#endregion
#region CreateCommand
/// <summary>
/// 创建一个SqlCommand对象以此来执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <returns>返回SqlCommand对象</returns>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
try
{
///打开数据库连接
Open();
///设置Command
SqlCommand cmd = new SqlCommand(procName, myConnection);
cmd.CommandType = CommandType.StoredProcedure;
///添加把存储过程的参数
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
cmd.Parameters.Add(parameter);
}
}
///添加返回参数ReturnValue
cmd.Parameters.Add(
new SqlParameter(RETURNVALUE, SqlDbType.Int,4,ParameterDirection.ReturnValue,
false,0,0,string.Empty, DataRowVersion.Default,null));
///返回创建的SqlCommand对象
return cmd;
}
catch(Exception ex)
{
///记录错误日志
//SystemError.SystemLog(ex.Message);
throw ex;
}
finally
{
///关闭数据库的连接
Close();
}
}
#endregion
#region CreateParam
/// <summary>
/// 生成存储过程参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Direction">参数方向</param>
/// <param name="Value">参数值</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter CreateParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
try
{
SqlParameter param;
///当参数大小为0时,不使用该参数大小值
if(Size > 0)
{
param = new SqlParameter(ParamName, DbType, Size);
}
else
{
///当参数大小为0时,不使用该参数大小值
param = new SqlParameter(ParamName, DbType);
}
///创建输出类型的参数
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
{
param.Value = Value;
}
///返回创建的参数
return param;
}
catch(Exception ex)
{
///记录错误日志
//SystemError.SystemLog(ex.Message);
throw ex;
}
finally
{
///关闭数据库的连接
Close();
}
}
#endregion
#region CreateInParam
/// <summary>
/// 传入输入参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param></param>
/// <param name="Size">参数大小</param>
/// <param name="Value">参数值</param>
/// <returns>新的parameter 对象</returns>
public SqlParameter CreateInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
try
{
///创建输入类型的参数
return CreateParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
catch(Exception ex)
{
///记录错误日志
//SystemError.SystemLog(ex.Message);
throw ex;
}
}
#endregion
#region CreateOutParam
/// <summary>
/// 传入返回值参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter CreateOutParam(string ParamName, SqlDbType DbType, int Size)
{
try
{
///创建输出类型的参数
return CreateParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
catch(Exception ex)
{
///记录错误日志
//SystemError.SystemLog(ex.Message);
throw ex;
}
}
#endregion
#region CreateReturnParam
/// <summary>
/// 传入返回值参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter CreateReturnParam(string ParamName, SqlDbType DbType, int Size)
{
try
{
///创建返回类型的参数
return CreateParam(ParamName, DbType, Size, ParameterDirection.ReturnValue, null);
}
catch(Exception ex)
{
///记录错误日志
//SystemError.SystemLog(ex.Message);
throw ex;
}
}
#endregion
}
}