DBHelper
DBHelper:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data; namespace DAL
{
public class DBhelper
{
private static string Constr = "server=.;database=PaiPaiDB;uid=sa;pwd=123";
private static SqlConnection conn;
private static SqlDataAdapter da;
private static SqlCommand cmd;
private static DBhelper dbhelper; public DBhelper()
{
conn = new SqlConnection(Constr);
} public static DBhelper Instance()
{
if (dbhelper == null)
{
dbhelper = new DBhelper();
}
return dbhelper;
} void DBOpen()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
} void DBClose()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
} public DataTable GetDtatTableBySql(string sql)
{
DBOpen(); DataTable dt = new DataTable();
da = new SqlDataAdapter(sql, conn);
try
{
da.Fill(dt);
return dt;
}
catch (Exception)
{ return null;
}
finally
{
DBClose();
}
} public bool ExcuteSql(string sql)
{ DBOpen();
cmd = new SqlCommand(sql, conn);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch (Exception)
{
return false;
}
finally
{
DBClose();
}
} public bool ExcuteProcedure(string proName, SqlParameter[] paras)
{
DBOpen();
cmd = new SqlCommand(proName, conn);
cmd.CommandType = CommandType.StoredProcedure;
for (int i = ; i < paras.Length; i++)
{
cmd.Parameters.Add(paras[i]);
}
try
{
cmd.ExecuteNonQuery();
return true;
}
catch (Exception)
{
return false;
}
finally
{
DBClose();
}
}
}
}
调用方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data; namespace DAL
{
public class GoodsService
{
//查询
public static DataTable GetDtatTableBySql(string sql)
{ DataTable dt = DBhelper.Instance().GetDtatTableBySql(sql);
if (dt != null && dt.Rows.Count > )
{
return dt;
}
else
{
return null;
}
}
//增加 删除 修改
public static bool ExecuteSql(string sql)
{
bool a;
a = DBhelper.Instance().ExcuteSql(sql);
return a;
} }
}
DBHelper的更多相关文章
- 不该活着的SqlHelper和DBHelper
前言: 还记得刚学ADO.NET的情景么? 还记得当年是怎么从ADO.NET被忽悠到用SqlHelper的么? 话说从入门到走上工作岗位那些年,我们就一直被纯纯地教导或引导,ADO.NET太原始,得封 ...
- 兼容SQLSERVER、Oracle、MYSQL、SQLITE的超级DBHelper
本示例代码的关键是利用.net库自带的DbProviderFactory来生产数据库操作对象. 从下图中,可以看到其的多个核心方法,这些方法将在我们的超级DBHelper中使用. 仔细研究,你会发现每 ...
- C#/ASP.NET完善的DBHelper,配套Model生成器
支持Oracle.MSSQL.MySQL.SQLite四种数据库,支持事务,支持对象关系映射:已在多个项目中实际使用. 没有语法糖,学习成本几乎为0,拿来即用. DBHelper类完整代码: usin ...
- C# DBHelper 第二版
1. [代码][C#]代码 跳至 [1] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- 我也来写:数据库访问类DBHelper
一.前言 相信许多人都百度过:“.net 数据库访问类”.然后就出来一大堆SqlHelper.我也用过这些SqlHelper,也自己写过,一堆静态方法,开始使用起来感觉很不错,它们也确实在很多时候可以 ...
- DbHelper为什么要用Using?
我们分析一下DbHelper做什么事情,大家都知道它用于数据库的连接操作,这里的数据库连接会创建非托管资源,c#的垃圾回收机制不会对它处理,需要实现IDisposable接口手动释放. 手动释放的 ...
- NHibernate3快速上手教程FluentNHibernate配置与DBHelper
很多学习NHibernate的新手很容易卡在配置文件这一关,正所谓万事开头难,上手后再配合官方文档就比较容易了. 网上关于配置文件的资料非常多,但由于版本的问题,许多老的教程中都没有明确指出类库的版本 ...
- 数据库助手类 DBHelper
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...
- 收集C#常用类:自己写的一个DBHelper类
随着学的东西越来越多,一点点的完善吧! using System; using System.Collections.Generic; using System.Linq; using System. ...
- Ado.net中简单的DBHelper类(增删改查)
private static string connString = "server=.;database=hotel;uid=aa;pwd=123";//最好从配置文件中取出 p ...
随机推荐
- src与href属性的区别
src和href之间存在区别,能混淆使用.src用于替换当前元素,href用于在当前文档和引用资源之间确立联系. src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在 ...
- 【摘】top命令
1.重要参数解释 VIRT:virtual memory usage.Virtual这个词很神,一般解释是:virtual adj.虚的, 实质的, [物]有效的, 事实上的.到底是虚的还是实的?让G ...
- 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...
- C#EXCEL 操作类--C#ExcelHelper操作类
主要功能如下1.导出Excel文件,自动返回可下载的文件流 2.导出Excel文件,转换为可读模式3.导出Excel文件,并自定义文件名4.将数据导出至Excel文件5.将指定的集合数据导出至Exce ...
- 简单实用的Windows命令(二)
昨天简单的记录了几个非常简单实用的Windows命令,不过我又想起来还有两个我在实际的工作中也是经常用到的命令——PING和IPCONFIG,不过我在工作中的使用都是非常简单的,用PING命令检测对应 ...
- 配置VS使用winteracter
一.winteracter是什么?为什么要用VS代替wide?winteracter 是一款强大的 Fortran 图形界面函数库,可方便的用 Fortran 生成GUI界面,对话框,菜单,绘图等操作 ...
- java, poi, excel
工作需要用java操作Excel,现在网上搜索了一下,决定选取POI包来操作.pom内容如下: <dependency> <groupId>org.apache.poi< ...
- numpy.concatenate
import numpy as np a = np.array([[1, 2], [3, 4]]) a.shape Out[3]: (2, 2) b = np.array([[5, 6]]) b.sh ...
- 8.8 CSS知识点1
什么是CSS CSS(Cascading Style Sheet) 层叠样式表 CSS3在CSS2的基础上增加了很多强大的新功能,目前主流浏览器都支持CSS3大部分功能.为了更好的向前兼容,不同的浏览 ...
- WinExec
WinAPI: WinExec - 运行外部程序 //声明 WinExec( lpCmdLine: LPCSTR; {文件名和参数; 如没指定路径会按以下顺序查找: 程序目录/当前目录/Syste ...