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 ...
随机推荐
- sqlserver获取当前id的前一条数据和后一条数据
一.条件字段为数值的情况 select * from tb where id=@id; --当前记录 select top 1 * from tb where id>@id order ...
- 查看 SELinux状态及关闭SELinux
查看SELinux状态: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: ...
- NSURLSessionDataTask
#import "ViewController.h" @interface ViewController ()<NSURLSessionDelegate,NSURLSessi ...
- 解决CentOS 5.8在虚拟机环境下如何桥接上网
1.虚拟机的网卡配置如下图所示: 2.在CentOS 5.8的命令行界面:输入如下指令 然后准备修改里面的网关地址和自己的IP地址 3.同时查看自己的IP地址和网关 4.在第二步里面修改,网关地址应该 ...
- java中setDate(Date date)方法和String与Date之间的转换
经常在开发的过程中遇到这样的问题,从数据库中读出来的数据需要转换为对像或者java bean,此时经常使用到setDate(Date date);这样的方法.感觉这是个很简单而又难受的事情,在这里浪费 ...
- Ant构建与部署Java项目---入门
原文地址:http://tech.it168.com/j/2007-11-09/200711091344781.shtml Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建 ...
- (摘)DataGuard物理standby管理 - 主备切换
DataGuard物理standby管理 - 主备切换 Dataguard的切换分为两种,switchover和failover. switchover一般用于数据库或硬件升级,这时只需要较短时间中断 ...
- write a macro to judge big endian or little endian
Big endian means the most significant byte stores first in memory. int a=0x01020304, if the cpu is b ...
- ResourceString的用法
在Delphi编程的那段“古老”的日子里(就是在版本4之前),在程序中使用字符串有两个基本的方法.你可以使用字符串将它们嵌入到源程序中,例如: MessageDlg( 'Leave your stin ...
- Powershell创建数组
在Powershell中创建数组可以使用逗号. PS C:Powershell> $nums=2,0,1,2 PS C:Powershell> $nums 2 0 1 2 对于连续的数字数 ...