using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text; namespace YcTools
{
/// <summary>C# SQL数据库助手类2.0</summary>
public class YSqlHelper
{
//Sql连接语句
/*注意引用System.Configuration
* <connectionStrings>
* <add name="YcSqlCon"
* connectionString="Data Source=服务器;Initial Catalog=数据库;User ID=登录名;Password=密码"
* providerName="System.Data.SqlClient"/>
* </connectionStrings>
*/
// private string connectionString = ConfigurationManager.ConnectionStrings["YcSqlCon"].ConnectionString;
private string connectionString = "";
public YSqlHelper(string sqlPath)
{
//实例化对应的数据库链接
connectionString = ConfigurationManager.ConnectionStrings[sqlPath].ConnectionString;
} /// <summary>执行不带参数的增删改SQL语句或存储过程 ,返回受影响的行数</summary>
public int ExecuteNonQuery(string cmdText)
{
int res = ;//受影响的行数
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();//打开数据库链接
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.CommandType = CommandType.Text;
res = cmd.ExecuteNonQuery();//执行Sql语句并受影响的行数
}
}
catch
{ }
finally
{
if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
{
conn.Close();//关闭与数据库的链接
}
}
}
return res;
} /// <summary> 执行带参数的增删改SQL语句或存储过程,返回受影响的行数</summary>
public int ExecuteNonQuery(string cmdText, SqlParameter[] paras)
{
int res = ;//受影响的行数
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();//打开数据库链接
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddRange(paras);
res = cmd.ExecuteNonQuery();//执行Sql语句并受影响的行数
}
}
catch
{ }
finally
{
if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
{
conn.Close();//关闭与数据库的链接
}
}
}
return res;
} /// <summary> 执行不带参数的查询SQL语句或存储过程,返回DataTable对象</summary>
public DataTable ExecuteQueryDataTable(string cmdText)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();//打开数据库链接
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.CommandType = CommandType.Text;
using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
}
}
catch
{ }
finally
{
if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
{
conn.Close();//关闭与数据库的链接
}
}
}
return dt;
} /// <summary> 执行带参数的查询SQL语句或存储过程,返回DataTable对象</summary>
public DataTable ExecuteQueryDataTable(string cmdText, SqlParameter[] paras)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();//打开数据库链接
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddRange(paras);
using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
}
}
catch
{ }
finally
{
if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
{
conn.Close();//关闭与数据库的链接
}
}
}
return dt;
} /// <summary> 执行不带参数的查询SQL语句或存储过程,返回DataSet对象</summary>
public DataSet ExecuteQueryDataSet(string cmdText)
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();//打开数据库链接
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds, "ds");
}
}
}
catch
{ }
finally
{
if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
{
conn.Close();//关闭与数据库的链接
}
}
}
return ds;
} /// <summary> 执行带参数的查询SQL语句或存储过程,返回DataSet对象</summary>
public DataSet ExecuteQueryDataSet(string cmdText, SqlParameter[] paras)
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();//打开数据库链接
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddRange(paras);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds, "ds");
}
}
}
catch
{ }
finally
{
if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态
{
conn.Close();//关闭与数据库的链接
}
}
}
return ds;
} /// <summary>查询数据是否存在</summary>
public bool ExecuteDataIsExistByData(string sqlStr)
{
bool iss = false;
DataSet ds = ExecuteQueryDataSet(sqlStr);
for (int i = ; i < ds.Tables.Count; i++)
{
if (ds.Tables[i].Rows.Count > ) iss = true;
}
return iss;
} /// <summary>查询数据是否存在 </summary>
public bool ExecuteDataIsExistByData(string sqlStr, SqlParameter[] paras)
{
bool iss = false;
DataSet ds = ExecuteQueryDataSet(sqlStr, paras);
for (int i = ; i < ds.Tables.Count; i++)
{
if (ds.Tables[i].Rows.Count > ) iss = true;
}
return iss;
} /// <summary>查询增删改数据操作是否成功 </summary>
public bool ExecuteDataIsExistByInt(string sqlStr)
{
int ds = ExecuteNonQuery(sqlStr);
bool iss = ds > ? true : false;
return iss;
} /// <summary>查询增删改数据操作是否成功 </summary>
public bool ExecuteDataIsExistByInt(string sqlStr, SqlParameter[] paras)
{
int ds = ExecuteNonQuery(sqlStr, paras);
bool iss = ds > ? true : false;
return iss;
}
}
}