Code
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.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// SqlHelper 的摘要说明
/// </summary>
public class SqlHelper
{
private static readonly string strConn = ConfigurationManager.ConnectionStrings["TangCompanyConn"].ConnectionString;
private static readonly string strName = ConfigurationManager.AppSettings["userName"].ToString();
private static readonly string strPass = ConfigurationManager.AppSettings["userPass"].ToString();
public SqlHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlDataReader ExcuteRead(string nText, CommandType nType, SqlParameter[] paras) {
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(conn, cmd, null, nType, nText, paras);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
}
public static int ExcuteNonQurey(string nText, CommandType nType, SqlParameter[] paras) {
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(strConn)) {
PrepareCommand(conn, cmd, null, nType, nText, paras);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
}
//返回第一列第一个object 类型数据 其他的都被省除了
public static object ExcuteSclare(string nText, CommandType nType, SqlParameter[] paras) {
SqlCommand cmd = new SqlCommand();
//利用 .NET 的 GC 机制,应用于实现了 IDisposable 接口的对象 using的作用
using (SqlConnection conn = new SqlConnection(strConn))
{
PrepareCommand(conn, cmd, null, nType, nText, paras);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return obj;
}
//using (定义一个对象)
//{
// 。。[范围]。。。//当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose[清除]。
//}
}
public static DataSet ExcuteReadApdater(string nText, CommandType nType, SqlParameter[] paras) {
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
try
{
PrepareCommand(con, cmd, null, nType, nText, paras);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
sqlAdapter.Fill(ds);
cmd.Parameters.Clear();
con.Close();
return ds;
}
catch (SqlException ex) {
throw new Exception(ex.Message, ex);
}
}
public static void PrepareCommand(SqlConnection con, SqlCommand cmd, SqlTransaction trans, CommandType nType, string nText, SqlParameter[] paras)
{
if (con.State != ConnectionState.Open)
con.Open();
cmd.Connection = con;
cmd.CommandText = nText;
if (trans != null) {
cmd.Transaction = trans;
}
cmd.CommandType = nType;
if (paras != null) {
foreach (SqlParameter para in paras) {
cmd.Parameters.Add(para);
}
}
}
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.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// SqlHelper 的摘要说明
/// </summary>
public class SqlHelper
{
private static readonly string strConn = ConfigurationManager.ConnectionStrings["TangCompanyConn"].ConnectionString;
private static readonly string strName = ConfigurationManager.AppSettings["userName"].ToString();
private static readonly string strPass = ConfigurationManager.AppSettings["userPass"].ToString();
public SqlHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlDataReader ExcuteRead(string nText, CommandType nType, SqlParameter[] paras) {
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(conn, cmd, null, nType, nText, paras);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
}
public static int ExcuteNonQurey(string nText, CommandType nType, SqlParameter[] paras) {
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(strConn)) {
PrepareCommand(conn, cmd, null, nType, nText, paras);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
}
//返回第一列第一个object 类型数据 其他的都被省除了
public static object ExcuteSclare(string nText, CommandType nType, SqlParameter[] paras) {
SqlCommand cmd = new SqlCommand();
//利用 .NET 的 GC 机制,应用于实现了 IDisposable 接口的对象 using的作用
using (SqlConnection conn = new SqlConnection(strConn))
{
PrepareCommand(conn, cmd, null, nType, nText, paras);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return obj;
}
//using (定义一个对象)
//{
// 。。[范围]。。。//当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose[清除]。
//}
}
public static DataSet ExcuteReadApdater(string nText, CommandType nType, SqlParameter[] paras) {
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
try
{
PrepareCommand(con, cmd, null, nType, nText, paras);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
sqlAdapter.Fill(ds);
cmd.Parameters.Clear();
con.Close();
return ds;
}
catch (SqlException ex) {
throw new Exception(ex.Message, ex);
}
}
public static void PrepareCommand(SqlConnection con, SqlCommand cmd, SqlTransaction trans, CommandType nType, string nText, SqlParameter[] paras)
{
if (con.State != ConnectionState.Open)
con.Open();
cmd.Connection = con;
cmd.CommandText = nText;
if (trans != null) {
cmd.Transaction = trans;
}
cmd.CommandType = nType;
if (paras != null) {
foreach (SqlParameter para in paras) {
cmd.Parameters.Add(para);
}
}
}