SqlSever帮助类
/// <summary>
/// 数据库访问接⼝
/// internal 只有同⼀命名空间才能访问,防⽌逻辑层调⽤此接⼝
sqltransaction什么意思/// </summary>
public class AgentDBHelper
{
/// <summary>
/// 数据库连接字符串
/// </summary>
internal static string connectionString = ConfigurationManager.ConnectionStrings["AgentDB"].ConnectionString; #region 构造函数
public AgentDBHelper()
{
}
#endregion
#region 执⾏简单SQL语句
/// <summary>
/// 执⾏SQL语句(增删改),返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
internal static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (SqlException e)
{
connection.Close();
throw e;
}
finally
{
connection.Close();
}
}
}
}
/// RunSql(SqlCommand,bool) 执⾏Sql语句
/// </summary>
/// <param name="m_SqlCommand">SqlCommand 对象</param>
/// <returns>影响⾏数</returns>
internal static int ExecuteSql(SqlCommand m_SqlCommand)
{
using (SqlConnection connection = new SqlConnection(connectionString)) {
try
{
m_SqlCommand.Connection = connection;
connection.Open();
return m_SqlCommand.ExecuteNonQuery();
}
catch (SqlException e)
{
throw e;
}
finally
{
connection.Close();
}
}
}
/// 执⾏多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
internal static int ExecuteSqlTran(List<String> SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString)) {
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
int count = 0;
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n];
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
count += cmd.ExecuteNonQuery();
}
}
tx.Commit();
return count;
}
catch
{
tx.Rollback();
return 0;
}
finally
{
conn.Close();
}
}
}
/// 执⾏⼀条语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
internal static object GetSingle(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) {
return null;
}
else
{
return obj;
}
}
catch (SqlException e)
{
throw e;
}
finally
{
connection.Close();
}
}
}
}
/// <summary>
/// 执⾏⼀条语句,返回⼀个值(第⼀⾏第⼀列)
/// </summary>
/// <param name="m_SqlCommand">SqlCommand 对象</param>
/// <returns>object对象</returns>
internal static object GetSingle(SqlCommand m_SqlCommand)
{
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
m_SqlCommand.Connection = conn;
return m_SqlCommand.ExecuteScalar();
}
catch (Exception exE)
{
conn.Close();
throw new Exception("执⾏GetSingleItem错误,请检查Sql语句。\n" + m_SqlCommand.CommandText + System.Environment.NewLine + exE.Message);
}
finally
{
conn.Close();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论