using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
///SqlHelper 的摘要说明
/// </summary>
public class SqlDataHelper
{
public SqlDataHelper()
{

}

//数据库连接字符串
private readonly static string connectionString = ConfigurationManager.ConnectionStrings["default"].ToString();

/// <summary>
/// 执行增,删,改的方法,支持存储过程
/// </summary>
/// <param name="connectionString">连接字符串,自满足,请用SqlHelper.connString赋值</param>
/// <param name="commandType">命令类型,如果是sql语句,则为CommandType.Text,否则为CommandType.StoredProcdure</param>
/// <param name="cmdText">SQL语句或者存储过程名称</param>
/// <param name="para">SQL参数,如果没有参数,则为null</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(CommandType commandType, string cmdText, SqlParameter[] para)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = commandType;
cmd.CommandText = cmdText;
if (para != null)
{
cmd.Parameters.AddRange(para);
}
conn.Open();
int no = Convert.ToInt32(cmd.ExecuteNonQuery());
cmd.Parameters.Clear();
return no;
}
}

/// <summary>
/// 执行查询的方法,支持存储过程
/// </summary>
/// <param name="connectionString">连接字符串,自满足,请用SqlHelper.connString赋值</param>
/// <param name="commandType">命令类型,如果是sql语句,则为CommandType.Text,否则为CommandType.StoredProcdure</param>
/// <param name="cmdText">SQL语句或者存储过程名称</param>
/// <param name="para">SQL参数,如果没有参数,则为null</param>
/// <returns>读取器SqlDataReader</returns>
public static SqlDataReader ExecuteReader(CommandType commandType, string cmdText, SqlParameter[] para)
{
SqlConnection conn = new SqlConnection(connectionString);
SqlDataReader dr = null;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = commandType;
cmd.CommandText = cmdText;
if (para != null)
{
cmd.Parameters.AddRange(para);
}
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}

/// <summary>
/// 执行查询的方法,支持存储过程
/// </summary>
/// <param name="connectionString">连接字符串,自满足,请用SqlHelper.connString赋值</param>
/// <param name="commandType">命令类型,如果是sql语句,则为CommandType.Text,否则为CommandType.StoredProcdure</param>
/// <param name="cmdText">SQL语句或者存储过程名称</param>
/// <param name="para">SQL参数,如果没有参数,则为null</param>
/// <returns>数据集</returns>
public static DataSet GetDataSet(CommandType commandType, string cmdText, SqlParameter[] para)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = cmdText;
da.SelectCommand.CommandType = commandType;
if (para != null)
{
da.SelectCommand.Parameters.AddRange(para);
}
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
da.SelectCommand.Parameters.Clear();
return ds;
}
}

/// <summary>
/// 执行查询的方法,支持存储过程
/// <summary>
/// 执行查询的方法,支持存储过程
/// </summary>
/// <param name="connectionString">连接字符串,自满足,请用SqlHelper.connString赋值</param>
/// <param name="commandType">命令类型,如果是sql语句,则为CommandType.Text,否则为CommandType.StoredProcdure</param>
/// <param name="cmdText">SQL语句或者存储过程名称</param>
/// <param name="para">SQL参数,如果没有参数,则为null</param>
/// <returns>返回查询结果的第一行第一列</returns>
public static object GetScalar(CommandType commandType, string cmdText, SqlParameter[] para)
{
object result = null;
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = commandType;
cmd.CommandText = cmdText;
if (para != null)
{
cmd.Parameters.AddRange(para);
}
conn.Open();
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
return result;
}

}

posted on 2016-01-18 15:17  邢帅杰  阅读(386)  评论(0编辑  收藏  举报