C# DBHelper类
class DBHelper
{
//public static string connstr = "server=.;database=SuperKTV;uid=sa;pwd=123456"; public static string connstr = "server=.;database=Dormitory;uid=sa;pwd=123456";
//数据库链接对象
private static SqlConnection Conn = null; //初始化数据库链接
private static void InitConnection()
{
//如果连接对象不存在,创建连接
if (Conn == null)
Conn = new SqlConnection(connstr);
//如果连接对象关闭,打开连接
if (Conn.State == ConnectionState.Closed)
Conn.Open();
//如果连接中断,重启连接
if (Conn.State == ConnectionState.Broken)
{
Conn.Close();
Conn.Open();
}
} //查询,获取DataReader
public static SqlDataReader GetDataReader(string sqlStr)
{
InitConnection();
SqlCommand cmd = new SqlCommand(sqlStr, Conn);
//CommandBehavior.CloseConnection 命令行为:当DataReader对象被关闭时,自动关闭占用的连接对象
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
} //查询,获取DataTable
public static DataTable GetDataTable(string sqlStr)
{
InitConnection();
DataTable table = new DataTable();
SqlDataAdapter dap = new SqlDataAdapter(sqlStr, Conn);
dap.Fill(table);
Conn.Close();
return table;
} //查询,获取DataSet
public static DataSet GetDataSet(string sqlStr)
{
InitConnection();
DataSet ds = new DataSet();
SqlDataAdapter dap = new SqlDataAdapter(sqlStr, Conn);
dap.Fill(ds);
Conn.Close();
return ds;
} //增改删
public static bool ExecuteNonQuery(string sqlStr)
{
InitConnection();
SqlCommand cmd = new SqlCommand(sqlStr, Conn);
int result = cmd.ExecuteNonQuery();
Conn.Close();
return result > 0;
} //执行集合函数
public static object ExecuteScalar(string sqlStr)
{
InitConnection();
SqlCommand cmd = new SqlCommand(sqlStr, Conn);
object result = cmd.ExecuteScalar();
Conn.Close();
return result;
}
}
C# DBHelper类的更多相关文章
- DBHelper 类(网上收集)
这个是我网上找的,觉得不错的一个DBHelper类,下面是作者话: // 微软的企业库中有一个非常不错的数据操作类了.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什 ...
- Accesss数据库的DBhelper类(带分页)
首先配置web.config,使配置文件连接access数据库: <connectionStrings> <add name="DBConnection" con ...
- C# -- 等待异步操作执行完成的方式 C# -- 使用委托 delegate 执行异步操作 JavaScript -- 原型:prototype的使用 DBHelper类连接数据库 MVC View中获取action、controller、area名称、参数
C# -- 等待异步操作执行完成的方式 C# -- 等待异步操作执行完成的方式 1. 等待异步操作的完成,代码实现: class Program { static void Main(string[] ...
- c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)
c#封装DBHelper类 public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...
- [C#]记录一次异常排查,关于using语法、sqlserver数据库session、DBHelper类
最近在做一个基于asp.net和sqlserver的网站项目,发现网站运行一段时间之后,会报异常: 超时时间已到,但是尚未从池中获取连接.出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小 ...
- 分享一个简单的C#的通用DbHelper类(支持数据连接池)
每次新项目的时候,都要从头去找一遍数据库工具类.这里分享一个简单实用的C#的通用DbHelper工具类,支持数据连接池. 连接池配置 <connectionStrings> <add ...
- Ado.net中简单的DBHelper类(增删改查)
private static string connString = "server=.;database=hotel;uid=aa;pwd=123";//最好从配置文件中取出 p ...
- 关于SqlServer的DBHelper类以及数据分页
前端: <My:AspNetPager class="arPage" PageSize="20" UrlPaging="true" r ...
- 收集C#常用类:自己写的一个DBHelper类
随着学的东西越来越多,一点点的完善吧! using System; using System.Collections.Generic; using System.Linq; using System. ...
- 关于MySql的DBHelper类以及数据分页
前端: <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix=& ...
随机推荐
- leetcode 【 Sort Colors 】python 实现
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- [oldboy-django][2深入django]django模板使用函数
1 模板引入子html--include 模板引擎 - 母版 - include,导入公共的html a. 用法:{% include "pub.html" %}, pub.htm ...
- [转]手写数字识别错误NameError: name 'mnist' is not defined
转自:https://blog.csdn.net/coder_Gray/article/details/78562382 在Tensorflow上进行mnist数字识别实例时,出现如下错误 NameE ...
- xctf --Hctf2014 Quals write up
描述 猫流大大发现一个女神,你能告诉我女神的名字么(名字即是flag) nvshen.zip Solution: Extract the file and we could find a txt wh ...
- Codeforces Round #362 (Div. 2) B 模拟
B. Barnicle time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- java运行时间计算
long startTime = System.currentTimeMillis(); //获取开始时间 doSomething(); //测试的代码段 long endTime = System. ...
- Python之时间:datetime模块
datetime在time基础之上封装了一些方法.但是time是经常使用的,datetime中的功能,time都能实现 一.datetime的三个模块 datetime.date datetime.t ...
- rand()与 srand()
一 头文件 <time.h> srand()就是给rand()提供种子seed. 在C语言中,srand()与rand()是随机函数,其中srand函数是伪随机数发生器的初始化函数,原 ...
- Java EE 学习(2):使用 IDEA 开发 最简java web
参考:http://www.cnblogs.com/carsonzhu/p/5468223.html 使用IntelliJ IDEA开发java web 前言:由于本人以前简略的学习过 java 语言 ...
- CodeChef Counting on a directed graph
Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. ...