• DBHelper类


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    namespace Business.DAL
     { 
        public static class DBHelper
        {
             //以下被注的是连接配置文件用,使用时可先编写配置文件,把Name设置为ConnectionString
             private static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
             //private static string connectionString = "server=WYP;database=MXDB1;uid=sa;pwd=sa";
             private static SqlConnection connection;
             public static SqlConnection Connection
             {
                 get
                 {
                    if (connection == null)
                    {
                       connection = new SqlConnection(connectionString);
                       connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Closed)
                         {
                            connection.Open();
                         }
                    else if (connection.State == System.Data.ConnectionState.Broken)
                         {
                             connection.Close();
                             connection.Open();
                         }
                    return connection;
                 }
             }
         public static int ExecuteCommand(string safeSql)
         {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            int result = cmd.ExecuteNonQuery();
            return result;
         }
         public static int ExecuteCommand(string sql, SqlParameter[] values)
         {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteNonQuery();
         }
         public static int ExecuteCommand(string sql, SqlParameter value)
         {
             SqlCommand cmd = new SqlCommand(sql, Connection);
             cmd.Parameters.Add(value);
             int result = cmd.ExecuteNonQuery();
             return result;
          }
          public static int ExecuteScalar(string safeSql)
          {
             SqlCommand cmd = new SqlCommand(safeSql, Connection);
             int result = (int)cmd.ExecuteScalar();
             return result;
          } 
           public static int ExecuteScalar(string sql, SqlParameter[] values)
           {
              SqlCommand cmd = new SqlCommand(sql, Connection);
              cmd.Parameters.AddRange(values);
              int result = (int)cmd.ExecuteScalar();
              return result;
           } 
           public static int ExecuteScalar(string sql, SqlParameter value)
           {
              SqlCommand cmd = new SqlCommand(sql, Connection);
              cmd.Parameters.Add(value);
              int result = (int)cmd.ExecuteScalar();
              return result;
           }
           public static SqlDataReader ExecuteReader(string safeSql)
           {
              SqlCommand cmd = new SqlCommand(safeSql, Connection);
              SqlDataReader reader = cmd.ExecuteReader();
              return reader;
           } 
           public static SqlDataReader ExecuteReader(string sql, SqlParameter value)
           {
              SqlCommand cmd = new SqlCommand(sql, Connection);
              cmd.Parameters.Add(value);
              SqlDataReader reader = cmd.ExecuteReader();
              return reader;
           }
           public static SqlDataReader ExecuteReader(string sql, SqlParameter[] values)
           {
              SqlCommand cmd = new SqlCommand(sql, Connection);
              cmd.Parameters.AddRange(values);
              SqlDataReader reader = cmd.ExecuteReader();
              return reader;
           }
           public static DataTable GetDataSet(string safeSql)
            {
               DataSet ds = new DataSet();
               SqlCommand cmd = new SqlCommand(safeSql, Connection);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(ds,"temp");
               return ds.Tables[0];
            }
            public static DataTable GetDataSet(string safeSql,SqlParameter[] values)
            {
               DataSet ds = new DataSet();
               SqlCommand cmd = new SqlCommand(safeSql, Connection);
               cmd.Parameters.AddRange(values);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(ds,"temp");
               return ds.Tables[0];
            }
            public static SqlDataReader GetReader(string safeSql)
            {
               SqlCommand cmd = new SqlCommand(safeSql, Connection);
               SqlDataReader reader = cmd.ExecuteReader();
               return reader;
            }
            public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
            {
               SqlCommand cmd = new SqlCommand(sql, Connection);
               cmd.Parameters.AddRange(values);
               SqlDataReader reader = cmd.ExecuteReader();
                return reader;
            }
        } 
     }
  • 相关阅读:
    ios ASI 断点下载
    ios 视频播放器MPMoviePlayerController
    ios SDWebImage 图片加载显示菊花
    ios简单的音频播放
    学iOS开发(一)——实现一款App之Foundation框架的使用
    学iOS开发(二)——实现一款App之编写自定义类
    蓝欧ios培训视频
    ios开发教程地址
    day15_雷神_前端03
    day14_雷神_前端02
  • 原文地址:https://www.cnblogs.com/hesheng/p/3945231.html
Copyright © 2020-2023  润新知