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. c# 静态方法和数据

    c#所有方法都必须在类的内部声明,但如果把方法或者字段声明为static就可以使用,类名代用方法或者访问字段. 在方法中声明一个静态变量a 和一个静态的aFun方法.下面是在主函数中调用. 从上图可以 ...

  2. git getting started

    2019/4/25-- after committing to blessed. modify dependency file to download file so as to get latest ...

  3. 给PHP开启shmop扩展实现共享内存

    在项目开发中,想要实现PHP多个进程之间共享数据的功能,让客户端连接能够共享一个状态,需要开启共享内存函数shmop.如果预期考虑会遇到这方面需求,那么最好在编译PHP的时候添加--with-shmo ...

  4. Hashtable与HashMap的区别

    HashMap不是线程安全的,HashTable是线程安全. HashMap允许空(null)的键和值(key),HashTable则不允许. HashMap性能优于Hashtable.

  5. PyCharm 安装及破解方法

    PyCharm 安装及破解方法 一.安装 首先要下载Pycharm这个软件,可以在官网下载进行下载. 下载完软件之后,双击安装文件即可进行安装,双击后出现下图所示的提示,点击Next即可. 然后会出现 ...

  6. python之块包导入

    一.模块 1.什么是模块 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写 ...

  7. 初识 GitHub

    初识 GitHub 一.注册账号 GitHub 官网:https://github.com/ 点击右上角sign up,进行注册,注册界面如下: 填写用户名,邮箱地址,密码,下滑点击绿色按钮:Crea ...

  8. win7安装mysql-8.0.13-winx64

    这里展示一下,由于需要安装一个版本测试一下数据,其实就是超简单的啦. 下包 注:https://dev.mysql.com/downloads/mysql/ 解压与配置 [mysqld] basedi ...

  9. rpm command

    rpm 实现程序管理 安装:-ivh ,--nodeps ,--replacepkgs 卸载: -e,  --nodeps 升级: -Uvh -Fvh , --nodeps,  --oldpackag ...

  10. IDEA安装与破解

    今天下午偶然在知乎上看到IDEA和eclipse的软件分析,所以装了一个IDEA,不过肯定是破解,不会购买激活码 IDEA官网:http://www.jetbrains.com/idea/ 安装教程: ...