初始化
This commit is contained in:
736
DataBase/DbCmdHelper.vb
Normal file
736
DataBase/DbCmdHelper.vb
Normal file
@@ -0,0 +1,736 @@
|
||||
Imports System.Text
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 数据库语句助手
|
||||
''' 时间:2020-12-21
|
||||
''' 作者:ML
|
||||
''' 版本:1.0
|
||||
'''
|
||||
''' 注意:添加一条数据库帮助语句时,需要考虑Mysql/Sqlite/Mssql等数据库是否支持命令,不支持则需要在对应帮助类中重写该帮助语句
|
||||
''' 注意:Sqlite数据库与大多数据库不相同,DB开头数据库语句大多不适用
|
||||
'''
|
||||
''' </summary>
|
||||
Public MustInherit Class DbCmdHelper
|
||||
Protected FiledSuffix As Char
|
||||
Protected FiledPrefix As Char
|
||||
|
||||
Public Shared Function CreateCmdHelper(type As DbExecutor.DbTypeEnum) As DbCmdHelper
|
||||
Select Case type
|
||||
Case DbExecutor.DbTypeEnum.Mysql
|
||||
Return New MysqlCmdHelper()
|
||||
Case DbExecutor.DbTypeEnum.Mssql
|
||||
Return New MssqlCmdHelper()
|
||||
Case DbExecutor.DbTypeEnum.Sqlite
|
||||
Return New SqliteCmdHelper()
|
||||
Case Else
|
||||
Throw New Exception($"CreateCmdHelper :Unknown Type {type}")
|
||||
End Select
|
||||
End Function
|
||||
|
||||
|
||||
#Region "访问单数据库连接"
|
||||
''' <summary>
|
||||
''' 查询指定数据表符合条件的所有数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="condition">查询条件,</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function SearchAll(tableName As String, Optional condition As String = "") As String
|
||||
If String.IsNullOrWhiteSpace(condition) Then
|
||||
Return $"Select * FROM {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
Else
|
||||
Return $"Select * FROM {FiledSuffix}{tableName}{FiledPrefix} WHERE {condition};"
|
||||
End If
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询表符合条件的所有指定列的数据
|
||||
''' </summary>
|
||||
''' <param name="columnName">列名集合,需要返回多列时用','符号分隔列名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Search(columnName As String, tableName As String, Optional condition As String = "") As String
|
||||
If String.IsNullOrWhiteSpace(condition) Then
|
||||
Return $"Select {columnName} FROM {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
Else
|
||||
Return $"Select {columnName} FROM {FiledSuffix}{tableName}{FiledPrefix} WHERE {condition};"
|
||||
End If
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询表符合条件的所有指定列的数据
|
||||
''' </summary>
|
||||
''' <param name="columnName">表名</param>
|
||||
''' <param name="tableName">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Search(columnName As List(Of String), tableName As String, Optional condition As String = "") As String
|
||||
Dim colNameString As New StringBuilder
|
||||
For i As Integer = 0 To columnName.Count - 1
|
||||
If i = 0 Then
|
||||
colNameString.Append($"{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
Else
|
||||
colNameString.Append($",{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
End If
|
||||
Next
|
||||
|
||||
If String.IsNullOrWhiteSpace(condition) Then
|
||||
Return $"Select {colNameString} FROM {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
Else
|
||||
Return $"Select {colNameString} FROM {FiledSuffix}{tableName}{FiledPrefix} Where {condition};"
|
||||
End If
|
||||
End Function
|
||||
|
||||
Public Overridable Function SearchOrder(columnName As List(Of String), tableName As String, Optional orderString As String = "") As String
|
||||
Dim colNameString As New StringBuilder
|
||||
For i As Integer = 0 To columnName.Count - 1
|
||||
If i = 0 Then
|
||||
colNameString.Append($"{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
Else
|
||||
colNameString.Append($",{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
End If
|
||||
Next
|
||||
|
||||
If String.IsNullOrWhiteSpace(orderString) Then
|
||||
Return $"Select {colNameString} FROM {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
Else
|
||||
Return $"Select {colNameString} FROM {FiledSuffix}{tableName}{FiledPrefix} {orderString};"
|
||||
End If
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定表包含的内容行数
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <param name="condition">查询条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function SearchCount(tableName As String, Optional condition As String = "") As String
|
||||
Return Search("count(*)", tableName, condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据表的信息
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function SearchTableInfo(tableName As String) As String
|
||||
Return $"Select * from information_schema.tables where table_name = '{tableName}';"
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据表是否存在的信息,返回查询当前表在数据库中存在的数量
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function SearchTableExists(tableName As String) As String
|
||||
Return $"Select count(*) from information_schema.tables where table_name = '{tableName}';"
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 数据表插入一行数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colNames">列名字符串</param>
|
||||
''' <param name="values">列值字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Insert(tableName As String, colNames As String, values As String) As String
|
||||
' Console.WriteLine($"Insert into {FiledSuffix}{tableName}{FiledPrefix} ({colNames}) Values ( {values} );")
|
||||
Return $"Insert into {FiledSuffix}{tableName}{FiledPrefix} ({colNames}) Values ( {values} );"
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 数据表插入一行数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="dicNameValues">列名与列值键值对</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Insert(tableName As String, dicNameValues As Dictionary(Of String, String)) As String
|
||||
Dim colNames As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
Dim cons As String
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If colNames.Length = 0 Then
|
||||
colNames.Append($"{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($"'{keyValuePair.Value.Replace("&", "").Replace(vbNullChar, "").Replace(vbCrLf, "").Replace(vbLf, "").Replace("&", "")}'")
|
||||
Else
|
||||
colNames.Append($",{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($",'{keyValuePair.Value.Replace("&", "").Replace(vbNullChar, "").Replace(vbCrLf, "").Replace(vbLf, "").Replace("&", "")}'")
|
||||
End If
|
||||
'If keyValuePair.Value.Contains("44;版本号:C1F_C12_BS_220523A1") Then
|
||||
' Console.WriteLine($",'{keyValuePair.Value.Replace("&", "")}'")
|
||||
'End If
|
||||
Next
|
||||
|
||||
Return Insert(tableName, colNames.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 数据表插入一行,通过命令参数方式执行时使用
|
||||
''' </summary>
|
||||
''' <param name="tableName"></param>
|
||||
''' <param name="dicNameValues"></param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function InsertParam(tableName As String, dicNameValues As Dictionary(Of String, String)) As String
|
||||
Dim colNames As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If colNames.Length = 0 Then
|
||||
colNames.Append($"{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($"{keyValuePair.Value}")
|
||||
Else
|
||||
colNames.Append($",{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($",{keyValuePair.Value}")
|
||||
End If
|
||||
Next
|
||||
Return Insert(tableName, colNames.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 数据表插入一行,通过命令参数方式执行时使用,参数名由@{ColName}
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colNames">字段列表</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function InsertParam(tableName As String, colNames As List(Of String)) As String
|
||||
Dim colNameString As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each colName As String In colNames
|
||||
If colNameString.Length = 0 Then
|
||||
colNameString.Append($"{FiledSuffix}{colName}{FiledPrefix}")
|
||||
values.Append($"@{colName}")
|
||||
Else
|
||||
colNameString.Append($",{FiledSuffix}{colName}{FiledPrefix}")
|
||||
values.Append($",@{colName}")
|
||||
End If
|
||||
Next
|
||||
Return Insert(tableName, colNameString.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定表数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="destStr">更新字符串</param>
|
||||
''' <param name="condition"></param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Update(tableName As String, destStr As String, condition As String) As String
|
||||
Return $"Update {FiledSuffix}{tableName}{FiledPrefix} Set {destStr} Where {condition};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定表数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="dicNameValues">更新列名与列值键值对</param>
|
||||
''' <param name="condition">更新列索引条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function Update(tableName As String, dicNameValues As Dictionary(Of String, String), condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If destStr.Length = 0 Then
|
||||
destStr.Append($"{FiledSuffix}{keyValuePair.Key}{FiledPrefix} = '{keyValuePair.Value}'")
|
||||
Else
|
||||
destStr.Append($",{FiledSuffix}{keyValuePair.Key}{FiledPrefix} = '{keyValuePair.Value}'")
|
||||
End If
|
||||
Next
|
||||
Return Update(tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定数据库中指定表数据,参数名由@{ColName}
|
||||
''' </summary>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="colNames">更新列名的集合</param>
|
||||
''' <param name="condition">更新列索引条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function UpdateParam(tableName As String, colNames As List(Of String), condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
For Each colName As String In colNames
|
||||
If destStr.Length = 0 Then
|
||||
destStr.Append($"{FiledSuffix}{colName}{FiledPrefix} = @{colName}")
|
||||
Else
|
||||
destStr.Append($",{FiledSuffix}{colName}{FiledPrefix} = @{colName}")
|
||||
End If
|
||||
Next
|
||||
Return Update(tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据表增加一列数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colName">列名</param>
|
||||
''' <param name="colType">列类型</param>
|
||||
''' <param name="isNull">是否允许为空</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function AddCol(tableName As String, colName As String, colType As String, Optional isNull As Boolean = True) As String
|
||||
Return $"Alter Table {FiledSuffix}{tableName}{FiledPrefix} Add {FiledSuffix}{colName}{FiledPrefix} {colType} {IIf(isNull, "Default Null", "Not Null")};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 数据表删除一列数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colName">需要删除的列名,仅一列</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DropCol(tableName As String, colName As String) As String
|
||||
Return $"Alter Table {FiledSuffix}{tableName}{FiledPrefix} Drop Column {FiledSuffix}{colName}{FiledPrefix};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 删除指定表多行数据
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DeleteRows(tableName As String, condition As String) As String
|
||||
Return $"Delete From {FiledSuffix}{tableName}{FiledPrefix} Where {condition};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 创建数据表
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="createStr">创建表的列信息字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function CreateTable(tableName As String, createStr As String) As String
|
||||
Return $"Create Table {FiledSuffix}{tableName}{FiledPrefix} ( {createStr} );"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 创建数据表,如果存在则不创建
|
||||
''' </summary>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="createStr">创建表的列信息字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function CreateTableWhenNotExists(tableName As String, createStr As String) As String
|
||||
Return $"Create Table if not exists {FiledSuffix}{tableName}{FiledPrefix} ( {createStr} );"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 清空数据表,表依旧存在
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DeleteTable(tableName As String) As String
|
||||
Return $"Delete From {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 删除数据表
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DropTable(tableName As String) As String
|
||||
Return $"Drop Table {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 删除数据表
|
||||
''' </summary>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DropTableWhenExists(tableName As String) As String
|
||||
Return $"Drop Table If Exists {FiledSuffix}{tableName}{FiledPrefix};"
|
||||
End Function
|
||||
#End Region
|
||||
|
||||
|
||||
#Region "访问多数据库连接"
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表符合条件的所有指定列的数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="ColsName">列名集合,需要返回多列时用','符号分隔列名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearch(dbName As String, colsName As String, tableName As String, Optional condition As String = "") As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Select {colsName} From ")
|
||||
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
|
||||
If String.IsNullOrWhiteSpace(condition) = False Then
|
||||
cmdText.Append($" WHERE {condition}")
|
||||
End If
|
||||
|
||||
cmdText.Append($";")
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表符合条件的所有指定列的去重数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="ColsName">列名集合,需要返回多列时用','符号分隔列名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDistinctSearch(dbName As String, colsName As String, tableName As String, Optional condition As String = "") As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Select Distinct {colsName} From ")
|
||||
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
|
||||
If String.IsNullOrWhiteSpace(condition) = False Then
|
||||
cmdText.Append($" WHERE {condition}")
|
||||
End If
|
||||
|
||||
cmdText.Append($";")
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表符合条件的所有指定列的数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="columnName">表名</param>
|
||||
''' <param name="tableName">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearch(dbName As String, columnName As List(Of String), tableName As String, Optional condition As String = "") As String
|
||||
Dim colNameString As New StringBuilder
|
||||
For i As Integer = 0 To columnName.Count - 1
|
||||
If i = 0 Then
|
||||
colNameString.Append($"{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
Else
|
||||
colNameString.Append($",{FiledSuffix}{columnName(i)}{FiledPrefix}")
|
||||
End If
|
||||
Next
|
||||
|
||||
Return DbSearch(dbName, colNameString.ToString(), tableName, condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定表包含的内容行数
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <param name="condition">查询条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearchCount(dbName As String, tableName As String, Optional condition As String = "") As String
|
||||
Return DbSearch(dbName, "count(*)", tableName, condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表符合条件的所有数据
|
||||
'''
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <param name="condition">查询条件(可选)</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearchAll(dbName As String, tableName As String, Optional condition As String = "") As String
|
||||
Return DbSearch(dbName, "*", tableName, condition)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据库中指定数据表的信息
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearchTableInfo(dbName As String, tableName As String) As String
|
||||
Return DbSearch("", "*", "information_schema.tables", "table_schema = '{dbName}' and table_name = '{tableName}'")
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据表是否存在的信息,返回查询当前表在指定数据库中存在的数量
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbSearchTableExists(dbName As String, tableName As String) As String
|
||||
Return DbSearch("", "count(*)", "information_schema.tables", "table_schema = '{dbName}' and table_name = '{tableName}'")
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中数据表插入一行数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colNames">列名字符串</param>
|
||||
''' <param name="values">列值字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbInsert(dbName As String, tableName As String, colNames As String, values As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Insert into ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" ({colNames}) Values ( {values} );")
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中数据表插入一行数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="dicNameValues">列名与列值键值对</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbInsert(dbName As String, tableName As String, dicNameValues As Dictionary(Of String, String)) As String
|
||||
Dim colNames As New StringBuilder
|
||||
Dim values As New StringBuilder
|
||||
For Each keyValuePair As KeyValuePair(Of String, String) In dicNameValues
|
||||
If colNames.Length = 0 Then
|
||||
colNames.Append($"{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($"'{keyValuePair.Value}'")
|
||||
Else
|
||||
colNames.Append($",{FiledSuffix}{keyValuePair.Key}{FiledPrefix}")
|
||||
values.Append($",'{keyValuePair.Value}'")
|
||||
End If
|
||||
Next
|
||||
|
||||
Return DbInsert(dbName, tableName, colNames.ToString(), values.ToString())
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中数据表插入一行,通过命令参数方式执行时使用,参数名由@{ColName}
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName"></param>
|
||||
''' <param name="colNames">需要插入列名的集合</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbInsertParam(dbName As String, tableName As String, colNames As List(Of String)) As String
|
||||
Dim colNameBuilder As New StringBuilder
|
||||
Dim valueBuilder As New StringBuilder
|
||||
For Each colName As String In colNames
|
||||
If colNameBuilder.Length = 0 Then
|
||||
colNameBuilder.Append($"{FiledSuffix}{colName}{FiledPrefix}")
|
||||
valueBuilder.Append($"@{colName}")
|
||||
Else
|
||||
colNameBuilder.Append($",{FiledSuffix}{colName}{FiledPrefix}")
|
||||
valueBuilder.Append($",@{colName}")
|
||||
End If
|
||||
Next
|
||||
'insert into dbName.tablename (1,2,3) value (@1,@2,@3)
|
||||
Return DbInsert(dbName, tableName, colNameBuilder.ToString(), valueBuilder.ToString())
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定数据库中指定表数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="destStr">更新字符串</param>
|
||||
''' <param name="condition"></param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbUpdate(dbName As String, tableName As String, destStr As String, condition As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
Dim tmpStrCmdText As String = ""
|
||||
|
||||
cmdText.Append($"Update ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" Set {destStr} Where {condition};")
|
||||
tmpStrCmdText = cmdText.ToString()
|
||||
Console.WriteLine("SQL_CMD = " & tmpStrCmdText)
|
||||
|
||||
Return tmpStrCmdText
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定数据库中指定表数据,参数名由@{ColName}
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="colNames">更新列名的集合</param>
|
||||
''' <param name="condition">更新列索引条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbUpdateParam(dbName As String, tableName As String, colNames As List(Of String), condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
For Each colName As String In colNames
|
||||
If destStr.Length = 0 Then
|
||||
destStr.Append($"{FiledSuffix}{colName}{FiledPrefix} = @{colName}")
|
||||
Else
|
||||
destStr.Append($",{FiledSuffix}{colName}{FiledPrefix} = @{colName}")
|
||||
End If
|
||||
Next
|
||||
Return DbUpdate(dbName, tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 更新指定数据库中指定表数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">指定表名</param>
|
||||
''' <param name="filedDictionary">更新列名与列值键值对</param>
|
||||
''' <param name="condition">更新列索引条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbUpdate(dbName As String, tableName As String, filedDictionary As Dictionary(Of String, String), condition As String) As String
|
||||
Dim destStr As New StringBuilder
|
||||
For Each filed As KeyValuePair(Of String, String) In filedDictionary
|
||||
If destStr.Length = 0 Then
|
||||
destStr.Append($"{FiledSuffix}{filed.Key}{FiledPrefix} = '{filed.Value}'")
|
||||
Else
|
||||
destStr.Append($",{FiledSuffix}{filed.Key}{FiledPrefix} = '{filed.Value}'")
|
||||
End If
|
||||
Next
|
||||
Return DbUpdate(dbName, tableName, destStr.ToString(), condition)
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中指定数据表增加一列数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colName">列名</param>
|
||||
''' <param name="colType">列类型</param>
|
||||
''' <param name="isNull">是否允许为空</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbAddCol(dbName As String, tableName As String, colName As String, colType As String, Optional isNull As Boolean = True) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Alter Table ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" Add {FiledSuffix}{colName}{FiledPrefix} {colType} {IIf(isNull, "Default Null", "Not Null")};")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中数据表删除一列数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="colName">需要删除的列名,仅一列</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDropCol(dbName As String, tableName As String, colName As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Alter Table ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" Drop Column {FiledSuffix}{colName}{FiledPrefix};")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中指定表删除多行数据
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="condition">条件</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDeleteRows(dbName As String, tableName As String, condition As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Delete From ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" Where {condition};")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中创建数据表
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="createStr">创建表的列信息字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbCreateTable(dbName As String, tableName As String, createStr As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Create Table ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" ( {createStr} );")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 指定数据库中创建数据表,如果存在则不创建
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">表名</param>
|
||||
''' <param name="createStr">创建表的列信息字符串</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbCreateTableWhenNotExists(dbName As String, tableName As String, createStr As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Create Table if not exists ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($" ( {createStr} );")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 清空指定数据库中数据表,表依旧存在
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDeleteTable(dbName As String, tableName As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Delete From ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($";")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 删除指定数据库中数据表
|
||||
''' </summary>
|
||||
''' <param name="dbName">数据库名</param>
|
||||
''' <param name="tableName">数据表名</param>
|
||||
''' <returns></returns>
|
||||
Public Overridable Function DbDropTable(dbName As String, tableName As String) As String
|
||||
Dim cmdText As New StringBuilder
|
||||
cmdText.Append($"Drop Table ")
|
||||
If String.IsNullOrEmpty(dbName) = False Then
|
||||
cmdText.Append($"{FiledSuffix}{dbName}{FiledPrefix}.")
|
||||
End If
|
||||
cmdText.Append($"{FiledSuffix}{tableName}{FiledPrefix}")
|
||||
cmdText.Append($";")
|
||||
|
||||
Return cmdText.ToString()
|
||||
End Function
|
||||
#End Region
|
||||
End Class
|
||||
377
DataBase/DbExecutor.vb
Normal file
377
DataBase/DbExecutor.vb
Normal file
@@ -0,0 +1,377 @@
|
||||
Imports System.Data.Common
|
||||
Imports System.Data.SqlClient
|
||||
Imports System.Data.SQLite
|
||||
Imports MySql.Data.MySqlClient
|
||||
|
||||
''' <summary>
|
||||
''' 数据库通用命令执行器
|
||||
''' 时间:2020-12-21
|
||||
''' 作者:ML
|
||||
''' 版本:1.0
|
||||
''' </summary>
|
||||
Public Class DbExecutor
|
||||
Implements IDisposable
|
||||
''' <summary>
|
||||
''' 数据库类型,目前支持Mysql与Sqlite
|
||||
''' </summary>
|
||||
Enum DbTypeEnum
|
||||
Mysql
|
||||
Sqlite
|
||||
Mssql
|
||||
End Enum
|
||||
|
||||
|
||||
Private ReadOnly _connectionString As String '数据库连接语句
|
||||
|
||||
Private ReadOnly _dbType As DbTypeEnum '数据库类型
|
||||
|
||||
Private _connection As DbConnection '数据库连接句柄
|
||||
|
||||
Private _command As DbCommand '数据库命令句柄
|
||||
|
||||
Private _dataAdapter As DbDataAdapter '数据库查询填充器句柄
|
||||
|
||||
Private _transaction As DbTransaction '数据库事务句柄
|
||||
|
||||
Private _commandHelper As DbCmdHelper '数据库语句填充助手
|
||||
|
||||
Sub New(type As DbTypeEnum, connectionString As String)
|
||||
_dbType = type
|
||||
_connectionString = connectionString
|
||||
InitByDbType(_dbType)
|
||||
End Sub
|
||||
|
||||
Private Sub InitByDbType(type As DbTypeEnum)
|
||||
Select Case type
|
||||
Case DbTypeEnum.Mysql
|
||||
_connection = New MySqlConnection()
|
||||
_command = _connection.CreateCommand()
|
||||
_dataAdapter = New MySqlDataAdapter() With {.MissingSchemaAction = MissingSchemaAction.AddWithKey}
|
||||
|
||||
_commandHelper = New MysqlCmdHelper()
|
||||
Case DbTypeEnum.Sqlite
|
||||
_connection = New SqliteConnection()
|
||||
_command = _connection.CreateCommand()
|
||||
_dataAdapter = New SQLiteDataAdapter() With {.MissingSchemaAction = MissingSchemaAction.AddWithKey}
|
||||
|
||||
_commandHelper = New SqliteCmdHelper()
|
||||
Case DbTypeEnum.Mssql
|
||||
_connection = New SqlConnection()
|
||||
_command = _connection.CreateCommand()
|
||||
_dataAdapter = New SqlDataAdapter() With {.MissingSchemaAction = MissingSchemaAction.AddWithKey}
|
||||
|
||||
_commandHelper = New MssqlCmdHelper()
|
||||
End Select
|
||||
End Sub
|
||||
|
||||
|
||||
Public ReadOnly Property DatabaseType() As DbTypeEnum
|
||||
Get
|
||||
Return _dbType
|
||||
End Get
|
||||
'Set(value As DbTypeEnum)
|
||||
' _dbType = value
|
||||
' '执行上一个数据库的关闭操作
|
||||
' InitByDbType(_dbType)
|
||||
'End Set
|
||||
End Property
|
||||
|
||||
Public ReadOnly Property Connection() As DbConnection
|
||||
Get
|
||||
Return _connection
|
||||
End Get
|
||||
End Property
|
||||
|
||||
|
||||
Public ReadOnly Property Command() As DbCommand
|
||||
Get
|
||||
Return _command
|
||||
End Get
|
||||
End Property
|
||||
|
||||
Public ReadOnly Property CmdHelper() As DbCmdHelper
|
||||
Get
|
||||
Return _commandHelper
|
||||
End Get
|
||||
End Property
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 打开数据库连接
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Function Open() As Boolean
|
||||
If _connection Is Nothing Then Return False
|
||||
If String.IsNullOrWhiteSpace(_connectionString) Then Return False
|
||||
Try
|
||||
_connection.ConnectionString = _connectionString
|
||||
_connection.Open()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return True
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 关闭数据库连接
|
||||
''' </summary>
|
||||
Public Sub Close()
|
||||
If _connection Is Nothing Then Return
|
||||
If _connection.State = ConnectionState.Closed Then Return
|
||||
_connection.Close()
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 创建当前连接的命令执行句柄
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Function CreateCommand() As DbCommand
|
||||
Return _connection.CreateCommand()
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 运行非查询语句,返回执行该语句受到影响的行数
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteNonQuery(commandText As String) As Integer
|
||||
Dim result As Integer
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
result = _command.ExecuteNonQuery()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 使用命令参数模式执行非查询语句,返回执行该语句受到影响的行数
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <param name="commandParams">执行的数据库命令参数</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteNonQuery(commandText As String, commandParams As DbParameterCollection) As Integer
|
||||
Dim result As Integer
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
_command.Parameters.Clear()
|
||||
For Each param As DbParameter In commandParams
|
||||
_command.Parameters.Add(param)
|
||||
Next
|
||||
result = _command.ExecuteNonQuery()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 执行数据库语句,返回数据库读取流的句柄
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteReader(commandText As String) As DbDataReader
|
||||
Dim result As DbDataReader
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
result = _command.ExecuteReader()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 使用命令参数模式执行数据库语句,返回数据库读取流的句柄
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <param name="commandParams">执行的数据库命令参数</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteReader(commandText As String, commandParams As DbParameterCollection) As DbDataReader
|
||||
Dim result As DbDataReader
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
_command.Parameters.Clear()
|
||||
For Each param As DbParameter In commandParams
|
||||
_command.Parameters.Add(param)
|
||||
Next
|
||||
result = _command.ExecuteReader()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 执行数据库语句,返回查询结果的第一行第一列的内容
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteScalar(commandText As String) As Object
|
||||
Dim result As Object
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
result = _command.ExecuteScalar()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 使用命令参数模式执行数据库语句,返回查询结果的第一行第一列的内容
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <param name="commandParams">执行的数据库命令参数</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteScalar(commandText As String, commandParams As DbParameterCollection) As Object
|
||||
Dim result As Object
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
_command.Parameters.Clear()
|
||||
For Each param As DbParameter In commandParams
|
||||
_command.Parameters.Add(param)
|
||||
Next
|
||||
result = _command.ExecuteScalar()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return result
|
||||
End Function
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 执行数据库语句,返回执行结果返回的数据表,常用于查询命令
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteDataTable(commandText As String, Optional withKey As Boolean = True) As DataTable
|
||||
Dim dataTable As New DataTable
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
If withKey Then
|
||||
_dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
|
||||
Else
|
||||
_dataAdapter.MissingSchemaAction = MissingSchemaAction.Add
|
||||
End If
|
||||
_dataAdapter.SelectCommand = _command
|
||||
_dataAdapter.Fill(dataTable)
|
||||
Catch ex As Exception
|
||||
'Throw
|
||||
End Try
|
||||
Return dataTable
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 执行数据库语句,返回执行结果返回的数据表,常用于查询命令
|
||||
''' </summary>
|
||||
''' <param name="commandText">执行的数据库命令文本</param>
|
||||
''' <param name="commandParams">执行的数据库命令参数</param>
|
||||
''' <returns></returns>
|
||||
Public Function ExecuteDataTable(commandText As String, commandParams As DbParameterCollection) As DataTable
|
||||
Dim dataTable As New DataTable
|
||||
Try
|
||||
_command.CommandText = commandText
|
||||
_command.Parameters.Clear()
|
||||
For Each param As DbParameter In commandParams
|
||||
_command.Parameters.Add(param)
|
||||
Next
|
||||
_dataAdapter.SelectCommand = _command
|
||||
_dataAdapter.Fill(dataTable)
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
Return dataTable
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 开启事务
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Function BeginTransaction() As DbTransaction
|
||||
_transaction = _connection.BeginTransaction()
|
||||
Return _transaction
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 提交事务
|
||||
''' </summary>
|
||||
Public Sub CommitTransaction()
|
||||
Try
|
||||
_transaction.Commit()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 回滚事务
|
||||
''' </summary>
|
||||
Public Sub RollbackTransaction()
|
||||
Try
|
||||
_transaction.Rollback()
|
||||
Catch ex As Exception
|
||||
Throw
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 创建数据参数
|
||||
''' </summary>
|
||||
''' <param name="type">参数数据类型</param>
|
||||
''' <param name="ParameterName">参数名称</param>
|
||||
''' <param name="value">参数值</param>
|
||||
''' <returns></returns>
|
||||
Public Function CreateDbParameter(type As DbType, parameterName As String, value As Object) As DbParameter
|
||||
Dim dbParam As DbParameter = _command.CreateParameter()
|
||||
dbParam.DbType = type
|
||||
dbParam.ParameterName = parameterName
|
||||
dbParam.Value = value
|
||||
Return dbParam
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 添加数据参数
|
||||
''' </summary>
|
||||
''' <param name="type"></param>
|
||||
''' <param name="parameterName"></param>
|
||||
''' <param name="value"></param>
|
||||
''' <returns></returns>
|
||||
Public Function AddDbParameter(type As DbType, parameterName As String, value As Object) As DbParameter
|
||||
Dim dbParam As DbParameter = _command.CreateParameter()
|
||||
dbParam.DbType = type
|
||||
dbParam.ParameterName = parameterName
|
||||
dbParam.Value = value
|
||||
_command.Parameters.Add(dbParam)
|
||||
Return dbParam
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 清空数据
|
||||
''' </summary>
|
||||
Public Sub ClearDbParameter()
|
||||
_command.Parameters.Clear()
|
||||
End Sub
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 回收资源
|
||||
''' </summary>
|
||||
Public Sub Dispose() Implements IDisposable.Dispose
|
||||
If _connection IsNot Nothing Then
|
||||
If _connection.State = ConnectionState.Open Then
|
||||
_connection.Close()
|
||||
End If
|
||||
_connection.Dispose()
|
||||
End If
|
||||
|
||||
If _command IsNot Nothing Then _command.Dispose()
|
||||
If _dataAdapter IsNot Nothing Then _dataAdapter.Dispose()
|
||||
|
||||
GC.Collect() '对所有缓存垃圾进行回收
|
||||
End Sub
|
||||
End Class
|
||||
8
DataBase/MssqlCmdHelper.vb
Normal file
8
DataBase/MssqlCmdHelper.vb
Normal file
@@ -0,0 +1,8 @@
|
||||
Public Class MssqlCmdHelper
|
||||
Inherits DbCmdHelper
|
||||
|
||||
Sub New()
|
||||
FiledSuffix = "["c
|
||||
FiledPrefix = "]"c
|
||||
End Sub
|
||||
End Class
|
||||
9
DataBase/MysqlCmdHelper.vb
Normal file
9
DataBase/MysqlCmdHelper.vb
Normal file
@@ -0,0 +1,9 @@
|
||||
Public Class MysqlCmdHelper
|
||||
Inherits DbCmdHelper
|
||||
|
||||
Sub New()
|
||||
FiledSuffix = "`"c
|
||||
FiledPrefix = "`"c
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
220
DataBase/MysqlDataParam.vb
Normal file
220
DataBase/MysqlDataParam.vb
Normal file
@@ -0,0 +1,220 @@
|
||||
Imports System.Text
|
||||
|
||||
Public Class MysqlDataParam
|
||||
Enum DataTypeEnum
|
||||
'###############################数值类型#############################
|
||||
''' <summary>
|
||||
''' 1 byte,小整数值
|
||||
''' </summary>
|
||||
Tinyint
|
||||
|
||||
''' <summary>
|
||||
''' 2 bytes,大整数值
|
||||
''' </summary>
|
||||
Smallint
|
||||
|
||||
''' <summary>
|
||||
''' 3 bytes,大整数值
|
||||
''' </summary>
|
||||
Mediumint
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,大整数值
|
||||
''' </summary>
|
||||
Int
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,大整数值
|
||||
''' </summary>
|
||||
[Integer]
|
||||
|
||||
''' <summary>
|
||||
''' 8 bytes,极大整数值
|
||||
''' </summary>
|
||||
Bigint
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,单精度浮点数值
|
||||
''' </summary>
|
||||
Float
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 8 bytes,双精度浮点数值
|
||||
''' </summary>
|
||||
[Double]
|
||||
|
||||
|
||||
|
||||
'####################日期类型###############################
|
||||
|
||||
''' <summary>
|
||||
''' 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2.小数值
|
||||
''' </summary>
|
||||
[Decimal]
|
||||
|
||||
''' <summary>
|
||||
''' 3 bytes,日期值,YYYY-MM-DD
|
||||
''' </summary>
|
||||
[Date]
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 3 bytes,时间值或持续时间,HH:MM:SS
|
||||
''' </summary>
|
||||
Time
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 1 bytes,年份值,YYYY
|
||||
''' </summary>
|
||||
Year
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 8 bytes,混合日期和时间值,YYYY-MM-DD HH:MM:SS
|
||||
''' </summary>
|
||||
Datetime
|
||||
|
||||
''' <summary>
|
||||
''' 4 bytes,混合日期和时间值,时间戳,YYYYMMDD HHMMSS
|
||||
''' </summary>
|
||||
Timestamp
|
||||
|
||||
|
||||
'####################字符类型###############################
|
||||
|
||||
''' <summary>
|
||||
''' 0-255 bytes,定长字符串
|
||||
''' </summary>
|
||||
[Char]
|
||||
|
||||
''' <summary>
|
||||
''' 0-65535 bytes,变长字符串
|
||||
''' </summary>
|
||||
Varchar
|
||||
|
||||
''' <summary>
|
||||
''' 0-255 bytes,不超过 255 个字符的二进制字符串
|
||||
''' </summary>
|
||||
Tinyblob
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 0-255 bytes,短文本字符串
|
||||
''' </summary>
|
||||
Tinytext
|
||||
|
||||
''' <summary>
|
||||
''' 0-65 535 bytes,二进制形式的长文本数据
|
||||
''' </summary>
|
||||
Blob
|
||||
|
||||
''' <summary>
|
||||
''' 0-65 535 bytes,长文本数据
|
||||
''' </summary>
|
||||
Text
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 0-16 777 215 bytes,二进制形式的中等长度文本数据
|
||||
''' </summary>
|
||||
Mediumblob
|
||||
|
||||
''' <summary>
|
||||
''' 0-16 777 215 bytes,中等长度文本数据
|
||||
''' </summary>
|
||||
Mediumtext
|
||||
|
||||
''' <summary>
|
||||
''' 0-4 294 967 295 bytes,二进制形式的极大文本数据
|
||||
''' </summary>
|
||||
Longblob
|
||||
|
||||
''' <summary>
|
||||
''' 0-4 294 967 295 bytes,极大文本数据
|
||||
''' </summary>
|
||||
Longtext
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 列名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property ColumnName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property Value() As String
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 默认值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DefaultValue() As String
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataType() As DataTypeEnum
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型长度
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataTypeLength() As Integer
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型是否带符号
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsUnsigned() As Boolean
|
||||
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 是否允许为空
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsNull() As Boolean = True
|
||||
|
||||
''' <summary>
|
||||
''' 是否自动增长
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsAutoIncrement() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为主键
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsPrimaryKey() As Boolean
|
||||
|
||||
Public Function ToAddColString() As String
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"`{ColumnName}`")
|
||||
|
||||
Select Case DataType
|
||||
Case DataTypeEnum.Varchar, DataTypeEnum.[Char]
|
||||
sb.Append($" {DataType}({DataTypeLength}) ")
|
||||
Case DataTypeEnum.Int
|
||||
sb.Append($" {DataType}")
|
||||
If IsUnsigned Then sb.Append($" Unsigned")
|
||||
If IsAutoIncrement Then sb.Append($" AUTO_INCREMENT")
|
||||
Case Else
|
||||
sb.Append($" {DataType}")
|
||||
End Select
|
||||
|
||||
sb.Append(IIf(IsNull, " Default Null", " Not Null"))
|
||||
|
||||
If IsPrimaryKey Then
|
||||
sb.Append($" PRIMARY KEY")
|
||||
End If
|
||||
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
End Class
|
||||
19
DataBase/SqliteCmdHelper.vb
Normal file
19
DataBase/SqliteCmdHelper.vb
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
Public Class SqliteCmdHelper
|
||||
Inherits DbCmdHelper
|
||||
|
||||
Sub New()
|
||||
FiledSuffix = "["c
|
||||
FiledPrefix = "]"c
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' 查询指定数据表的信息
|
||||
''' </summary>
|
||||
''' <param name="tableName"></param>
|
||||
''' <returns></returns>
|
||||
Public Overrides Function SearchTableInfo(tableName As String) As String
|
||||
Return $"select * from sqlite_master where tbl_name = '{tableName}';"
|
||||
End Function
|
||||
|
||||
End Class
|
||||
105
DataBase/SqliteDataParam.vb
Normal file
105
DataBase/SqliteDataParam.vb
Normal file
@@ -0,0 +1,105 @@
|
||||
Imports System.Text
|
||||
|
||||
|
||||
Public Class SqliteDataParam
|
||||
Enum DataTypeEnum
|
||||
Varchar
|
||||
Nchar
|
||||
Blob
|
||||
Bit
|
||||
Datetime
|
||||
[Decimal]
|
||||
Real
|
||||
UniqueIdentifier
|
||||
Int
|
||||
[Integer]
|
||||
TinyInt
|
||||
[Single]
|
||||
Nvarchar
|
||||
SmallInt
|
||||
SmallUint
|
||||
Uint
|
||||
UnsignedInteger
|
||||
End Enum
|
||||
|
||||
''' <summary>
|
||||
''' 列名
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property ColumnName() As String
|
||||
|
||||
''' <summary>
|
||||
''' 当前值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property Value() As String
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 默认值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DefaultValue() As String
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataType() As DataTypeEnum
|
||||
|
||||
''' <summary>
|
||||
''' 数据类型长度
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property DataTypeLength() As Integer
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' 是否允许为空
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsNull() As Boolean = True
|
||||
|
||||
''' <summary>
|
||||
''' 是否自动增长
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsAutoIncrement() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为主键
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsPrimaryKey() As Boolean
|
||||
|
||||
''' <summary>
|
||||
''' 是否为唯一值
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Public Property IsUnique() As Boolean
|
||||
|
||||
|
||||
Public Function ToAddColString() As String
|
||||
Dim sb As New StringBuilder
|
||||
sb.Append($"`{ColumnName}`")
|
||||
|
||||
Select Case DataType
|
||||
Case DataTypeEnum.Varchar, DataTypeEnum.Nchar, DataTypeEnum.Nvarchar
|
||||
sb.Append($" {DataType}({DataTypeLength}) ")
|
||||
Case DataTypeEnum.Int, DataTypeEnum.Integer
|
||||
sb.Append($" {DataType}")
|
||||
Case Else
|
||||
sb.Append($" {DataType}")
|
||||
End Select
|
||||
|
||||
If IsAutoIncrement Then sb.Append($" AUTOINCREMENT")
|
||||
|
||||
sb.Append(IIf(IsNull, " Default Null", " Not Null"))
|
||||
|
||||
If IsPrimaryKey Then
|
||||
sb.Append($" PRIMARY KEY")
|
||||
End If
|
||||
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
End Class
|
||||
Reference in New Issue
Block a user