各位好
日前系統在AWS的資料庫有作升級
使用的是MariaDB,引擎版本10.6.10
升級完後,資料庫的編碼,就從utf8變成了utf8mb3
我系統的程式是用vb.net作開發的
開發工具:Visual Studio 2012
Framework:.NET Framework 4.5.2
以前在舊版本連線時不會有問題,升級上來後便會在連線查詢時發生「指定的索引鍵不在字典中」錯誤。
我推測會不會是因為資料庫升級,導致在資料庫連線會有問題
我有嘗試google查詢與問ChatGPT
但仍無法找到解決的方案
還請各位專家給我一些建議與方案,謝謝
以下是資料表欄位:
CREATE TABLE `TURNKEY_MESSAGE_LOG_DETAIL` (
`SEQNO` varchar(8) NOT NULL,
`SUBSEQNO` varchar(5) NOT NULL,
`PROCESS_DTS` varchar(17) DEFAULT NULL,
`TASK` varchar(30) NOT NULL DEFAULT '',
`STATUS` varchar(5) DEFAULT NULL,
`FILENAME` varchar(255) DEFAULT NULL,
`UUID` varchar(40) DEFAULT NULL,
PRIMARY KEY (`SEQNO`,`SUBSEQNO`,`TASK`),
KEY `TURNKEY_MESSAGE_LOG_DETAIL_INDEX1` (`FILENAME`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
SET FOREIGN_KEY_CHECKS=1;
以下是我的程式碼:
Private Sub checkEinvoiceXmlUploadTurnkey()
Dim cmd As String = "SELECT FILENAME FROM turnkey.TURNKEY_MESSAGE_LOG_DETAIL WHERE PROCESS_DTS>=DATE(NOW()) AND TASK='UpCast' AND `STATUS`='G'"
Dim turnkeyDt As DataTable = Me.SelectSQL(cmd)
End Sub
<MethodImpl(MethodImplOptions.Synchronized)> _
Public Function SelectSQL(ByVal command As String, Optional ByVal isExceteAgain As Boolean = False, Optional ByVal isNewConnection As Boolean = False) As DataTable
Dim startTime As Date = Now
Dim data As New DataTable
'set connection
Dim myConn As MySqlConnection
If isNewConnection Then
myConn = dbHelper.getNewConnection
Else
myConn = initCommonConnection()
End If
Dim myDa As New MySqlDataAdapter(command, myConn)
Dim myCb As New MySqlCommandBuilder(myDa)
'send
Try
myDa.Fill(data)
Catch e As Exception
Dim excuteTime As Long = Now.Millisecond - startTime.Millisecond
Dim errorMsg As String = StringModule.getErrorString(e, command, excuteTime)
If e.Message.Contains("Fatal error") Then 'try again if execute under 200 ms
If Not isExceteAgain AndAlso excuteTime < 200 Then
log.Error(fatalMsg + vbCrLf + errorMsg)
Return SelectSQL(command, True, isNewConnection)
End If
End If
log.Error(errorMsg)
MsgModule.alert(errorMsg)
End Try
'close new connection
If isNewConnection Then
myConn.Close()
myConn.Dispose()
End If
Return data
End Function
'建立資料庫連線
Public Function getNewConnection(Optional ByVal again As Boolean = True) As MySqlConnection
Dim newConn As MySqlConnection
Try
newConn = New MySqlConnection(Me.getConnectionString)
newConn.Open()
Return newConn
Catch ex As MySqlException
If again Then
Threading.Thread.Sleep(100)
Return getNewConnection(False)
End If
log.Error(StringModule.getErrorString("Error connecting to the server , conection is nothing : "), ex)
MsgBox("資料庫取得連線失敗", MsgBoxStyle.Critical)
End Try
Return Nothing
End Function
Private Function getConnectionString() As String
Dim connStr As String = "server={0};user id={1};password={2};database={3};port={4};pooling=true;charset=utf8;Convert Zero Datetime=True"
If SystemModule.isTestMode Then
connStr = String.Format(connStr, ConfigModule.getConfig("db.turnkey.test.url"), ConfigModule.getConfig("db.turnkey.test.account"), ConfigModule.getConfig("db.turnkey.test.password"), Me.db, ConfigModule.getConfig("db.turnkey.test.port"))
Else
connStr = String.Format(connStr, ConfigModule.getConfig("db.turnkey.url"), ConfigModule.getConfig("db.turnkey.account"), ConfigModule.getConfig("db.turnkey.password"), Me.db, ConfigModule.getConfig("db.turnkey.port"))
End If
Return connStr
End Function
應該是 MYSQL的library 太舊。
可能的原因是 informacth schema 的 DB engine 換了。舊 library 會無法讀
我MySQL的Library是用本機的dll,是否有建議的版本或是dll呢?謝謝
設定&檔案如下:
https://drive.google.com/file/d/1X1xtNGUYBCTNBGd8IxsQoPMEQnn-0-7r/view?usp=share_link
按照這篇的說法
你的編碼是utf8mb3
而你的程式寫的是utf8
Dim connStr As String = "server={0};user id={1};password={2};database={3};port={4};pooling=true;charset=utf8;Convert Zero Datetime=True"
就會出現這錯誤