• 大小: 20KB
    文件类型: .cs
    金币: 1
    下载: 0 次
    发布日期: 2021-05-29
  • 语言: C#
  • 标签: DbHelper  C#  

资源简介

特别全面的DbHelper,节省大家时间,欢迎大家下载。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;

namespace Com.Caresoft.BPM.Common
{
    public abstract class DBHelper
    {
        //数据库连接字符串(web.config来配置)
        protected static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[“Caresoft“].ConnectionString;
            
        public DBHelper()
        {
        }

        #region 公用方法

        public static int GetMaxID(string FieldName string TableName)
        {
            string strsql = “select max(“ + FieldName + “)+1 from “ + TableName;
            object obj = GetSingle(strsql);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }
        public static bool Exists(string strSql params SqlParameter[] cmdParms)
        {
            object obj = GetSingle(strSql cmdParms);
            int cmdresult;
            if ((object.Equals(obj null)) || (object.Equals(obj System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion

        //执行简单SQL语句#region  执行简单SQL语句

        /**/
        /// 
        /// 执行SQL语句,返回影响的记录数
        /// 

        /// SQL语句
        /// 影响的记录数
        public 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 (System.Data.SqlClient.SqlException E)
                    {
                        connection.Close();
                        throw new Exception(E.Message);
                    }
                }
            }
        }

        /**/
        /// 
        /// 执行多条SQL语句,实现数据库事务。
        /// 

        /// 多条SQL语句  
        public static void ExecuteSqlTran(ArrayList SQLStringList)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
     

评论

共有 条评论