MsSqlHelper类库

1 篇文章 1 订阅
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;


namespace YF.MsSqlHelper
{
    public static class YFMsSqlHelper
    {
        //连接字符串
        public static readonly string connectionString = ConfigurationManager.ConnectionStrings["xiaobai"].ConnectionString;

        public static int GetMaxID(string FieldName,string TableName)
        {
			string strsql = "select max(" + FieldName +") +1 from * + TableName;
			object obj = GetString(strsql);
			if(obj == null)
			{
	   		  return 1;
			}
			else
			{
	 		   return int.paras(obj.ToString());
			}
        }

        public static bool Exists(string strSql)
        {
			object obj = GetString(strSql);
			int cmdresult;
			if(Object.Equals(obj,null)) || 		(Object.Equals(obj,System.DBNull.Value)))
			{
				cmdresult =0;
			}
			else
			{
				cmdresult = int.Parse(obj.Tostrign());
			}
			if(cmdresult == 0)
			{
				return false;
			}
			else
			{
				return true;
			}
       }


     //执行SQL语句,返回影响的记录数
     public static int ExecuteSql(string SQLString)
      {
            using(SqlConnection connection = new SqlConnection(connectionString))
             {
				try
				{
	     		 	 connection.Open();
	      		 	 int rows = cmd.ExecuteNonQuery();
	     		 	 return rows;
				}
				catch(SqlException E)
				{
	     		 	connection.Close();
	     		 	throw new Exception(E.Message);
				}
            }
        }
    

       //设置SQL语句,设置命令的执行等待时间
       public static int ExecuteSqlByTime(string SQLString ,int Times)
       {
           using (SqlConnection cmd = new SqlConnection(connectionString))
           {
				using (SqlCommand cmd = new SqlCommand(SQLString,connection))
				{
		 		    try
	     			 {
							connection.Open();
							cmd.CommandTimeout = Times;
							int rows = cmd.ExecuteNonQuery();
	    			  }	
	     			 catch(SqlException E)
	      			 {
						connection.Close();
						throw new Exception(E.Message);
	      			 }
				}
           }
       }
       
       //执行多条SQL语句,实现数据库事务
        public static void ExecuteSqlTran(ArrayList SQLStringList)
        {
           using (SqlConnection conn = new SqlConnection(connectionString))
           {
				conn.Open();
				SqlCommand cmd = new SqlCommand();
				cmd.Connection = conn;
				SqlTransaction tx = conn.BeginTransaction();
				cmd.Transaction = tx;
				try
				{
	    			for(int n=0; n<SQLStringList.Count;n++)
	    			{
						string strsql = SQLStringList[n].ToString();
						if(strsql.Trim().Length >1)
						{
		   					 cmd.CommandText = strsql;
		    				 cmd.ExecuteNonQuery();
						}
	  				 }
	   				 tx.Commit();
				}
				catch (SqlException E)
				{
					tx.Rollback();
					throw new Exception(E.Message);
				}
          }
     }
     //执行一个带存储参数过程的SQL语句
     public static int ExecuteSql(string SQLString,string content)
     {
           using (SqlConnection connection = new SqlConnection(connection))
           {
				SqlCommand cmd = new SqlCommand(SQLString,connection);
				SqlParameter myParameter = new SqlParameter("@content",SqlDbType.NText);
				myParameter.Value = content;
				cmd.Parameters.Add(myparameter);
                 try
				{
					connection.Open();
					int rows =cmd.ExecuteNonQuery();
					return rows;
				}
				catch(SqlException E)
				{
					throw new Exception(E.Message);
				}
				finally
				{
	  			 	cmd.Dispose();
	  			 	connection.Close();
				}
          }
     }
     //执行带一个存储过程参数的SQL语句
      public static object ExecuteSqlGet(string SQLString , string content)
      {
           using (SqlConnection connection = new SqlConnection(connectionString))
           {
				SqlCommand cmd = new SqlCommand(SQLString , connection);
				SqlParameter myParameter = new SqlParameter("@content",SqlDbType.Ntext);
				myParameter.Value = content;
				cmd.Parameter.Add(myParameter);
			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 new Exception(E.Message);
			}
			finally
			{
				cmd.Dispose();
				connection.Close();
			}
         }
    }

   //向数据库中插入图像格式的字段(和上面情况类似的一种实例)
    public static int ExecuteSqlInsertImg(string strSQL,byte [] fs)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
			SqlCommand cmd = new SqlCommand(strSQL,connection);	
			SqlParameter myParameter = new SqlParameter("@fs",SqlDbType.Image);
			myParameter.Value = fs;
			cmd.Parameters.Add(myParameter);
			try
			{
				connection.Open();
				int rows = cmd.ExecuteNonQuery();
				return rows;
			}
			catch(SqlException E)
			{
				throw new Exception(E.Message);
			}
			finally
			{
				cmd.Dispose();
				connection.Close();
			}
      }
    }
}















  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值