当前位置:数据库 > SQLServer >>

一个SqlHelper类

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using System.Configuration;
using System.Web; 
/// <summary>
/// SqlHelper 的摘要说明
/// </summary>
public class SqlHelper
{
    private static readonly string connectionString = ConfigurationManager.ConnectionStrings["DataBaseString"].ConnectionString;
    public SqlHelper()  
    {  
    } 
 
    #region 公用方法  
    public static int GetMaxID(string FieldName, string TableName)  
    {  
        string strsql = "select max(" + FieldName + ")+1 from " + TableName;  
        object obj = SqlHelper.GetSingle(strsql);  
        if (obj == null)  
        {  
            return 1;  
        }  
        else 
        {  
            return int.Parse(obj.ToString());  
        }  
    }  
 
    public static bool Exists(string strSql)  
    {  
        object obj = SqlHelper.GetSingle(strSql);  
        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;  
        }  
    }  
 
    public static bool Exists(string strSql, params SqlParameter[] cmdParms)  
    {  
        object obj = SqlHelper.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 
 
    #region  执行简单SQL语句  
    /// <summary>  
    /// 执行SQL语句,返回影响的记录数  
    /// </summary>  
    /// <param name="SQLString">SQL语句</param>  
    /// <returns>影响的记录数</returns>  
    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);  
                }  
            }  
        }  
    }  
 
    /// <summary>

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,