当前位置:网站首页>sqlHelper的增删改查
sqlHelper的增删改查
2022-07-07 18:44:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
当一件事情被反复做了多次后。会想找一种办法来取代自己去做这个反复的动作。
敲代码也一样。
在程序中。对于反复的部分。假设是全然同样,那我们就会想着将其写成一个方法(过程、函数),放在一个具有权限的需求者都可以得着的地儿。
假设需求者在同一项目中。那么就把这种方法写成一个类。假设需求者在同一类中。那么就在本类中单独建一个方法写它。将同样的东西抽象出来。供多用户调用,就是用的抽象的思想。
不论什么一个系统,都会涉及数据的传输、操作。而数据的操作概括起来不外乎增删改查(CURD),如今的系统随着使用者的增多,越来越多的用户操作,大数据频繁操作。
假设採用原来的方式来写。复杂的系统会造成大量赘余的代码。
在类上方加入两条引用:
Imports System.Data.SqlClient '引用SQL数据库连接
Imports System.Configuration '引用配置文件建立一个操作数据库的SQLHelper类,
Public Class SQLHelper
'获取配置文件里的连接字符串
Private ReadOnly strSQLConnection As String = ConfigurationManager.AppSettings("sqlConcectStr")
'定义连接
Dim connSQL As SqlConnection = New SqlConnection(strSQLConnection)
'定义cmd命令
Dim cmdSQL As New SqlCommand
' ///<summary>
' ///depiction:<该方法是sqlhelper类的初始化>
' ///</summary>
Public Sub New()
connSQL = New SqlConnection(strSQLConnection)
End Sub
'' ///<summary>
'' ///depiction:<该方法是关闭数据库的连接>
'' ///<summary>
Private Sub CloseSQLConnection()
'推断数据库连接对象状态是否为断开。假设还没有断,则断开
If connSQL.State <> ConnectionState.Closed Then
connSQL.Close()
End If
End Sub
' ///<summary>
' ///depiction:<该方法是关闭数据库命令>
' ///</summary>
Private Sub CloseSQLCommand()
'假设命令存在。则关闭
If Not IsNothing(cmdSQL) Then
cmdSQL.Dispose() '销毁命令
cmdSQL = Nothing
End If
End Sub
'///<summary>
''//运行增删改三个操作。(有參)返回值为Boolean类型,确认是否运行成功
'///</summary>
' ///<param name="strSql">须要运行语句。通常是Sql语句,也有存储过程</param>
' ///<param name="cmdType">推断Sql语句的类型,一般都不是存储过程</param>
' ///<returns>
' ///<返回Boolean,成功为true。否则为false>
' ///</returns>
Public Function ExecuteAddDelUpdate(ByVal strSql As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As Boolean
'用传进的參数填充本类自己的cmd对象
cmdSQL.Parameters.AddRange(sqlParams) '參数传入
cmdSQL.CommandType = cmdType '
cmdSQL.Connection = connSQL '连接
cmdSQL.CommandText = strSql '查询语句
Try
connSQL.Open() '打开连接
Return cmdSQL.ExecuteNonQuery() '运行操作
cmdSQL.Parameters.Clear() '清除參数
Catch ex As Exception
Return False
Finally
Call CloseSQLConnection() '关闭连接
Call CloseSQLCommand() '结束命令
End Try
End Function
'///<summary>
'///运行增删改三个操作,(无參)返回值为Boolean类型,确认是否运行成功
''///</summary>
'///<param name="strSql">须要运行语句。通常是Sql语句,也有存储过程</param>
' ///<returns>
' ///<返回Boolean类型。成功为true,否则为false>
'///</returns>
Public Function ExecuteAddDelUpdate(ByVal strSql As String, ByVal cmdType As CommandType) As Boolean
'用传进的參数填充类自己的cmd对象
cmdSQL.CommandType = cmdType '将
cmdSQL.Connection = connSQL '建立连接
cmdSQL.CommandText = strSql '设置查询语句
Try
connSQL.Open() '打开连接
Return cmdSQL.ExecuteNonQuery() '返回sql运行后受影响的行数
Catch ex As Exception
Return False
Finally
Call CloseSQLConnection() '关闭连接
Call CloseSQLCommand() '结束命令
End Try
End Function
'///<summary>
''///运行查询操作,(有參)返回值为DataTable类型
'///</summary>
' ///<param name="strSql">须要运行语句。通常是Sql语句。也有存储过程</param>
'' ///<param name="cmdType">推断Sql语句的类型。一般都不是存储过程</param>
' ///<returns>
' ///<返回表>
' ///</returns>
Public Function ExecuteSelect(ByVal strSql As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable
Dim sqlAdapter As SqlDataAdapter
Dim dtSQL As New DataTable
Dim dsSQL As New DataSet
'用传进的參数填充本类自己的cmd对象
cmdSQL.Parameters.AddRange(sqlParams) '传入參数
cmdSQL.CommandType = cmdType
cmdSQL.Connection = connSQL '建立连接
cmdSQL.CommandText = strSql '查询语句
sqlAdapter = New SqlDataAdapter(cmdSQL) '实例化Adapter
Try
sqlAdapter.Fill(dsSQL) '用Adater将DataSet填充
dtSQL = dsSQL.Tables(0) 'DataTable为DataSet的第一个表
cmdSQL.Parameters.Clear() '清除參数
Catch ex As Exception
MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
Finally
Call CloseSQLCommand()
End Try
Return dtSQL
End Function
'///<summary>
''//运行查询操作,(无參)返回值为DataTable类型
''</summary>
' ///<param name="strSql">须要运行语句,通常是Sql语句,也有存储过程</param>
' ///<param name="cmdType">推断Sql语句的类型,一般都不是存储过程</param>
' ///<returns>
' ///<返回表>
' ///</returns>
Public Function ExecuteSelect(ByVal strSql As String, ByVal cmdType As CommandType) As DataTable
Dim sqlAdapter As SqlDataAdapter
Dim dtSQL As New DataTable
Dim dsSQL As New DataSet
'用传进的參数填充类自己的cmd对象
cmdSQL.CommandText = strSql
cmdSQL.CommandType = cmdType
cmdSQL.Connection = connSQL
sqlAdapter = New SqlDataAdapter(cmdSQL) '实例化Adapter
Try
sqlAdapter.Fill(dsSQL) '用Adaper将DataSet填充
dtSQL = dsSQL.Tables(0) 'DataTable为DataSet的第一个表
Catch ex As Exception
MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
Finally
Call CloseSQLCommand()
End Try
Return dtSQL
End Function
End Class发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116442.html原文链接:https://javaforall.cn
边栏推荐
- Referrer和Referrer-Policy简介
- How to meet the dual needs of security and confidentiality of medical devices?
- 有用的win11小技巧
- Cantata9.0 | 全 新 功 能
- Écrivez une liste de sauts
- Make this crmeb single merchant wechat mall system popular, so easy to use!
- Measure the height of the building
- 嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
- 神兵利器——敏感文件发现工具
- Tensorflow2. How to run under x 1 Code of X
猜你喜欢

The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization

最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化

Apifox 接口一体化管理新神器

Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围

软件缺陷静态分析 CodeSonar 5.2 新版发布
![[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System](/img/76/b725788272ba2dcdf866b28cbcc897.jpg)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
CodeSonar通过创新型静态分析增强软件可靠性

Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)

Tensorflow2. How to run under x 1 Code of X

使用camunda做工作流设计,驳回操作
随机推荐
上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
Codesonar enhances software reliability through innovative static analysis
让这个CRMEB单商户微信商城系统火起来,太好用了!
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
Apifox interface integrated management new artifact
[résolution] le paquet « xxxx» n'est pas dans goroot
Meta Force原力元宇宙系统开发佛萨奇模式
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Implement secondary index with Gaussian redis
如何挑选基金产品?2022年7月份适合买什么基金?
EasyGBS级联时,上级平台重启导致推流失败、画面卡住该如何解决?
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
Micro service remote debug, nocalhost + rainbow micro service development second bullet
程序猿赚的那点钱算个P啊!
CodeSonar网络研讨会
How to meet the dual needs of security and confidentiality of medical devices?