C# 封装SqlHelper
在项目配置文件中添加数据库连接字符串
<connectionStrings>
<add connectionString="Data Source=主机;Initial Catalog=数据库名;Persist Security Info=True;User ID=用户名;Password=密码" name="当前连接字符串名称"/>
</connectionStrings>
封装类
class SqlHelper
{
//获取连接字符串
private readonly static string connectionStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
/// <summary>
/// 执行sql 增 删 该
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public static bool ExecuteNonQuery(string sql, params SqlParameter[] param)
{
int result;
using (SqlConnection con = new SqlConnection(connectionStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (param != null)
{
cmd.Parameters.AddRange(param);
}
try
{
con.Open();
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
//如果连接失败,抛出异常,并手动释放资源
con.Close();
con.Dispose();
throw ex;
}
}
con.Close();
}
if (result == )
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 查询单行单列 或者是统计
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public static object ExecuteScalar(string sql, params SqlParameter[] param)
{
using (SqlConnection con = new SqlConnection(connectionStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (param != null)
{
cmd.Parameters.AddRange(param);
}
try
{
con.Open();
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
//如果连接失败,抛出异常,并手动释放资源
con.Close();
con.Dispose();
throw ex;
} }
}
}
/// <summary>
/// 根据条件查询数据
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns>SqlDataReader对象</returns>
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] param)
{
using (SqlConnection con = new SqlConnection(connectionStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (param != null)
{
cmd.Parameters.AddRange(param);
}
try
{
con.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
//如果连接失败,抛出异常,并手动释放资源
con.Close();
con.Dispose();
throw ex;
}
}
}
} /// <summary>
/// 根据条件查询数据
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns>DataTable对象</returns>
public static DataTable ExcuteTable(string sql, params SqlParameter[] param)
{
using (SqlConnection con = new SqlConnection(connectionStr))
{
DataTable dt = new DataTable(); using (SqlDataAdapter sda = new SqlDataAdapter(sql, connectionStr))
{
if (param != null) {
sda.SelectCommand.Parameters.AddRange(param);
}
sda.Fill(dt);
} return dt;
} }
}
C# 封装SqlHelper的更多相关文章
- 封装sqlhelper【一】
控件信息展示: //定义调用数据库类文件 namespace SqlHelper { public class TblClass { public int classId { get; set; } ...
- 数据操作的封装--sqlhelper
为了提高软件的灵活性和可维护性,软件的代码须要科学的管理.我们引入了架构这个词.设计模式提醒我们,软件中反复性的代码须要封装起来. 近期在做收费系统时.须要和数据库进行频繁的联系.既然是反复的使用,就 ...
- 封装SqlHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- 封装sqlhelper类
using System;using System.Collections.Generic;using System.Data;using System.Data.Common;using Syste ...
- ADO.Net和SqlHelper封装
1.什么是ADO.Net 简单来讲,ADO.NET是用于和数据源打交道的.Net结束,是一组向.NET程序员公开数据访问服务的类 2.ADO.NET的组成部分和对象模型 (1)ADO.NET的两个 ...
- ado.net的简单数据库操作(二)之封装SqlHelperl类
今天我书接上回,接着昨天的ado.net的数据库操作的相关知识来讲哈! 从上篇文章给出的实例来看,你一定会发现,操作数据库其实还挺麻烦的,就连一个最简单的数据库操作语句都要包括 定义数据库连接字符串. ...
- 【整理】待毕业.Net码农就业求职储备
声明:本文题目来源于互联网,仅供即将从学校毕业的.Net码农(当然,我本人也是菜逼一个)学习之用.当然,学习了这些题目不一定会拿到offer,但是针对就业求职做些针对性的准备也是不错的.此外,除了技术 ...
- C#向Sql数据库插入控制
string name = textBox1.Text; int age = Convert.ToInt32(textBox2.Text.Trim()); ? null : (int?)Convert ...
- .net学习总结
.NET 学前入门 了解.Net能做什么 了解.NET,C#语言及其特点(分清.NET和C#的关系),对.Net学习有系统全面的认识. C#基础 变量,赋值运算符.数据类型转换等. 选择结构控制(if ...
随机推荐
- 18-10-09 Linux常用命令大全(非常全!!!)
Linux常用命令大全(非常全!!!) Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制, ...
- linux do{} while(0)
do{}while(0) 在linux中,经常会看到do{}while(0)这样的语句,许多人开始都会疑惑,认为do{}while(0)毫无意义,因为它只会执行一次,加不加do{}while(0)效果 ...
- Python全栈之路----数据类型—字典
字典:可变,一种key-value的数据类型 info = { 'stu1101' : 'TengLan Wu' , 'stu1102' : 'LongZe Luola' , 'stu1103' : ...
- Parsing Natural Scenes and Natural Language with Recursive Neural Networks-paper
Parsing Natural Scenes and Natural Language with Recursive Neural Networks作者信息: Richard Socher richa ...
- 同时开始了SQL。。。
SQL LIMIT OFFSET 和 LIMIT code1: SELECT id, name, score FROM table ORDER BY score DESC LIMIT OFFSET 4 ...
- [cf1038E][欧拉路]
http://codeforces.com/contest/1038/problem/E E. Maximum Matching time limit per test 2 seconds memor ...
- Tarjan求割点&桥
概念 1.桥:是存在于无向图中的这样的一条边,如果去掉这一条边,那么整张无向图会分为两部分,这样的一条边称为桥无向连通图中,如果删除某边后,图变成不连通,则称该边为桥. 2.割点:无向连通图中,如果删 ...
- CodeForce 517 Div 2. B Curiosity Has No Limits
http://codeforces.com/contest/1072/problem/B B. Curiosity Has No Limits time limit per test 1 second ...
- [R] t.test()
t.test(x, y = NULL, alternative = c("two.sided", "less","greater"), mu ...
- 让docker容器开机启动
网上有些文章说,要让docker 的容器自动在开机启动,是写脚本,比如在 rc.local 中写.其实完全没必要这么麻烦,docker 有相关指令,docker run 指令中加入 --restart ...