using System.Data.SqlClient;
using System.Data;
using System;
using System.Configuration;
namespace SqlServer.Common
{
public class SqlServerHelper
{
/// <summary>
/// 连接字符串
/// </summary>
private string strconn = string.Empty;
/// <summary>
/// 构造函数
/// </summary>
public DbHelper()
{
//strconn = ConfigurationManager.AppSettings["Conn"].ToString();
strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
}
/// <summary>
/// 执行增删改SQL语句
/// </summary>
/// <param name="cmdText">SQL语句</param>
/// <returns></returns>
public int ExecuteNonQuery(string cmdText)
{
using (SqlConnection conn = new SqlConnection(strconn))
{
conn.Open();
return ExecuteNonQuery(conn, cmdText);
}
}
/// <summary>
/// 执行增删改SQL语句
/// </summary>
/// <param name="conn">SqlConnection</param>
/// <param name="cmdText">SQL语句<</param>
/// <returns></returns>
public int ExecuteNonQuery(SqlConnection conn, string cmdText)
{
int res;
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.CommandType = CommandType.Text;
res = cmd.ExecuteNonQuery();
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
return res;
}
/// <summary>
/// 执行查询SQL语句
/// </summary>
/// <param name="cmdText">SQL语句</param>
/// <returns></returns>
public DataTable ExecuteDataTable(string cmdText)
{
using (SqlConnection conn = new SqlConnection(strconn))
{
conn.Open();
return ExecuteDataTable(conn, cmdText);
}
}
/// <summary>
/// 执行查询SQL语句
/// </summary>
/// <param name="conn">SqlConnection</param>
/// <param name="cmdText">SQL语句</param>
/// <returns></returns>
private DataTable ExecuteDataTable(SqlConnection conn, string cmdText)
{
DataTable dt = new DataTable();
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
}
return dt;
}
/// <summary>
/// 执行查询SQL语句
/// </summary>
/// <param name="cmdText">SQL语句</param>
/// <returns></returns>
public DataTable ExecuteQuery(string cmdText)
{
using (SqlConnection conn = new SqlConnection(strconn))
{
conn.Open();
return ExecuteQuery(conn, cmdText);
}
}
/// <summary>
/// 执行查询SQL语句
/// </summary>
/// <param name="conn">SqlConnection</param>
/// <param name="cmdText">SQL语句</param>
/// <returns></returns>
public DataTable ExecuteQuery(SqlConnection conn, string cmdText)
{
DataTable dt = new DataTable();
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
using (SqlDataReader sdr = cmd.ExecuteReader())
{
dt.Load(sdr);
sdr.Close();
sdr.Dispose();
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
}
return dt;
}
}
}