zoukankan      html  css  js  c++  java
  • ADO.NET工具类(一)

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;
    using DC.BE.Entity;
    
    namespace DC.BE.BusinessImpl
    {
        public static class DBHelper
        {
            private static SqlConnection connection;
            public static SqlConnection Connection
            {
                get
                {
                    string connectionString = ConnectionHelper.ConnectionString;
                    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 void BulkInsert(DataTable dt,string tableName)
            {
                using (SqlBulkCopy bulk = new SqlBulkCopy(connection))
                {
                    bulk.BatchSize = 20000;
                    bulk.BulkCopyTimeout = 500000000;
                    bulk.DestinationTableName = tableName;
    
                    foreach (DataColumn dc in dt.Columns)
                    {
                        bulk.ColumnMappings.Add(dc.ColumnName, dc.ColumnName);
                    }
    
                    bulk.WriteToServer(dt);
                }
            }
    
            public static int ExecuteCommand(string safeSql)
            {
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                cmd.CommandTimeout = 2000;
                int result = cmd.ExecuteNonQuery();
                return result;
            }
    
            public static int ExecuteCommand(string sql, params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }
    
            public static int GetScalar(string safeSql)
            {
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result;
            }
    
            public static int GetScalar(params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = Connection;
                cmd.CommandText = "Pro_InsertOrder";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(values);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result;
            }
    
            public static int GetScalar(string sql, params SqlParameter[] values)
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result;
            }
    
            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;
            }
    
            public static DataTable GetDataSet(string safeSql)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
    
            public static DataTable GetDataSet(string sql, params SqlParameter[] values)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }
    }
    萌橙 你瞅啥?
  • 相关阅读:
    Sublime Text 3 支持的热门插件推荐
    Sublime text 2/3 中 Package Control 的安装与使用方法
    UML类图中箭头和线条的含义和用法
    this guy gonna be a daddy
    PHP设计模式之:单例模式
    PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )
    php memcache 基础操作
    获取IP地址方法
    短信发送
    使用Shell脚本对Linux系统和进程资源进行监控
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/8242675.html
Copyright © 2011-2022 走看看