日期:2014-05-16  浏览次数:20459 次

c#数据库帮助类SqlHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlClient;
namespace DAL
{
    public class SqlHelper
    {
        public static string connstring = ConfigurationManager.ConnectionStrings["zzconstr"].ConnectionString;
        //public static string connstring = "Data Source=ZZ-PC;Initial Catalog=IPTVDB;User ID=sa;Password=sa";
        /// <summary>
        /// 执行非查询,返回受影响行数,异常返回-1;
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="type"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public static bool ExceNonQuery(string sql, CommandType type, IDataParameter[] pars)
        {
            
            SqlConnection con = new SqlConnection(connstring);
            SqlCommand com = new SqlCommand(sql, con);
            
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter pp in pars)//把参数集全部加进去
                    com.Parameters.Add(pp);
            }
            try
            {
                con.Open();
                int t = com.ExecuteNonQuery();
                if (t > 0)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception e) { return false; }
            finally
            {
                com.Parameters.Clear();
                com.Dispose();
                con.Close();
            }
        }
        /// <summary>
        /// 执行sql语句的查询,返回查询的数量。异常返回-1.
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="type"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public static int ExceQuery(string sql, CommandType type, IDataParameter[] pars)
        {
            SqlConnection con = new SqlConnection(connstring);
            SqlCommand com = new SqlCommand(sql, con);
            com.CommandType = type;
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter pp in pars)//把参数集全部加进去
                    com.Parameters.Add(pp);
            }
            try
            {
                con.Open();
                if (com.ExecuteScalar() != null)//查询结果为空时返回0
                {
                    int t = (int)com.ExecuteScalar();

                    return t;
                }
                else
                    return -1;
            }
            catch (Exception e) { return -1; }
            finally
            {
                com.Parameters.Clear();
                com.Dispose();
                con.Close();
            }
        }
        /// <summary>
        /// 执行查询,返回一个数据集
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public static DataSet ExcueReturnDataset(string sql, IDataParameter[] pars)
        {
            SqlConnection con = new SqlConnection(connstring);
            DataSet set = new DataSet();
            SqlCommand com = new SqlCommand(sql, con);
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter pp in pars)//把参数集全部加进去
                    com.Parameters.Add(pp);
            }
            
            SqlDataAdapter adpter = new SqlDataAdapter(com);
            
            try
            {
                set.Clear();
                adpter.Fill(set);
                return set;
            }