当前位置:网站首页>Addition, deletion, modification and query of sqlhelper
Addition, deletion, modification and query of sqlhelper
2022-07-07 21:05:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
When a thing is done repeatedly . You will want to find a way to replace yourself to do this repeated action .
It's the same with tapping code .
In the program . For the repeated part . The assumption is exactly the same , Then we will think of writing it as a method ( The process 、 function ), Put it in a place that can be obtained by anyone who needs permission .
Suppose the demander is in the same project . Then write this method as a class . Suppose the demanders are in the same category . Then create a separate method in this class to write it . Abstract the same thing . For multiple users , Is to use abstract ideas .
No matter what a system , Will involve data transmission 、 operation . The operation of data is nothing more than adding, deleting, modifying and checking (CURD), Today's system with the increase of users , More and more users operate , Big data operates frequently .
Suppose we use the original way to write . Complex systems will cause a lot of redundant code .
Add two references above the class :
Imports System.Data.SqlClient ' quote SQL Database connection
Imports System.Configuration ' Reference profile Set up an operation database SQLHelper class ,
Public Class SQLHelper
' Get the connection string in the configuration file
Private ReadOnly strSQLConnection As String = ConfigurationManager.AppSettings("sqlConcectStr")
' Define connections
Dim connSQL As SqlConnection = New SqlConnection(strSQLConnection)
' Definition cmd command
Dim cmdSQL As New SqlCommand
' ///<summary>
' ///depiction:< The method is sqlhelper Class initialization >
' ///</summary>
Public Sub New()
connSQL = New SqlConnection(strSQLConnection)
End Sub
'' ///<summary>
'' ///depiction:< The method is to close the connection to the database >
'' ///<summary>
Private Sub CloseSQLConnection()
' Infer whether the database connection object state is disconnected . The assumption is not broken , The disconnect
If connSQL.State <> ConnectionState.Closed Then
connSQL.Close()
End If
End Sub
' ///<summary>
' ///depiction:< The method is to close the database command >
' ///</summary>
Private Sub CloseSQLCommand()
' Suppose the command exists . Then close
If Not IsNothing(cmdSQL) Then
cmdSQL.Dispose() ' Destroy order
cmdSQL = Nothing
End If
End Sub
'///<summary>
''// Run the three operations of adding, deleting and modifying .( Ginseng ) The return value is Boolean type , Confirm whether the operation is successful
'///</summary>
' ///<param name="strSql"> You need to run the statement . Usually Sql sentence , There are also stored procedures </param>
' ///<param name="cmdType"> infer Sql Type of statement , Generally, it is not a stored procedure </param>
' ///<returns>
' ///< return Boolean, Success for true. Otherwise false>
' ///</returns>
Public Function ExecuteAddDelUpdate(ByVal strSql As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As Boolean
' Fill the class's own... With the passed parameters cmd object
cmdSQL.Parameters.AddRange(sqlParams) ' Parameter incoming
cmdSQL.CommandType = cmdType '
cmdSQL.Connection = connSQL ' Connect
cmdSQL.CommandText = strSql ' Query statement
Try
connSQL.Open() ' Open the connection
Return cmdSQL.ExecuteNonQuery() ' operation
cmdSQL.Parameters.Clear() ' Clear parameters
Catch ex As Exception
Return False
Finally
Call CloseSQLConnection() ' Close the connection
Call CloseSQLCommand() ' Closing order
End Try
End Function
'///<summary>
'/// Run the three operations of adding, deleting and modifying ,( No reference ) The return value is Boolean type , Confirm whether the operation is successful
''///</summary>
'///<param name="strSql"> You need to run the statement . Usually Sql sentence , There are also stored procedures </param>
' ///<returns>
' ///< return Boolean type . Success for true, Otherwise false>
'///</returns>
Public Function ExecuteAddDelUpdate(ByVal strSql As String, ByVal cmdType As CommandType) As Boolean
' Fill the class's own... With the passed parameters cmd object
cmdSQL.CommandType = cmdType ' take
cmdSQL.Connection = connSQL ' Establishing a connection
cmdSQL.CommandText = strSql ' Set query statement
Try
connSQL.Open() ' Open the connection
Return cmdSQL.ExecuteNonQuery() ' return sql Number of rows affected after running
Catch ex As Exception
Return False
Finally
Call CloseSQLConnection() ' Close the connection
Call CloseSQLCommand() ' Closing order
End Try
End Function
'///<summary>
''/// Run the query operation ,( Ginseng ) The return value is DataTable type
'///</summary>
' ///<param name="strSql"> You need to run the statement . Usually Sql sentence . There are also stored procedures </param>
'' ///<param name="cmdType"> infer Sql Type of statement . Generally, it is not a stored procedure </param>
' ///<returns>
' ///< Go back to the table >
' ///</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
' Fill the class's own... With the passed parameters cmd object
cmdSQL.Parameters.AddRange(sqlParams) ' Pass in parameters
cmdSQL.CommandType = cmdType
cmdSQL.Connection = connSQL ' Establishing a connection
cmdSQL.CommandText = strSql ' Query statement
sqlAdapter = New SqlDataAdapter(cmdSQL) ' Instantiation Adapter
Try
sqlAdapter.Fill(dsSQL) ' use Adater take DataSet fill
dtSQL = dsSQL.Tables(0) 'DataTable by DataSet The first table of
cmdSQL.Parameters.Clear() ' Clear parameters
Catch ex As Exception
MsgBox(" The query fails ", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), " Warning ")
Finally
Call CloseSQLCommand()
End Try
Return dtSQL
End Function
'///<summary>
''// Run the query operation ,( No reference ) The return value is DataTable type
''</summary>
' ///<param name="strSql"> You need to run the statement , Usually Sql sentence , There are also stored procedures </param>
' ///<param name="cmdType"> infer Sql Type of statement , Generally, it is not a stored procedure </param>
' ///<returns>
' ///< Go back to the table >
' ///</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
' Fill the class's own... With the passed parameters cmd object
cmdSQL.CommandText = strSql
cmdSQL.CommandType = cmdType
cmdSQL.Connection = connSQL
sqlAdapter = New SqlDataAdapter(cmdSQL) ' Instantiation Adapter
Try
sqlAdapter.Fill(dsSQL) ' use Adaper take DataSet fill
dtSQL = dsSQL.Tables(0) 'DataTable by DataSet The first table of
Catch ex As Exception
MsgBox(" The query fails ", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), " Warning ")
Finally
Call CloseSQLCommand()
End Try
Return dtSQL
End Function
End ClassPublisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116442.html Link to the original text :https://javaforall.cn
边栏推荐
- Intelligent software analysis platform embold
- Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
- Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]
- 私募基金在中国合法吗?安全吗?
- Cocos2d-x game archive [easy to understand]
- What are the official stock trading apps in the country? Is it safe to use
- Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
- ISO 26262 - 基于需求测试以外的考虑因素
- 解决使用uni-app MediaError MediaError ErrorCode -5
- 使用高斯Redis实现二级索引
猜你喜欢

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

程序猿赚的那点钱算个P啊!

OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验

Tensorflow2. How to run under x 1 Code of X
CodeSonar通过创新型静态分析增强软件可靠性

Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
![[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

上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法

95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全

【C语言】指针进阶---指针你真的学懂了吗?
随机推荐
DataTable数据转换为实体
uva 12230 – Crossing Rivers(概率)「建议收藏」
如何满足医疗设备对安全性和保密性的双重需求?
How to meet the dual needs of security and confidentiality of medical devices?
sqlHelper的增删改查
华泰证券可以做到万一佣金吗,万一开户安全嘛
想杀死某个端口进程,但在服务列表中却找不到,可以之间通过命令行找到这个进程并杀死该进程,减少重启电脑和找到问题根源。
上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
AADL inspector fault tree safety analysis module
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
margin 等高布局
Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
程序猿赚的那点钱算个P啊!
智能软件分析平台Embold
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
Nebula Importer 数据导入实践
国家正规的股票交易app有哪些?使用安不安全
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
【C语言】指针进阶---指针你真的学懂了吗?