Imports MySql.Data.MySqlClient
Imports Newtonsoft.Json
Imports System.IO
Module Module1
Sub Main()
'
Dim config As String = File.ReadAllText("config.json")
Dim configData As ConfigData = JsonConvert.DeserializeObject(Of ConfigData)(config)
Dim connStr As String = $"serve-r={configData.server};user={configData.user};password={configData.password};database={configData.database}"
Using conn As New MySqlConnection(connStr)
conn.Open()
Dim backupSql As String = "BACKUP DATABASE your_database TO DISK = 'AA.sql'"
Using cmd As New MySqlCommand(backupSql, conn)
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
Public Class ConfigData
Public Property server As String
Public Property user As String
Public Property password As String
Public Property database As String
End Class
End Module
全局代码
Public Class ConfigData
Public Property server As String
Public Property user As String
Public Property password As String
Public Property database As String
End Class
按钮
Dim config As String = Filesys.ReadAllText("config.json")
Dim configData As ConfigData = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ConfigData)(config)
Dim connStr As String = cexp("serve-r={0};user={1};password={2};database={3}",configData.server,configData.user,configData.password,configData.database)
Using conn As New MySql.Data.MySqlClient.MySqlConnection(connStr)
conn.Open()
Dim backupSql As String = "BACKUP DATABASE your_database TO DISK = 'AA.sql'"
Using cmd As New MySql.Data.MySqlClient.MySqlCommand(backupSql, conn)
cmd.ExecuteNonQuery()
End Using
End Using
按键代码
Dim config As String = Filesys.ReadAllText("config.json")
Dim configData As ConfigData = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ConfigData)(config)
Dim connStr As String = cexp("serve-r={0};user={1};password={2};database={3}", configData.server, configData.user, configData.password, configData.database)
Using conn As New MySql.Data.MySqlClient.MySqlConnection(connStr)
conn.Open()
Dim backupSql As String = "BACKUP DATABASE your_database TO DISK = 'D:\BF\t03-2024-9-27.sql'"
Using cmd As New MySql.Data.MySqlClient.MySqlCommand(backupSql, conn)
cmd.ExecuteNonQuery()
End Using
End Using
报错:
Authentication to host '' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''@'fe80::3bb8:b5f1:fce2:6b0b%5' (using password: NO)
说明:config.json是MYSQL加密的连配置
问题一:上面报错是不是config.json有加密,这里没解密?还是没有指定config.json的文件指定的路径?如何解决?
问题二:上面如何加下面异常代码,进行异常处理?
Try
' proc.Start()
' proc.StandardInput.Flush()
' proc.StandardInput.Close()
' ' 等待进程执行完成
' proc.WaitForExit()
' ' 检查退出代码以确认是否成功
' If proc.ExitCode <> 0 Then
' MessageBox.Show("备份失败,请检查错误输出。")
' Else
' MessageBox.Show("备份成功!")
' End If
' Catch ex As Exception
' MessageBox.Show("备份过程中发生错误: " & ex.Message)
' End Try
[此贴子已经被作者于2024/9/27 13:30:22编辑过]
1、1楼提供的代码里没有解密的用法
[此贴子已经被作者于2024/9/27 13:42:23编辑过]
1、1楼提供的代码里没有解密的用法报错:
Authentication to host '' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''@'fe80::3bb8:b5f1:fce2:6b0b%5' (using password: NO)
说明:config.json是MYSQL加密的连配置与不加密的连配置都一样上面报错,如何解决?
1.调试:
Dim connStr As String = cexp("serve-r={0};user={1};password={2};database={3}", configData.server, configData.user, configData.password, configData.database)
msgbox(connStr) '调试用
Using conn As New MySql.Data.MySqlClient.MySqlConnection(connStr)
msgbox(3) '调试用
conn.Open()
msgbox(4) '调试用
报错:
Authentication to host '' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''@'fe80::3bb8:b5f1:fce2:6b0b%5' (using password: NO)
2.
2.1config.json无加密
{
"dbSettings": {
"defaultConnection": "ts",//
"connectionStrings": [
{
"name": "ts",
"type": "mysql",
"connectionString": "serve-r=127.0.0.1;user id=MYSQL;password=123456;database=ts;sslmode=none"
},
}
}
}
2.1config.json加密
2.2
{
"dbSettings": {
"defaultConnection": "ts",//
"connectionStrings": [
{
"name": "ts",
"type": "mysql",
"connectionString": "serve-r=加密;sslmode=none"
},
}
}
}