using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration; namespace ImportExcel
{
public class SqlDbHelper
{
/// <summary>
/// 连接字符串
/// </summary>
public static readonly string connectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString; #region ExecuteNonQuery命令
/// <summary>
/// 对数据库执行增、删、改命令
/// </summary>
/// <param name="safeSql">T-Sql语句</param>
/// <returns>受影响的记录数</returns>
public static int ExecuteNonQuery(string safeSql)
{
using (SqlConnection Connection = new SqlConnection(connectionString))
{
Connection.Open();
SqlTransaction trans = Connection.BeginTransaction();
try
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
cmd.Transaction = trans; if (Connection.State != ConnectionState.Open)
{
Connection.Open();
}
int result = cmd.ExecuteNonQuery();
trans.Commit();
return result;
}
catch
{
trans.Rollback();
return ;
}
}
} /// <summary>
/// 对数据库执行增、删、改命令
/// </summary>
/// <param name="sql">T-Sql语句</param>
/// <param name="values">参数数组</param>
/// <returns>受影响的记录数</returns>
public static int ExecuteNonQuery(string sql, SqlParameter[] values)
{
using (SqlConnection Connection = new SqlConnection(connectionString))
{
Connection.Open();
SqlTransaction trans = Connection.BeginTransaction();
try
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Transaction = trans;
cmd.Parameters.AddRange(values);
if (Connection.State != ConnectionState.Open)
{
Connection.Open();
}
int result = cmd.ExecuteNonQuery();
trans.Commit();
return result;
}
catch (Exception ex)
{
trans.Rollback();
return ;
}
}
}
#endregion #region ExecuteScalar命令
/// <summary>
/// 查询结果集中第一行第一列的值
/// </summary>
/// <param name="safeSql">T-Sql语句</param>
/// <returns>第一行第一列的值</returns>
public static int ExecuteScalar(string safeSql)
{
using (SqlConnection Connection = new SqlConnection(connectionString))
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
} /// <summary>
/// 查询结果集中第一行第一列的值
/// </summary>
/// <param name="sql">T-Sql语句</param>
/// <param name="values">参数数组</param>
/// <returns>第一行第一列的值</returns>
public static int ExecuteScalar(string sql, SqlParameter[] values)
{
using (SqlConnection Connection = new SqlConnection(connectionString))
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
}
#endregion #region ExecuteReader命令
/// <summary>
/// 创建数据读取器
/// </summary>
/// <param name="safeSql">T-Sql语句</param>
/// <param name="Connection">数据库连接</param>
/// <returns>数据读取器对象</returns>
public static SqlDataReader ExecuteReader(string safeSql, SqlConnection Connection)
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
} /// <summary>
/// 创建数据读取器
/// </summary>
/// <param name="sql">T-Sql语句</param>
/// <param name="values">参数数组</param>
/// <param name="Connection">数据库连接</param>
/// <returns>数据读取器</returns>
public static SqlDataReader ExecuteReader(string sql, SqlParameter[] values, SqlConnection Connection)
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
#endregion #region ExecuteDataTable命令
/// <summary>
/// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataTable
/// </summary>
/// <param name="type">命令类型(T-Sql语句或者存储过程)</param>
/// <param name="safeSql">T-Sql语句或者存储过程的名称</param>
/// <param name="values">参数数组</param>
/// <returns>结果集DataTable</returns>
public static DataTable ExecuteDataTable(CommandType type, string safeSql, params SqlParameter[] values)
{
using (SqlConnection Connection = new SqlConnection(connectionString))
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
cmd.CommandType = type;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[];
}
} /// <summary>
/// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataTable
/// </summary>
/// <param name="safeSql">T-Sql语句</param>
/// <returns>结果集DataTable</returns>
public static DataTable ExecuteDataTable(string safeSql)
{
using (SqlConnection Connection = new SqlConnection(connectionString))
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
da.Fill(ds);
}
catch (Exception ex)
{ }
return ds.Tables[];
}
} /// <summary>
/// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataTable
/// </summary>
/// <param name="sql">T-Sql语句</param>
/// <param name="values">参数数组</param>
/// <returns>结果集DataTable</returns>
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] values)
{
using (SqlConnection Connection = new SqlConnection(connectionString))
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.CommandTimeout = ;
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[];
}
}
#endregion #region GetDataSet命令
/// <summary>
/// 取出数据
/// </summary>
/// <param name="safeSql">sql语句</param>
/// <param name="tabName">DataTable别名</param>
/// <param name="values"></param>
/// <returns></returns>
public static DataSet GetDataSet(string safeSql, string tabName, params SqlParameter[] values)
{
using (SqlConnection Connection = new SqlConnection(connectionString))
{
if (Connection.State != ConnectionState.Open)
Connection.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection); if (values != null)
cmd.Parameters.AddRange(values); SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
da.Fill(ds, tabName);
}
catch (Exception ex)
{ }
return ds;
}
}
#endregion #region ExecureData 命令
/// <summary>
/// 批量修改数据
/// </summary>
/// <param name="ds">修改过的DataSet</param>
/// <param name="strTblName">表名</param>
/// <returns></returns>
public static int ExecureData(DataSet ds, string strTblName)
{
try
{
//创建一个数据库连接
using (SqlConnection Connection = new SqlConnection(connectionString))
{
if (Connection.State != ConnectionState.Open)
Connection.Open(); //创建一个用于填充DataSet的对象
SqlCommand myCommand = new SqlCommand("SELECT * FROM " + strTblName, Connection);
SqlDataAdapter myAdapter = new SqlDataAdapter();
//获取SQL语句,用于在数据库中选择记录
myAdapter.SelectCommand = myCommand; //自动生成单表命令,用于将对DataSet所做的更改与数据库更改相对应
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter); return myAdapter.Update(ds, strTblName); //更新ds数据
} }
catch (Exception err)
{
throw err;
}
} #endregion
}
}

原文:https://www.cnblogs.com/felix-wang/p/6742785.html

ADO.NET DBHelper的更多相关文章

  1. ADO.NET DBHelper 类库

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  2. Ado.net中简单的DBHelper类(增删改查)

    private static string connString = "server=.;database=hotel;uid=aa;pwd=123";//最好从配置文件中取出 p ...

  3. ADO.NET帮助类DBHelper

    一. DBHelper帮助类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  4. ADO.net 增删改查封装DBhelper

    using System; using System.Collections.Generic; using System.Data.SqlClient;//引用数据库客户端 using System. ...

  5. ADO.NET连接数据库DBHelper工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. ADO.NET sqlHelper类(DBHelper类)

    1.配置文件 <connectionStrings> <add name="constr" connectionString="Data Source= ...

  7. 不该活着的SqlHelper和DBHelper

    前言: 还记得刚学ADO.NET的情景么? 还记得当年是怎么从ADO.NET被忽悠到用SqlHelper的么? 话说从入门到走上工作岗位那些年,我们就一直被纯纯地教导或引导,ADO.NET太原始,得封 ...

  8. 我也来写:数据库访问类DBHelper

    一.前言 相信许多人都百度过:“.net 数据库访问类”.然后就出来一大堆SqlHelper.我也用过这些SqlHelper,也自己写过,一堆静态方法,开始使用起来感觉很不错,它们也确实在很多时候可以 ...

  9. ADO.NET基础--组成部分

    第一部分:数据提供程序,在命令空间:System.Data.SqlClient下: Connection--连接数据库的通道 Command----命令执行对象 DataReader--数据读取器 D ...

随机推荐

  1. react系列笔记1 用npx npm命令创建react app

    react系列笔记1 用npx npm命令创建react app create-react-app my-app是开始构建新的 React 单页应用程序的最佳方式.它已经为你设置好了开发环境,以便您可 ...

  2. jQuery实现无刷新切换主题皮肤功能

    主题皮肤切换功能在很多网站和系统中应用,用户可以根据此功能设置自己喜欢的主题颜色风格,增强了用户体验.本文将围绕如何使用jQuery实现点击无刷新切换主题皮肤功能. 查看演示DEMO:https:// ...

  3. vue路由5:命名视图

    <div id="app"> <div> <router-link to="/">首页</router-link> ...

  4. Requests+BeautifulSoup+正则表达式爬取猫眼电影Top100(名称,演员,评分,封面,上映时间,简介)

    # encoding:utf-8 from requests.exceptions import RequestException import requests import re import j ...

  5. PHP官方文档和phpstorm配置指南

    http://cn2.php.net/manual/zh/ phpstorm安装——>next——>…… 下载PHP.exe 地址:http://www.php.net/ 配置interp ...

  6. QQ项目(续)

    1.项目查找好友的原理 sql:select * from qquser where account in(select friendAccount from friend where userAcc ...

  7. EDK II之USB设备驱动程序的加载与运行

    本文简单介绍一下USB设备的驱动程序是如何匹配设备以及被加载的: 上文(UDK中USB总线驱动的实现框架)提到USB总线枚举设备的最后一步是调用gBS->ConnectController()去 ...

  8. uwsgi 的巨坑

    网上各种找,最后自己猜,猜到了. 必须安装python插件, 网上找的都是不带数字的版本号, 要么找不到要么不行. 我是 3.6.1,尝试加36, 成了. yum install -y uwsgi-p ...

  9. ELK学习笔记之F5利用ELK进行应用数据挖掘系列(1)-HTTP

    0x00 概述 F5 BIGIP从应用角度位于网络结构的关键咽喉位置,可获取所有应用的流量,针对流量执行L7层处理,即便是TLS加密的流量也可以通过F5进行SSL offload.通过F5可以统一获取 ...

  10. centos6安装多实例mysql

    centos 6.5,使用二进制安装多实例mysql 5.5.60 所需安装包mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz.ncurses-devel-5.7- ...