SqlSever帮助类

更新时间:2023-05-24 22:55:32 阅读: 评论:0

SqlSever帮助类
/// <summary>
/// 数据库访问接⼝
/// internal 只有同⼀命名空间才能访问,防⽌逻辑层调⽤此接⼝
/// </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.Clo();
throw e;
}
finally
{
connection.Clo();
}
}
}
}
/// 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.Clo();
育儿小知识}
}
}
宝宝几个月会爬/// 执⾏多条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.Clo();
}
}
阴吹是什么原因}
/// 执⾏⼀条语句,返回查询结果(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;
}
el
{
return obj;
}
}
catch (SqlException e)
{
throw e;
}
finally
{
connection.Clo();
}
}
}
}
/// <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.Clo();
throw new Exception("执⾏GetSingleItem错误,请检查Sql语句。\n" + m_SqlCommand.CommandText + System.Environment.NewLine + exE.Message);
}
finally
{
conn.Clo();
出口信贷}
}

本文发布于:2023-05-24 22:55:32,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/927303.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:语句   返回   数据库   访问   结果
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图