SQLHELPER 帮助类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 数据库的读取
{
using System.Data;
using System.Data.SqlClient;
class SQLHelper
{
static string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
/// <summary>
/// 执行查询结果返回第一列值第一列的值
/// </summary>
public static object ExecuteScalar(string sql ,params SqlParameter[] sp)
{
using (SqlConnection conn =new SqlConnection(sqlconn))
{
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.AddRange(sp);
return comm.ExecuteScalar();
}
}
/// <summary>
/// 返回表 默认
/// </summary>
/// <param name="sql"></param>
/// <param name="sp"></param>
/// <returns></returns>
public static DataTable GetTable(string sql ,SqlParameter[] sp)
{
using(SqlConnection conn =new SqlConnection(sqlconn))
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.SelectCommand.Parameters.AddRange(sp);
DataTable table = new DataTable();
da.Fill(table);
return table;
}
}
/// <summary>
/// 返回表
/// </summary>
/// <param name="sql"></param>
/// <param name="sp"></param>
/// <returns></returns>
public static DataTable GetTable(string sql, CommandType type ,SqlParameter[] sp)
{
//using (SqlConnection conn = new SqlConnection(sqlconn)) //可以自动释放conn 用了适配器
//{
// conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql,new SqlConnection(sqlconn));
da.SelectCommand.CommandType = type;
da.SelectCommand.Parameters.AddRange(sp);
DataTable table = new DataTable();
da.Fill(table);
return table;
//}
}
/// <summary>
/// 创建读取器
/// </summary>
/// <returns></returns>
public static SqlDataReader GetSqlDataRead(string sql ,SqlParameter[] sp)
{
//创建读取器不能关闭
SqlConnection conn = new SqlConnection(sqlconn);
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.AddRange(sp);
return comm.ExecuteReader(CommandBehavior.CloseConnection);//当关闭读取器关闭相对的连接
}
//执行sql语句
public static int ExecuteNonQuery(string sql, SqlParameter[] sp)
{
using (SqlConnection conn =new SqlConnection(sqlconn))
{
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.AddRange(sp);
return comm.ExecuteNonQuery();
}
}
}
}
SQLHELPER 帮助类的更多相关文章
- 微软版的SqlHelper.cs类
一,微软SQLHelper.cs类 中文版: using System; using System.Data; using System.Xml; using System.Data.SqlClien ...
- 微软SQLHelper.cs类 中文版
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Co ...
- 微软SQLHelper.cs类
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
- SQLHelper帮助类_下(支持多数据库的封装)
在上篇关于SQLHelper类中,主要针对SQLServer数据库进行的.在使用别的数据库,就要修改部分代码!所以今天就写一个支持多数据库的封装!主要用到枚举,读取config文件!接口的简单用法.获 ...
- 处女篇:自用C#后端SqlHelper.cs类
自用SqlHelper.cs类,此类来自软谋教育徐老师课程SqlHelper.cs! using System; using System.Collections; using System.Coll ...
- C#版SQLHelper.cs类
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
- 万能的SQLHelper帮助类
/// <summary> /// 数据库帮助类 /// </summary> public class SQLHelper { private static string c ...
- 微软C#版SQLHelper.cs类
转载自:http://blog.csdn.net/fengqingtao2008/article/details/17399247 using System; using System.Data; u ...
- SQLHelper.cs类 微软C#版
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
- SqlHelper帮助类
数据库连接字符串//Data Source=.;Initial Catalog=Test1;User Id=sa;Password=123456; public static class SqlHel ...
随机推荐
- UVa 10718 - Bit Mask
题目大意:给一数N,在区间[L, U]上找到一个数M使得M| N的值最大,如果有M有多个可能值,取最小的那个值. 从最高位开始逐位判断,如果N的该位为0,为使M | N的值最大,M的该位应考虑置为1, ...
- iOS 准备
iOS 编程知识点 iOS 基础知识点 iOS 之 系统机制 Xcode 使用技巧 Mac 使用技巧 iOS 之 英语 iOS 之 编外知识点 iOS 知识库链接
- javascript设计模式之解释器模式详解
http://www.jb51.net/article/50680.htm 神马是“解释器模式”? 先翻开<GOF>看看Definition:给定一个语言,定义它的文法的一种表示,并定义一 ...
- .NET DLL 保护措施详解(非混淆加密加壳)核心思路的实现
最近有很多朋友通过BLOG找到我询问我的相关细节,其实相关的实现细节我早已把源码上传到51aspx上面了,地址是http://www.51aspx.com/code/codename/56949 也有 ...
- 如何测试LBS功能
在LBS功能的开发中,为了保证通用性,服务器存在的坐标是基于wgs84的,这个通常由GPS设备传过来,对于PC来说,如何获得这个值呢?可以利用Google Earth来获得,并修改显示的坐标系统,“工 ...
- java服务器获取客户端ip
在写服务端代码时,有时需要对客户端ip做认证,比如限制只有某些ip能访问,或者1个ip1天只能访问几次.最近就碰到个需要限制ip的情况,从网上找了一些服务器获取客户端ip的方法,说的都不太完善,这里整 ...
- HTML5行业现状与未来 - 2016年终大盘点
* { margin: 0; padding: 0 } .con { width: 802px; margin: 0 auto; text-align: center; position: inher ...
- Codeforces758B
B. Blown Garland time limit per test:1 second memory limit per test:256 megabytes input:standard inp ...
- Hadoop权威指南:HDFS-数据流
Hadoop权威指南:HDFS-数据流 [TOC] 剖析文件读取 客户端通过调用FileSystem对象的open()方法来打开希望读取的文件,对于HDFS来说, 这个对象是分布式文件系统的一个实例 ...
- HDU 1006 [Tick Tick]时钟问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1006 题目大意:钟表有时.分.秒3根指针.当任意两根指针间夹角大于等于n°时,就说他们是happy的, ...