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();

        }

    }

}

时间: 2024-10-12 22:48:58

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

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

原文发布时间为:2008-10-27 -- 来源于本人的百度文章 [由搬家工具导入] 确定GridView的AutoGenerateColumns设置为False;使用GridView的“编辑列”,添加一个绑定字段:“BoundField”,在该绑定字段的BindField的属性中的数据中的DataField中添加你要绑定的数据库中表的列的名称,然后用你的代码没问题了,或者是在“外观”中的“HeadText”中输入要显示的名称也可以实现

SharePoint 2010 中新建项目时显示的“新”“New”字样探讨

SharePoint 2010 中新建项目时显示的"新""New"字样探讨 在SharePoint 2010 中,当我们新建项目时,会发现新项目右上角出现"新"字样.见图: 但是请思考一下,为什么下面第二个项目不一样呢?说明"新"字样出现的时间是有限制的,如果超出了某个时间,会自动消失.你知道"新"字样会出现多久吗?一天?两天? 默认情况下,"新"字样持续2天,由days-to-show

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

做web项目时,常常会遇到改动了代码,但浏览器端没有生效,原因是多种多样的,我会依据我遇到的情况逐步更新解决的方法 1.执行的时候採用debug模式,普通情况下使用项目部署button右边那个button下的tomcat7中的run即可,假设使用的是serves中的run serves,这样的情况貌似不会自己主动编译 2.点击project菜单下的clean选项,在打开的窗体中选择你使用的项目,ok,这样会删除tomcat容器中关于该项目的一些信息,然后又一次部署,执行 3.删除电脑中tomca

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

做web项目时,经常会遇到修改了代码,但浏览器端没有生效,原因是多种多样的,我会根据我遇到的情况逐步更新解决办法 1.运行的时候采用debug模式,一般情况下使用项目部署按钮右边那个按钮下的tomcat7中的run就行,如果使用的是serves中的run serves,这种情况貌似不会自动编译 2.点击project菜单下的clean选项,在打开的窗口中选择你使用的项目,ok,这样会删除tomcat容器中关于该项目的一些信息,然后重新部署,运行 3.删除电脑中tomcat文件夹,重新解压,然后在

idea 新建 maven 项目 (避免 idea 在使用 maven 新建 webapp 骨架项目时一直 downloading 的问题)

1. 新建 module,左侧选择 Maven,右侧勾选 Create from archetype,中间找到 maven-archetype-webapp 2. 填写 GroupId and ArtifactId GroupId: the unique identifier of the organization or group that created the project ArtifactId: unique base name of the primary artifact bein

eclipse新建Android项目时提示AndroidManifest.xml file missing!

最近做一个小的项目时发现刚新建了一个项目失败了,控制台就输出AndroidManifest.xml file missing! 是么R文件都没有.原来是项目名称不能带中文,用英文的就OK了.

刚做完几个简单的响应式设计的网站项目下来,杂七杂八 (一)

之前没接触过responsive design这玩意,突然最近客户的项目都要求要有响应式设计的要求: 1,当浏览器缩放时,页面要根据浏览器大小,而自动适应. 2,当用手机或者移动设备打开页面时,页面会根据屏幕浏览器的大小自动适应. 3,移动设备有横屏和竖屏之分,页面也要相应适应 首页设计 UI设计师只设计出了2种mockup,一种是full site一种是mobile site然后扔过来,一看那mockup就知道是当前比较流行的设计风格. 1,页头head,左logo右菜单:在full site

关于Eclipse创建Android项目时,会多出一个appcompat_v7的问题

问题描述: 使用eclipse创建一个Android项目时,发现project列表中会多创建出一个appcompat_v7项目,再创建一个Android项目时,又会再多出一个appcompat_v7_2,如果再次创建,会以此类推地创建出appcompat_v7_x格式的“多余项目”出来(此情况在ADT升级为22.6.x版本后出现,22.3.x前的版本不会有) 查明原因: ADT在22.3.x版本前没有出现该情况,升级为22.6.x版本后,才出现该情况,可以猜测是新版本导致.猜测到原因后可以分析下

Nginx 做代理服务器时浏览器加载大文件失败 ERR_CONTENT_LENGTH_MISMATCH 的解决方案

此文章仅作为本人的笔记,文章转载自  http://blog.csdn.net/defonds/article/details/46042809 Nginx 做反向代理,后端是 tomcat,chrome 浏览器访问项目时加载大文件失败 ERR_CONTENT_LENGTH_MISMATCH: 查看 linux 日志:2015/05/27 02:19:10 [crit] 29263#0: *72 open() "/usr/local/nginx/proxy_temp/3/00/000000000