using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Tandy.Helper
{
/// <summary>
///
/// 类名:DBHelper
///
/// 作用:用于处理对MSSQL数据库的增删改查操作
///
/// 作者:汤晓华
///
/// 创建日期:2009年6月20日
///
/// </summary>
[Serializable()]
public class DBHelper
{
//连接字符串
private static readonly string connectionString =
ConfigurationManager.ConnectionStrings["mssqlConnString"].ConnectionString;
#region 方法
/// <summary>
/// 执行SQL语句或者存储过程的非查询操作
/// </summary>
/// <param name="cmdText">SQL语句或存储过程</param>
/// <param name="cmdType">命令字符串类型</param>
/// <param name="parameters">参数</param>
/// <returns>INT 受影响行数</returns>
public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
//Sql命令处理对象
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
//处理SqlCommand对象
PrepareCommand(cmd, connection, cmdText, cmdType, parameters);
//声明异步操作
IAsyncResult async = cmd.BeginExecuteNonQuery();
int val = cmd.EndExecuteNonQuery(async);
cmd.Dispose();
return val;
}
catch (SqlException ex)
{
throw ex;
}
}
}
/// <summary>
/// 返回数据集的首行首列
/// </summary>
/// <param name="cmdText">SQL语句或存储过程</param>
/// <param name="cmdType">命令字符串类型</param>
/// <param name="parameters">参数</param>
/// <returns>返回首行首页</returns>
public static object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
//Sql命令处理对象
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
//处理SqlCommand对象
PrepareCommand(cmd, connection, cmdText, cmdType, parameters);
object val = cmd.ExecuteScalar();
cmd.Dispose();
return val;
}
catch (SqlException ex)
{
throw ex;
}
}
}
/// <summary>
/// 获取数据流
/// </summary>
/// <param name="cmdText">SQL语句或存储过程</param>
/// <param name="cmdType">命令字符串类型</param>
/// <param name="parameters">参数</param>
/// <returns>SqlDataReader对象</returns>
public static SqlDataReader ExecuteReader(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
//Sql命令处理对象
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
PrepareCommand(cmd, connection, cmdText, cmdType, parameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rdr;
}
catch (SqlException ex)
{
throw ex;
}
}
}
/// <summary>
/// 获取数据集
/// </summary>
/// <param name="cmdText">SQL语句或存储过程</param>
/// <param name="cmdType">命令字符串类型</param>
/// <param name="parameters">参数</param>
/// <returns>DataTable</returns>
public static DataTable GetDataTable(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
PrepareCommand(cmd, connection, cmdText, cmdType, parameters);
DataTable table = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(table);
sda.Dispose();
cmd.Dispose();
return table;
}
catch (SqlException ex)
{
throw ex;
}
}
}
/// <summary>
/// 获取数据集
/// </summary>
/// <param name="cmdText">SQL语句或存储过程</param>
/// <param name="cmdType">命令字符串类型</param>
/// <param name="parameters">参数</param>
/// <returns>DataTable</returns>
public static SqlDataAdapter GetSqlDataAdapter(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
PrepareCommand(cmd, connection, cmdText, cmdType, parameters);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
return sda;
}
catch (SqlException ex)
{
throw ex;
}
}
}
/// <summary>
/// 处理SqlCommand对象
/// </summary>
/// <param name="cmdText">SQL语句或存储过程</param>
/// <param name="cmdType">命令字符串类型</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
private static void PrepareCommand(SqlCommand cmd, SqlConnection connection, string cmdText, CommandType cmdType, SqlParameter[] parameters)
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
cmd.Connection = connection;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
if (null != parameters)
{
cmd.Parameters.Clear();
cmd.Parameters.AddRange(parameters);
}
}
#endregion
}
}