当前位置:网站首页>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
边栏推荐
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- AADL inspector fault tree safety analysis module
- 阿里云有奖体验:如何通过ECS挂载NAS文件系统
- Nebula importer data import practice
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
- How to choose financial products? Novice doesn't know anything
- 备份 TiDB 集群到持久卷
- C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
- H3C s7000/s7500e/10500 series post stack BFD detection configuration method
- 九度 1201 -二叉排序数遍历- 二叉排序树「建议收藏」
猜你喜欢
Network principle (1) - overview of basic principles
Codesonar enhances software reliability through innovative static analysis
如何满足医疗设备对安全性和保密性的双重需求?
Make this crmeb single merchant wechat mall system popular, so easy to use!
Mysql子查询关键字的使用方式(exists)
Jenkins 用户权限管理
AADL inspector fault tree safety analysis module
Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
如何满足医疗设备对安全性和保密性的双重需求?
Implement secondary index with Gaussian redis
随机推荐
Cantata9.0 | new features
阿洛的烦恼
Mrs offline data analysis: process OBS data through Flink job
怎样用Google APIs和Google的应用系统进行集成(1)—-Google APIs简介
FTP steps for downloading files from Huawei CE switches
Tensorflow2. How to run under x 1 Code of X
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
网络原理(1)——基础原理概述
[award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
Apifox interface integrated management new artifact
MySQL约束之默认约束default与零填充约束zerofill
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
实战:sqlserver 2008 扩展事件-XML转换为标准的table格式[通俗易懂]
理财产品要怎么选?新手还什么都不懂
AADL inspector fault tree safety analysis module
刚开户的能买什么股票呢?炒股账户安全吗
Micro service remote debug, nocalhost + rainbow micro service development second bullet
华为CE交换机下载文件FTP步骤
How does codesonar help UAVs find software defects?
寫一下跳錶