ADO.NET通用数据库访问类
作者:TuringChang 时间:2024-01-28 03:26:19
本文实例为大家分享了ADO.NET通用数据库访问类,供大家参考学习,具体内容如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace Test
{
public class DBHelper
{
public static string ConString = "Data Source=.;Initial Catalog=bankdb;User id=sa;Password=123;";
//执行增删改的方法
public static int RunNoQuery(string cmdText, CommandType cmdType, params SqlParameter[] pars)
{
SqlConnection con = new SqlConnection(ConString);
con.Open();
SqlCommand cmd = new SqlCommand(cmdText, con);
cmd.CommandType = cmdType;
if (pars != null && pars.Length > 0)
{
foreach (SqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
int rows = cmd.ExecuteNonQuery();
con.Close();
return rows;
}
//执行查询(DataSet)的方法
public static DataSet RunSelect(string cmdText, CommandType cmdType, params SqlParameter[] pars)
{
SqlConnection con = new SqlConnection(ConString);
SqlDataAdapter da = new SqlDataAdapter(cmdText, con);
da.SelectCommand.CommandType = cmdType;
if (pars != null && pars.Length > 0)
{
foreach (SqlParameter p in pars)
{
da.SelectCommand.Parameters.Add(p);
}
}
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
//执行查询得到一个值
public static object RunOneValue(string cmdText, CommandType cmdType, params SqlParameter[] pars)
{
SqlConnection con = new SqlConnection(ConString);
con.Open();
SqlCommand cmd = new SqlCommand(cmdText, con);
cmd.CommandType = cmdType;
if (pars != null && pars.Length > 0)
{
foreach (SqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
object obj = cmd.ExecuteScalar();
con.Close();
return obj;
}
}
}
标签:ADO.NET,数据库访问类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用开源Cesium+Vue实现倾斜摄影三维展示功能
2024-05-28 15:51:51
![](https://img.aspxhome.com/file/2023/8/123158_0s.jpg)
Python实现url长短链接的转换
2022-07-21 14:05:58
![](https://img.aspxhome.com/file/2023/1/115851_0s.jpg)
Django框架下在URLconf中指定视图缓存的方法
2023-10-03 01:54:28
python通过elixir包操作mysql数据库实例代码
2024-01-26 04:12:16
![](https://img.aspxhome.com/file/2023/2/92322_0s.png)
二级域名的解析指向ASP源码
2007-10-19 19:13:00
php微信开发之批量生成带参数的二维码
2023-11-23 19:27:11
![](https://img.aspxhome.com/file/2023/8/113608_0s.jpg)
Python 爬虫多线程详解及实例代码
2021-01-25 14:05:03
python相对包导入报“Attempted relative import in non-package”错误问题解决
2022-02-28 12:57:05
PythonPC客户端自动化实现原理(pywinauto)
2022-11-27 02:24:27
![](https://img.aspxhome.com/file/2023/3/95673_0s.png)
PyCharm关闭碍眼的波浪线图文详解
2023-07-27 08:47:53
![](https://img.aspxhome.com/file/2023/7/118027_0s.png)
python实现自动下载sftp文件
2022-01-25 12:55:36
![](https://img.aspxhome.com/file/2023/7/111967_0s.jpg)
使用post方法实现json往返传输数据的方法
2024-04-18 09:28:23
![](https://img.aspxhome.com/file/2023/5/136615_0s.png)
加密处理使密码更安全[CFS编码加密]
2008-03-19 13:30:00
Python列表推导式,元组推导式,字典推导式,集合推导式
2022-08-01 23:26:36
python实现简单flappy bird
2022-08-03 12:06:33
解决Python传递中文参数的问题
2021-04-10 09:00:07
Python图像处理之直线和曲线的拟合与绘制【curve_fit()应用】
2021-01-28 10:30:49
![](https://img.aspxhome.com/file/2023/3/67353_0s.png)
超详细注释之OpenCV更改像素与修改图像通道
2021-07-01 17:42:45
![](https://img.aspxhome.com/file/2023/8/134608_0s.jpg)
pycharm debug 断点调试心得分享
2022-11-12 05:10:06
![](https://img.aspxhome.com/file/2023/2/120582_0s.png)
Git 教程之基本操作详解
2023-08-04 08:04:20