******************************************

这是官网新闻左侧类别那部分用到的

****************************************

public string ConnectionString = ConfigurationManager.ConnectionStrings["GsWebDbEntities"].ConnectionString;
public myDBHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}

public DataSet ExecuteDataset(CommandType commandType, string commandText, params DbParameter[] commandParameters)
{

if ((this.ConnectionString == null) || (this.ConnectionString.Length == 0))
{
throw new ArgumentNullException("ConnectionString");
}
using (DbConnection connection = new SqlConnection(ConnectionString))
{
connection.ConnectionString = this.ConnectionString;
connection.Open();
return this.ExecuteDataset(connection, commandType, commandText, commandParameters);
}
}

public DataSet ExecuteDataset(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
{
if (connection == null)
{
throw new ArgumentNullException("connection");
}
DbCommand command = new SqlCommand();

this.PrepareCommand(command, connection, null, commandType, commandText, commandParameters);
using (DbDataAdapter adapter = new SqlDataAdapter())
{
adapter.SelectCommand = command;
DataSet dataSet = new DataSet();
DateTime now = DateTime.Now;
adapter.Fill(dataSet);
DateTime dtEnd = DateTime.Now;

command.Parameters.Clear();
if (connection.State==ConnectionState.Open)
{
connection.Close();
}

return dataSet;
}
}

private void PrepareCommand(DbCommand command, DbConnection connection, DbTransaction transaction, CommandType commandType, string commandText, DbParameter[] commandParameters)
{
if (command == null)
{
throw new ArgumentNullException("command");
}
if ((commandText == null) || (commandText.Length == 0))
{
throw new ArgumentNullException("commandText");
}
if (connection.State != ConnectionState.Open)
{

connection.Open();
}

command.Connection = connection;
command.CommandText = commandText;
if (transaction != null)
{
if (transaction.Connection == null)
{
throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
}
command.Transaction = transaction;
}
command.CommandType = commandType;
if (commandParameters != null)
{

foreach (DbParameter parameter in commandParameters)
{
if (parameter != null)
{
if (((parameter.Direction == ParameterDirection.InputOutput) || (parameter.Direction == ParameterDirection.Input)) && (parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
command.Parameters.Add(parameter);
}
}

}
}

*****************************************************************************以下来自网络,以上来自自己项目中的整理

using System;

using System.Data;
using System.Data.SqlClient;
 
namespace DbHelper
{
    public class SqlDbHelper
    {
        private SqlConnection conn;
        private SqlCommand cmd;
        private SqlDataReader reader;
        private SqlDataAdapter adapter;
        private string connectionString = @"server=.;database=student;uid=sa;pwd=scce";
 
        public string ConnectionString
        {
            get { return this.connectionString; }
            set { this.connectionString = value; }
        }
 
        /// <summary>
        /// 获取一个未打开连接的SqlConnection对象
        /// </summary>
        /// <returns>SqlConnection对象</returns>
        public SqlConnection GetConnection()
        {
            if (conn != null)
                return this.conn;
            return this.conn = new SqlConnection(connectionString);
        }
 
        /// <summary>
        /// 使用连接字符串获取未打开连接SqlConnection对象
        /// </summary>
        /// <param name="_connStr">连接字符串</param>
        /// <returns>SqlConnection对象</returns>
        public SqlConnection GetConnection(string _connStr)
        {
            if (this.conn != null)
                this.conn.ConnectionString = _connStr;
            else
                this.conn = new SqlConnection(_connStr);
            return this.conn;
        }
 
        /// <summary>
        /// 使用指定的Sql语句创建SqlCommand对象
        /// </summary>
        /// <param name="sqlStr">Sql语句</param>
        /// <returns>SqlCommand对象</returns>
        private SqlCommand GetCommand(string sqlStr)
        {
            if (this.conn == null)
                this.conn = GetConnection();
            if (this.cmd == null)
                this.cmd = this.GetCommand(sqlStr, CommandType.Text, null);
            else
            {
                this.cmd.CommandType = CommandType.Text;
                this.cmd.Parameters.Clear();
            }
            this.cmd.CommandText = sqlStr;
            return this.cmd;
        }
 
        /// <summary>
        /// 使用指定的Sql语句,CommandType,SqlParameter数组创建SqlCommand对象
        /// </summary>
        /// <param name="sqlStr">Sql语句</param>
        /// <param name="type">命令类型</param>
        /// <param name="paras">SqlParameter数组</param>
        /// <returns>SqlCommand对象</returns>
        public SqlCommand GetCommand(string sqlStr, CommandType type, SqlParameter[] paras)
        {
            if (conn == null)
                this.conn = this.GetConnection();
            if (cmd == null)
                this.cmd = conn.CreateCommand();
            this.cmd.CommandType = type;
            this.cmd.CommandText = sqlStr;
            this.cmd.Parameters.Clear();
            if (paras != null)
                this.cmd.Parameters.AddRange(paras);
            return this.cmd;
        }
 
        /// <summary>
        /// 执行Sql语句返回受影响的行数
        /// </summary>
        /// <param name="sqlStr">Sql语句</param>
        /// <returns>受影响的行数,失败则返回-1</returns>
        public int ExecuteNoQuery(string sqlStr)
        {
            int line = -1;
            CheckArgs(sqlStr);
            try { OpenConn(); line = this.ExecuteNonQuery(sqlStr,CommandType.Text,null); }
            catch (SqlException e) { throw e; }
            return line;
        }
 
        /// <summary>
        /// 使用指定的Sql语句,CommandType,SqlParameter数组执行Sql语句,返回受影响的行数
        /// </summary>
        /// <param name="sqlStr">Sql语句</param>
        /// <param name="type">命令类型</param>
        /// <param name="paras">SqlParameter数组</param>
        /// <returns>受影响的行数</returns>
        public int ExecuteNonQuery(string sqlStr, CommandType type, SqlParameter[] paras)
        {
            int line = -1;
            CheckArgs(sqlStr);
            if (this.cmd == null)
                GetCommand(sqlStr, type, paras);
            this.cmd.Parameters.Clear();
            this.cmd.CommandText = sqlStr;
            this.cmd.CommandType = type;
            if(paras != null)
                this.cmd.Parameters.AddRange(paras);
            try { OpenConn(); line = this.cmd.ExecuteNonQuery(); }
            catch (SqlException e) { throw e; }
            return line;
        }
 
        /// <summary>
        /// 使用指定Sql语句获取dataTable
        /// </summary>
        /// <param name="sqlStr">Sql语句</param>
        /// <returns>DataTable对象</returns>
        public DataTable GetDataTable(string sqlStr)
        {
            CheckArgs(sqlStr);
            if (this.conn == null)
                this.conn = GetConnection();
            this.adapter = new SqlDataAdapter(sqlStr, this.conn);
            DataTable table = new DataTable();
            try { adapter.Fill(table); }
            catch (SqlException e) { throw e; }
            return table;
        }
 
        /// <summary>
        /// 使用指定的Sql语句获取SqlDataReader
        /// </summary>
        /// <param name="sqlStr">sql语句</param>
        /// <returns>SqlDataReader对象</returns>
        public SqlDataReader GetSqlDataReader(string sqlStr)
        {
            CheckArgs(sqlStr);
            if (cmd == null)
                GetCommand(sqlStr);
            if(reader != null)
                reader.Dispose();
            try { OpenConn(); this.reader = this.cmd.ExecuteReader(); }
            catch (SqlException e) { throw e; }
            return this.reader;
        }
 
        /// <summary>
        /// 使用事务执行多条Sql语句
        /// </summary>
        /// <param name="sqlCommands">sql语句数组</param>
        /// <returns>全部成功则返回true否则返回false</returns>
        public bool ExecuteSqls(string[] sqlCommands)
        {
            if (sqlCommands == null)
                throw new ArgumentNullException();
            if(this.cmd == null)
                GetCommand(null);
            SqlTransaction tran = null;
            try
            {
                OpenConn();
                tran = this.conn.BeginTransaction();
                this.cmd.Transaction = tran;
                foreach (string sql in sqlCommands)
                {
                    if (ExecuteNoQuery(sql) == 0)
                    { tran.Rollback(); return false; }
                }
            }
            catch (SqlException e)
            {
                if(tran != null)
                    tran.Rollback();
                throw e;
            }
            tran.Commit();
            return true;
        }
 
        private void OpenConn()
        {
            try
            {
                if (this.conn.State == ConnectionState.Closed)
                    conn.Open();
            }
            catch (SqlException e) { throw e; }
        }
 
        private void CheckArgs(string sqlStr)
        {
            if (sqlStr == null)
                throw new ArgumentNullException();
            if (sqlStr.Length == 0)
                throw new ArgumentOutOfRangeException();
        }
 
    }
}

SqlDbHelper备份,做项目时方便应用(目前不太全,把自己项目中的逐渐转移过来)的更多相关文章

  1. IDEA问题之“微服务启动项目时,不会加载Spring Boot到Services中”

    1.启动项目时,不会加载Spring Boot到Services中 现象解析: 启动项目时 会在debug的位置加载项目 注:这里没有配图,因为问题已解决,未记录图,需往后遇到记录 解决方案: 需要在 ...

  2. 用dataset做数据源时,让gridview显示的列名与数据库表中的字段名不同

    原文发布时间为:2008-10-27 -- 来源于本人的百度文章 [由搬家工具导入] 确定GridView的AutoGenerateColumns设置为False;使用GridView的“编辑列”,添 ...

  3. eclipse创建项目时出现appcompat_v7包及解决办法

    Android开发学习总结(三)--appcompat_v7项目说明 一.appcompat_v7项目说明 今天来说一下appcompat_v7项目的问题,使用eclipse创建Android项目时, ...

  4. 在部署 C#项目时转换 App.config 配置文件

    问题 部署项目时,常常需要根据不同的环境使用不同的配置文件.例如,在部署网站时可能希望禁用调试选项,并更改连接字符串以使其指向不同的数据库.在创建 Web 项目时,Visual Studio 自动生成 ...

  5. idea 为模块添加Tomcat依赖 解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包

    解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包 今天创建SpringMVC项目时 用到HttpServletRequest时, 发现项目中根本没有Servlet这个包, ...

  6. vue-cli 启动项目时空白页面

    vue-cli 启动项目时空白页面 在启动项目时 npm run serve / npm run dev 启动 vue 项目空白页:且终端及控制台都未报错 通过各种查阅发现在项目根目录中 vue-co ...

  7. 2022最新版超详细的Maven下载配置教程、IDEA中集成maven(包含图解过程)、以及导入项目时jar包下载不成功的问题解决

    文章目录 1.maven下载 2.maven环境变量的配置 3.查看maven是否配置成功 4.配置文件的修改 5.IDEA集成maven 6.导入项目时jar包下载不成功的问题解决 maven教程: ...

  8. 增量更新项目时的备份MyBak

    在增量更新项目时,做好备份十分重要,这里提供一个方法备份java Web所更新的文件. 把更新包放在指定目录,配好如下webappFolder.updateFolder以及bakeupFolder的路 ...

  9. 做web项目时对代码改动后浏览器端不生效的应对方法(持续更新)

    做web项目时,常常会遇到改动了代码,但浏览器端没有生效,原因是多种多样的,我会依据我遇到的情况逐步更新解决的方法 1.执行的时候採用debug模式,普通情况下使用项目部署button右边那个butt ...

随机推荐

  1. ASP.net导出Excel的几种方式

    2.导出多个sheet页的Excel 在Office Excel 中设计好 导出的格式,然后另存为xml电子表格,然后用记事本打开保存的xml文件,复制内容放入程序Response.Write() 输 ...

  2. c3p0写连接池 Demo

    1.导包 2.配置文件:名称必须为:c3p0-config.xml,且必须放在src根目录下 <c3p0-config> <!-- 默认配置,有且仅可出现一次 ,如果没有指定则使用这 ...

  3. js分页算法

    function get_hs_page(cur_page, total_page) { var result = ""; for(var i = 1; i <= total ...

  4. JQuery控制input的readonly和disabled属性

    jquery设置元素的readonly和disabled Jquery的api中提供了对元素应用disabled和readonly属性的方法,在这里记录下.如下: 1.readonly   $('in ...

  5. dedecms 织梦ping服务插件 最新破解可用版

    dedecms 织梦ping服务插件 最新破解可用版  ping_gbk.xml <module> <baseinfo> name=ping服务 team=井哥 time=20 ...

  6. python中的super

    super用于类的继承.用super()代替父类名 (一)通过类名调用父类中的方法                                                         (二 ...

  7. Android学习笔记--Menu菜单的使用

    实现选项菜单.上下文菜单,以及菜单内部的子菜单. 视图效果: MainActivity 选项菜单 选项菜单的子菜单 上下文菜单(按住按钮或者EditText弹出) 注意:上下文菜单如何弹出?在注册该菜 ...

  8. Java学习笔记--NIO

    参考资料:http://ifeve.com/buffers/ BIO/NIO/AIO的区别联系 http://stevex.blog.51cto.com/4300375/1284437http://w ...

  9. iOS 7 二维码

    维码扫描 2014-06-13 10:20:29|  分类: iOS|举报|字号 订阅     下载LOFTER客户端     // //  TCTosweepScan.m //  TongCheng ...

  10. AFNetworking使用总结

    AFNetworking使用总结   关于AFNetworking使用总结 以及一些错误的解决办法. AD:WOT2015 互联网运维与开发者大会 热销抢票 AFNetworking使用总结 分享类型 ...