码迷,mamicode.com
首页 > 数据库 > 详细

Sqlhelper.cs--数据库操作类

时间:2015-12-12 18:40:16      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Security.Cryptography;
using System.Text;
using System.Data.Common;

/// <summary>
///Sqlhelper 的摘要说明
/// </summary>
 namespace DbHelp  
{
     public class Sqlhelper
    {
        public Sqlhelper()
        {}
        private static readonly string ConnectString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        public void ExecuteNonQuery(string sql)//执行sql语句    
        {
             using (SqlConnection myconn = new SqlConnection(ConnectString))
             {
                 myconn.Open();
                 SqlCommand mycmd = new SqlCommand(sql, myconn);
                 mycmd.ExecuteNonQuery();
             }
        }
        public void sqlexcute(string sql, SqlParameter[] parameters) //导入excel时用的para方式
        {
                SqlConnection myconn = new SqlConnection(ConnectString);
                myconn.Open();
                SqlCommand mycmd = new SqlCommand(sql, myconn);
                if (parameters != null)
                {
                    foreach (SqlParameter parameter in parameters)
                    {
                        mycmd.Parameters.Add(parameter);
                    }
                }
               mycmd.ExecuteNonQuery();
               myconn.Close();
               mycmd.Parameters.Clear();

        }
        public int ExecuteReturn(string sql)// 返回受影响行数
        {
            using (SqlConnection myconn = new SqlConnection(ConnectString))
            {
                myconn.Open();
                SqlCommand mycmd = new SqlCommand(sql, myconn);
                int rows = Convert.ToInt32(mycmd.ExecuteNonQuery());
                return rows;
            }
        }
        public int ExecutePro(string proname, SqlParameter[] parameters)
        {
            using(SqlConnection myconn = new SqlConnection(ConnectString))
            {
                myconn.Open();
                SqlCommand mycmd = new SqlCommand(proname, myconn);
                mycmd.CommandType = CommandType.StoredProcedure;
                mycmd.CommandText = proname;
                if (parameters != null)
                    {
                      foreach (SqlParameter parameter in parameters)
                        {
                          mycmd.Parameters.Add(parameter);
                        }
                    }
            return mycmd.ExecuteNonQuery();
            }
        }
        public object ExecuteScalar(string sql) //执行查询,返回集合第一行第一列
        {
             using (SqlConnection myconn = new SqlConnection(ConnectString))
            {
                myconn.Open();
                SqlCommand mycmd = new SqlCommand(sql, myconn);
                object data = mycmd.ExecuteScalar();
                return data;
            }
        }

        public SqlDataReader sql_datareader(string sql) //执行sql语句,返回sqldatareader对象
        {
            using (SqlConnection myconn = new SqlConnection(ConnectString))
            {
                myconn.Open();
                SqlCommand mycmd = new SqlCommand(sql, myconn);
                SqlDataReader result = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
                return result;
            }
       }
       public DataSet sql_dataadapter(string  sql) //DataSet填充,返回该对象
       {

                SqlConnection myconn = new SqlConnection(ConnectString);
                myconn.Open();
                //SqlCommand mycmd = new SqlCommand(sql, myconn);
                SqlDataAdapter mydata_ad = new SqlDataAdapter(sql, myconn);
                DataSet mydata_ds = new DataSet();
                mydata_ad.Fill(mydata_ds,"id");//id为内存中表名,可自定义
                  //填充完了后,就可以方便的访问数据了。例如
                  //WriteLine(ds.Tables["ID"].Rows[0][1]);}
                return mydata_ds;
      }
      public DataTable GetDT2(string sql, SqlParameter[] para)//返回datatable,传参数法 
      {
           using (SqlConnection myconn = new SqlConnection(ConnectString))
           {
                myconn.Open();
                SqlCommand mycmd = new SqlCommand(sql, myconn);
                if (para != null)
                   {
                      foreach (SqlParameter parameter in para)
                     {
                        mycmd.Parameters.Add(parameter);
                     }
                   }
                SqlDataAdapter mydata_ad = new SqlDataAdapter(mycmd);
                DataSet ds = new DataSet();
                DataTable dt = new DataTable("table");
                ds.Tables.Add(dt);
                mydata_ad.Fill(ds, "table");
                return dt;
          }
      }
      public DataTable GetDT(string sql)//返回datatable 
      {
           using (SqlConnection myconn = new SqlConnection(ConnectString))
          {
               myconn.Open();
               SqlCommand mycmd = new SqlCommand(sql, myconn);
               SqlDataAdapter mydata_ad = new SqlDataAdapter(mycmd);
               DataSet ds = new DataSet();
               DataTable dt = new DataTable("table");
               ds.Tables.Add(dt);
               mydata_ad.Fill(ds, "table");
               return dt;
          }
      }
      //MD5加密 方法一
      public  string MD5(string Sourcein)
      {
            MD5CryptoServiceProvider MD5CSP = new MD5CryptoServiceProvider();
            byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(Sourcein);
            byte[] MD5Out = MD5CSP.ComputeHash(MD5Source);
            return Convert.ToBase64String(MD5Out);
      }
            //加密二
      public string md5_bit(string str)
      {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)));
            t2 = t2.Replace("-", "");
            t2 = t2.ToLower();
            return t2;
      }
    }
}
  <connectionStrings>//webconfig添加
    <add name="ConnectionString" connectionString="Data Source=localhost(or ip);Initial Catalog=Print;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
  </connectionStrings>

 

Sqlhelper.cs--数据库操作类

标签:

原文地址:http://www.cnblogs.com/lilixiang-go/p/5041459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!