当前位置:网站首页>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
边栏推荐
- Codesonar Webinar
- Phoenix JDBC
- 机械臂速成小指南(十一):坐标系的标准命名
- Cantata9.0 | 全 新 功 能
- Klocwork 代码静态分析工具
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
- 使用高斯Redis实现二级索引
- 写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
猜你喜欢

机械臂速成小指南(十一):坐标系的标准命名

Mrs offline data analysis: process OBS data through Flink job

机械臂速成小指南(十二):逆运动学分析

智能软件分析平台Embold

Onespin | solve the problems of hardware Trojan horse and security trust in IC Design

Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system

C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)

ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your

Tensorflow2.x下如何运行1.x的代码

I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
随机推荐
Write a jump table
EasyGBS级联时,上级平台重启导致推流失败、画面卡住该如何解决?
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
[concept of network principle]
H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
恶魔奶爸 A0 英文零基础的自我提升路
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
测量楼的高度
OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验
恶魔奶爸 C
Cantata9.0 | new features
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
CodeSonar通过创新型静态分析增强软件可靠性
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
How to choose fund products? What fund is suitable to buy in July 2022?
2022如何评估与选择低代码开发平台?
Referrer和Referrer-Policy简介
最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
Validutil, "Rethinking the setting of semi supervised learning on graphs"