SqlDbHelper备份,做项目时方便应用(目前不太全,把自己项目中的逐渐转移过来)
******************************************
这是官网新闻左侧类别那部分用到的
****************************************
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备份,做项目时方便应用(目前不太全,把自己项目中的逐渐转移过来)的更多相关文章
- IDEA问题之“微服务启动项目时,不会加载Spring Boot到Services中”
1.启动项目时,不会加载Spring Boot到Services中 现象解析: 启动项目时 会在debug的位置加载项目 注:这里没有配图,因为问题已解决,未记录图,需往后遇到记录 解决方案: 需要在 ...
- 用dataset做数据源时,让gridview显示的列名与数据库表中的字段名不同
原文发布时间为:2008-10-27 -- 来源于本人的百度文章 [由搬家工具导入] 确定GridView的AutoGenerateColumns设置为False;使用GridView的“编辑列”,添 ...
- eclipse创建项目时出现appcompat_v7包及解决办法
Android开发学习总结(三)--appcompat_v7项目说明 一.appcompat_v7项目说明 今天来说一下appcompat_v7项目的问题,使用eclipse创建Android项目时, ...
- 在部署 C#项目时转换 App.config 配置文件
问题 部署项目时,常常需要根据不同的环境使用不同的配置文件.例如,在部署网站时可能希望禁用调试选项,并更改连接字符串以使其指向不同的数据库.在创建 Web 项目时,Visual Studio 自动生成 ...
- idea 为模块添加Tomcat依赖 解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包
解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包 今天创建SpringMVC项目时 用到HttpServletRequest时, 发现项目中根本没有Servlet这个包, ...
- vue-cli 启动项目时空白页面
vue-cli 启动项目时空白页面 在启动项目时 npm run serve / npm run dev 启动 vue 项目空白页:且终端及控制台都未报错 通过各种查阅发现在项目根目录中 vue-co ...
- 2022最新版超详细的Maven下载配置教程、IDEA中集成maven(包含图解过程)、以及导入项目时jar包下载不成功的问题解决
文章目录 1.maven下载 2.maven环境变量的配置 3.查看maven是否配置成功 4.配置文件的修改 5.IDEA集成maven 6.导入项目时jar包下载不成功的问题解决 maven教程: ...
- 增量更新项目时的备份MyBak
在增量更新项目时,做好备份十分重要,这里提供一个方法备份java Web所更新的文件. 把更新包放在指定目录,配好如下webappFolder.updateFolder以及bakeupFolder的路 ...
- 做web项目时对代码改动后浏览器端不生效的应对方法(持续更新)
做web项目时,常常会遇到改动了代码,但浏览器端没有生效,原因是多种多样的,我会依据我遇到的情况逐步更新解决的方法 1.执行的时候採用debug模式,普通情况下使用项目部署button右边那个butt ...
随机推荐
- Objective-C 异常处理
#import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { ...
- Objective-C Delegate
ios设计模式中的委托 Delegate 官方文档解释如下: Delegation is a simple and powerful pattern in which one object in a ...
- n!(n的阶乘)
我们在这里介绍一些关于n!的性质. 在计数问题中,经常需要用到n!.有必要了解n!在mod p下的一些性质.下面我们假设p是素数,n!=ape(a无法被p整除),并试图求解e和a mod p(把这个东 ...
- 使用“bulk insert ”进行批量插入数据
本文转自csdn中文章,再次感谢他给我们分享. Bulk Insert命令详细 BULK INSERT以用户指定的格式复制一个数据文件至数据库表或视图中.语法: BULK INSERT [ [ 'da ...
- DEDE数据库修改后台变量
进行数据库之后找到 dede_sysconfig 这个数据表,然后查找到你要删除的dede教程变量名称. 这样就可以了
- ie7 不兼容overflow:hidden;
用overflow:hidden; 隐藏不需要显示的数据,在IE6\IE8都显示正常,但是在ie7中就是不起作用,万恶的IE7啊.后来加了一句position:relative; 好了... stat ...
- Decimal:解决0.3 != 0.1+0.1+0.1的方法(十进制浮点数)
0.普通情况 1.引入Decimal 也可以这样引入: from decimal import Decimal as D 相当于: from decimal import Decimal D = De ...
- uC/OS-II中的时间 (转)
时间是一个非常重要的概念,我们和朋友出去游玩需要约定一个时间,做事情也需要花费一段时间,总之,我们的生活离不开时间.操作系统也一样,也需要一个时间来规范其任务的执行. 我们生活中,时间的最小单位是秒, ...
- 请转到http://zhuangyongyao.com
个人博客搬迁到http://zhuangyongyao.com.
- injector
angular 提供了一套依赖注入的机制,和后台很像.虽然我不觉得有很重要. var $injector = angular.injector(["myModule"]); var ...