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 ...
随机推荐
- border-radius归纳
一.基本语法 1.1 语法 解释 border-radius:10px 将创建四个大小一样的圆角. border-radius:10px 15px 10px 5px; 四个值分别表示左上角.右上角.右 ...
- [git] 细说commit (git add/commit/diff/rm/reset 以及 index 的概念)
http://kasicass.blog.163.com/blog/static/39561920133294219374/ 创建测试仓库 $ git init $ echo "line o ...
- 如何解决Ajax跨域问题-1
如何解决Ajax跨域问题 最近在做AJAX调用C的问题,出现跨域问题,学习总结如下: 在做ajax读取数据的时候,经常会遇到ajax需要跨域的问题,但由于浏览器安全方面的限制,XMLHttpReque ...
- osgearth earth文件规范-符号参考
osgearth earth文件规范-符号参考 osgEarth用样式表渲染要素和注记. 本文档列出了可在样式表中使用的所有符号属性.不是每个符号是适用于每种情况:这只是一个主列表. 跳转到符号: • ...
- error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches
error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches &quo ...
- Jenkins中deploy插件的deploy war/ear to a container与deploy artifacts to maven reepository区别
deploy war/ear to a container:发布war包到服务器 deploy artifacts to maven reepository:发布到maven服务器
- mysql数据库锁定机制
前言 为了保证数据的一致完整性,任何一个数据库都存在锁定机制.锁定机制的优劣直接应想到一个数据库系统的并发处理能力和性能,所以锁定机制的实现也就 成为了各种数据库的核心技术之一.本章将对MySQL中两 ...
- Angular - - 脏值检查及其相关
今天突然就想写写$digest和$apply,这些都是脏值检查的主体内容. 先以普通js来做一个简单的监控例子吧: var div = ducoment.getElementById("my ...
- HDU-1233-还是畅通工程(并查集)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1233题目很简单(最小生成树) #include<cstdio> #include<io ...
- swift中标签的使用
1,标签的创建 1 2 3 4 5 6 7 8 9 10 import UIKit class ViewController: UIViewController { override func ...