【C#】SQL数据库助手类2.0(自用)
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Text; 7 8 namespace YcTools 9 { 10 /// <summary>C# SQL数据库助手类2.0</summary> 11 public class YSqlHelper 12 { 13 //Sql连接语句 14 /*注意引用System.Configuration 15 * <connectionStrings> 16 * <add name="YcSqlCon" 17 * connectionString="Data Source=服务器;Initial Catalog=数据库;User ID=登录名;Password=密码" 18 * providerName="System.Data.SqlClient"/> 19 * </connectionStrings> 20 */ 21 // private string connectionString = ConfigurationManager.ConnectionStrings["YcSqlCon"].ConnectionString; 22 private string connectionString = ""; 23 public YSqlHelper(string sqlPath) 24 { 25 //实例化对应的数据库链接 26 connectionString = ConfigurationManager.ConnectionStrings[sqlPath].ConnectionString; 27 } 28 29 /// <summary>执行不带参数的增删改SQL语句或存储过程 ,返回受影响的行数</summary> 30 public int ExecuteNonQuery(string cmdText) 31 { 32 int res = 0;//受影响的行数 33 using (SqlConnection conn = new SqlConnection(connectionString)) 34 { 35 try 36 { 37 conn.Open();//打开数据库链接 38 using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 39 { 40 cmd.CommandType = CommandType.Text; 41 res = cmd.ExecuteNonQuery();//执行Sql语句并受影响的行数 42 } 43 } 44 catch 45 { 46 47 } 48 finally 49 { 50 if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态 51 { 52 conn.Close();//关闭与数据库的链接 53 } 54 } 55 } 56 return res; 57 } 58 59 /// <summary> 执行带参数的增删改SQL语句或存储过程,返回受影响的行数</summary> 60 public int ExecuteNonQuery(string cmdText, SqlParameter[] paras) 61 { 62 int res = 0;//受影响的行数 63 using (SqlConnection conn = new SqlConnection(connectionString)) 64 { 65 try 66 { 67 conn.Open();//打开数据库链接 68 using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 69 { 70 cmd.CommandType = CommandType.Text; 71 cmd.Parameters.AddRange(paras); 72 res = cmd.ExecuteNonQuery();//执行Sql语句并受影响的行数 73 } 74 } 75 catch 76 { 77 78 } 79 finally 80 { 81 if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态 82 { 83 conn.Close();//关闭与数据库的链接 84 } 85 } 86 } 87 return res; 88 } 89 90 /// <summary> 执行不带参数的查询SQL语句或存储过程,返回DataTable对象</summary> 91 public DataTable ExecuteQueryDataTable(string cmdText) 92 { 93 DataTable dt = new DataTable(); 94 using (SqlConnection conn = new SqlConnection(connectionString)) 95 { 96 try 97 { 98 conn.Open();//打开数据库链接 99 using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 100 { 101 cmd.CommandType = CommandType.Text; 102 using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) 103 { 104 dt.Load(sdr); 105 } 106 } 107 } 108 catch 109 { 110 111 } 112 finally 113 { 114 if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态 115 { 116 conn.Close();//关闭与数据库的链接 117 } 118 } 119 } 120 return dt; 121 } 122 123 /// <summary> 执行带参数的查询SQL语句或存储过程,返回DataTable对象</summary> 124 public DataTable ExecuteQueryDataTable(string cmdText, SqlParameter[] paras) 125 { 126 DataTable dt = new DataTable(); 127 using (SqlConnection conn = new SqlConnection(connectionString)) 128 { 129 try 130 { 131 conn.Open();//打开数据库链接 132 using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 133 { 134 cmd.CommandType = CommandType.Text; 135 cmd.Parameters.AddRange(paras); 136 using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) 137 { 138 dt.Load(sdr); 139 } 140 } 141 } 142 catch 143 { 144 145 } 146 finally 147 { 148 if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态 149 { 150 conn.Close();//关闭与数据库的链接 151 } 152 } 153 } 154 return dt; 155 } 156 157 /// <summary> 执行不带参数的查询SQL语句或存储过程,返回DataSet对象</summary> 158 public DataSet ExecuteQueryDataSet(string cmdText) 159 { 160 DataSet ds = new DataSet(); 161 using (SqlConnection conn = new SqlConnection(connectionString)) 162 { 163 try 164 { 165 conn.Open();//打开数据库链接 166 using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 167 { 168 cmd.CommandType = CommandType.Text; 169 using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 170 { 171 da.Fill(ds, "ds"); 172 } 173 } 174 } 175 catch 176 { 177 178 } 179 finally 180 { 181 if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态 182 { 183 conn.Close();//关闭与数据库的链接 184 } 185 } 186 } 187 return ds; 188 } 189 190 /// <summary> 执行带参数的查询SQL语句或存储过程,返回DataSet对象</summary> 191 public DataSet ExecuteQueryDataSet(string cmdText, SqlParameter[] paras) 192 { 193 DataSet ds = new DataSet(); 194 using (SqlConnection conn = new SqlConnection(connectionString)) 195 { 196 try 197 { 198 conn.Open();//打开数据库链接 199 using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 200 { 201 cmd.CommandType = CommandType.Text; 202 cmd.Parameters.AddRange(paras); 203 using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 204 { 205 da.Fill(ds, "ds"); 206 } 207 } 208 } 209 catch 210 { 211 212 } 213 finally 214 { 215 if (conn.State == ConnectionState.Open)//判断连接是否处于打开状态 216 { 217 conn.Close();//关闭与数据库的链接 218 } 219 } 220 } 221 return ds; 222 } 223 224 /// <summary>查询数据是否存在</summary> 225 public bool ExecuteDataIsExistByData(string sqlStr) 226 { 227 bool iss = false; 228 DataSet ds = ExecuteQueryDataSet(sqlStr); 229 for (int i = 0; i < ds.Tables.Count; i++) 230 { 231 if (ds.Tables[i].Rows.Count > 0) iss = true; 232 } 233 return iss; 234 } 235 236 /// <summary>查询数据是否存在 </summary> 237 public bool ExecuteDataIsExistByData(string sqlStr, SqlParameter[] paras) 238 { 239 bool iss = false; 240 DataSet ds = ExecuteQueryDataSet(sqlStr, paras); 241 for (int i = 0; i < ds.Tables.Count; i++) 242 { 243 if (ds.Tables[i].Rows.Count > 0) iss = true; 244 } 245 return iss; 246 } 247 248 /// <summary>查询增删改数据操作是否成功 </summary> 249 public bool ExecuteDataIsExistByInt(string sqlStr) 250 { 251 int ds = ExecuteNonQuery(sqlStr); 252 bool iss = ds > 0 ? true : false; 253 return iss; 254 } 255 256 /// <summary>查询增删改数据操作是否成功 </summary> 257 public bool ExecuteDataIsExistByInt(string sqlStr, SqlParameter[] paras) 258 { 259 int ds = ExecuteNonQuery(sqlStr, paras); 260 bool iss = ds > 0 ? true : false; 261 return iss; 262 } 263 } 264 }
【C#】SQL数据库助手类2.0(自用)相关推荐
- mysql db模块下载_易语言MySql数据库操作类V1.0模块源码
易语言MySql数据库操作类V1.0模块源码 易语言MySql数据库操作类V1.0模块源码 系统结构:MySql数据库连接类_测试子程序,置连接选项,连接,关闭,是否已连接,取连接句柄,置连接句柄,执 ...
- 使用sqlmetal工具自动生成SQL数据库的Linq类文件
第一部:找到sqlmetal.exe. 运行cmd. 执行命令 cd C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5 ...
- SQL数据库修复教程-浪潮GS7.0中了后缀是.5ss5c勒索病毒修复过程
SQL数据库修复教程-浪潮GS7.0中了后缀是.5ss5c勒索病毒修复过程 今天接到一个浪潮GS7.0的用户的求救电话,中了后缀是.5ss5c的勒索病毒,服务器里的所有文件被加密,重要的SQL数据库文 ...
- Access数据库访问助手类
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.D ...
- oracle 调用java 类_oracle数据库PL SQL调用Java类程序代码
作为JAVA程序员,对ORACLE的javasource一直是想找个机会下手的.呵呵!风高月夜,此时正下手之机. Oracle有提供一个叫"外部例程"来实现与其他编程语言的访问.例 ...
- windows ce 5.0 + vs2005 + sql数据库_开发注意事项
今天通过对RFID读写器(windows ce 5.0)的摸索以及实验总结出一下注意事项: 安装 vs2005 后要配置windows ce 的开发环境,从网上下载对应设备版本的SDK,安装后新建项目 ...
- sql server 2008 r2卸载重装_免费下载:Intouch软件、Windows操作系统、SQL数据库,VB6.0、C#...
为大家整理了常用的Windows操作系统和安装软件,基本上都是经过我们项目测试OK的版本,以后项目调试就齐全了,不用再"东奔西走","小鹿乱撞"了.整理不易,若 ...
- 云原生 SQL 数据库 CockroachDB 2.0 发布:性能极大提升
2018 年 4 月,CockroachDB 2.0 版本正式发布,带来全新升级.作为 CockroachDB 2.x 系列的第一个版本,CockroachDB 2.0 极大提升了性能,并带来了一系列 ...
- D-RecoverySQLServer2019达思SQL数据库修复软件升级至3.0版
一.支持的SQL数据库版本: 达思SQL数据库修复软件2019在功能上支持几乎所有的Microsoft SQL Server版本,如: 7.0.2000.2005.2008.2008R2.2012.2 ...
- 因为数据库正在使用,所以无法获得对数据库的独占访问权。 (3101)[42000] [Microsoft][SQL Server Native Client 10.0][SQL Server]RES
问题描述: SQL server 数据库还原时,遇到问题. [RES] Database restore start [RES] (Full) [2022-06-27 22:12:15.000] [ ...
最新文章
- [C++] Lvalue and Rvalue Reference
- 【干货】产品需求确定后,不要着急设计原型
- Dynamic Web Module 3.0 requires Java 1.6 or newer.
- loadrunner脚本设计:检查点(断言)及web_submit_data函数使用示例
- Java枚举enum以及应用:枚举实现单例模式
- 本文中的问题直接重装mysql即可,但是注意数据也会一并消失
- uboot: RTL8201 100M PHY驱动代码
- [POJ2888] Magic Bracelet
- P3346-[ZJOI2015]诸神眷顾的幻想乡【广义SAM】
- CSS3 @media媒体查询 适配不同尺寸设备的响应式布局(清晰详解)
- matlab矩阵处理实验报告,matlab实验报告一二三
- java里新建线程设置线程名字_多线程开发不得不掌握,设置和获取线程名称及JVM如何运行的...
- win10键盘快捷键
- Remote end closed connection without response
- mac 下 python 批量删除 PDF 中的某些页面
- poi 导出 word 表格样式
- 商品规格常用的规格排列组合算法
- 川土微|国产数字隔离器兼容ISO7762用于新能源汽车热管理系统
- 二维数组(矩阵)顺时针旋转90°
- 【Spark你妈喊你回家吃饭-01】 Spark是什么鬼?