vb.net mysql 单击查询 运行
vb.net mysql query running in single click
我需要从保存在计算机中的记事本文件中获取 mysql 登录数据,连接到服务器并 运行 查询,只需单击一下即可。
我有这段代码,但问题是有一个 "public Sub Savenames(ByRef sqlstatement As String)" 部分,我不知道如何将它包含到按钮点击事件中。
Imports MySql.Data.MySqlClient
Public Class Admission
Private Sub MetroButton1_Click(sender As Object, e As EventArgs) Handles admission_btn_save.Click
If System.IO.File.Exists("D:\DBSystem\Connection\connconfig.dbs") = True Then
Dim strFile As String = "D:\DBSystem\Connection\connconfig.dbs"
Dim sr As New IO.StreamReader(strFile)
Dim ip As String
Dim port As String
Dim userid As String
Dim password As String
Dim database As String
'reading
ip = sr.ReadLine()
port = sr.ReadLine()
userid = sr.ReadLine()
password = sr.ReadLine()
database = sr.ReadLine()
sr.Close()
'default connection code
Dim serverstring As String = "server=" & ip & ";port=" & port & ";database=" & database & ";user ID=" & userid & ";password=" & password & ";OldGuids=true"
Dim sqlconnection As MySqlConnection = New MySqlConnection
sqlconnection.ConnectionString = serverstring
Try
If sqlconnection.State = ConnectionState.Closed Then
sqlconnection.Open()
MsgBox("Successfully connected to database!")
Else
sqlconnection.Close()
MsgBox("connection closed!")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'reading from notepad config file
Else
MessageBox.Show("Do not have any previous configurations!", "Error",
MessageBoxButtons.OK)
End If
End Sub
'i need to eliminate this sub and add it into the button click event
Public Sub Savenames(ByRef sqlstatement As String)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = sqlstatement
.CommandType = CommandType.Text
.Connection = sqlconnection
.ExecuteNonQuery()
End With
sqlconnection.Close()
MsgBox("Successfully recorded!")
sqlconnection.Dispose()
End Sub
End Class
我需要删除 "Public Sub Savenames(ByRef sqlstatement As String)" 部分并将其添加到按钮单击事件中。我没有那么多编程知识,因为我是自学的。知道怎么做吗? (请告诉我修改后的代码)
简单地将子任务添加到您正在检查的部分 Connection.Since 连接已经打开,无需再次打开它..另外不要忘记提供查询语句..(我想插入 )
Private Sub MetroButton1_Click(sender As Object, e As EventArgs) Handles admission_btn_save.Click
If System.IO.File.Exists("D:\DBSystem\Connection\connconfig.dbs") = True Then
Dim strFile As String = "D:\DBSystem\Connection\connconfig.dbs"
Dim sr As New IO.StreamReader(strFile)
Dim ip As String
Dim port As String
Dim userid As String
Dim password As String
Dim database As String
'reading
ip = sr.ReadLine()
port = sr.ReadLine()
userid = sr.ReadLine()
password = sr.ReadLine()
database = sr.ReadLine()
sr.Close()
'default connection code
Dim serverstring As String = "server=" & ip & ";port=" & port & ";database=" & database & ";user ID=" & userid & ";password=" & password & ";OldGuids=true"
Dim sqlconnection As MySqlConnection = New MySqlConnection
sqlconnection.ConnectionString = serverstring
Try
If sqlconnection.State = ConnectionState.Closed Then
sqlconnection.Open()
MsgBox("Successfully connected to database!")
Dim sqlstatement As String = "Insert int >>>Your Query"
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = sqlstatement
.CommandType = CommandType.Text
.Connection = sqlconnection
.ExecuteNonQuery()
End With
sqlconnection.Close()
MsgBox("Successfully recorded!")
Else
sqlconnection.Close()
MsgBox("connection closed!")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'reading from notepad config file
Else
MessageBox.Show("Do not have any previous configurations!", "Error",
MessageBoxButtons.OK)
End If
End Sub
我需要从保存在计算机中的记事本文件中获取 mysql 登录数据,连接到服务器并 运行 查询,只需单击一下即可。
我有这段代码,但问题是有一个 "public Sub Savenames(ByRef sqlstatement As String)" 部分,我不知道如何将它包含到按钮点击事件中。
Imports MySql.Data.MySqlClient Public Class Admission Private Sub MetroButton1_Click(sender As Object, e As EventArgs) Handles admission_btn_save.Click If System.IO.File.Exists("D:\DBSystem\Connection\connconfig.dbs") = True Then Dim strFile As String = "D:\DBSystem\Connection\connconfig.dbs" Dim sr As New IO.StreamReader(strFile) Dim ip As String Dim port As String Dim userid As String Dim password As String Dim database As String 'reading ip = sr.ReadLine() port = sr.ReadLine() userid = sr.ReadLine() password = sr.ReadLine() database = sr.ReadLine() sr.Close() 'default connection code Dim serverstring As String = "server=" & ip & ";port=" & port & ";database=" & database & ";user ID=" & userid & ";password=" & password & ";OldGuids=true" Dim sqlconnection As MySqlConnection = New MySqlConnection sqlconnection.ConnectionString = serverstring Try If sqlconnection.State = ConnectionState.Closed Then sqlconnection.Open() MsgBox("Successfully connected to database!") Else sqlconnection.Close() MsgBox("connection closed!") End If Catch ex As Exception MsgBox(ex.ToString) End Try 'reading from notepad config file Else MessageBox.Show("Do not have any previous configurations!", "Error", MessageBoxButtons.OK) End If End Sub 'i need to eliminate this sub and add it into the button click event Public Sub Savenames(ByRef sqlstatement As String) Dim cmd As MySqlCommand = New MySqlCommand With cmd .CommandText = sqlstatement .CommandType = CommandType.Text .Connection = sqlconnection .ExecuteNonQuery() End With sqlconnection.Close() MsgBox("Successfully recorded!") sqlconnection.Dispose() End Sub End Class
我需要删除 "Public Sub Savenames(ByRef sqlstatement As String)" 部分并将其添加到按钮单击事件中。我没有那么多编程知识,因为我是自学的。知道怎么做吗? (请告诉我修改后的代码)
简单地将子任务添加到您正在检查的部分 Connection.Since 连接已经打开,无需再次打开它..另外不要忘记提供查询语句..(我想插入 )
Private Sub MetroButton1_Click(sender As Object, e As EventArgs) Handles admission_btn_save.Click
If System.IO.File.Exists("D:\DBSystem\Connection\connconfig.dbs") = True Then
Dim strFile As String = "D:\DBSystem\Connection\connconfig.dbs"
Dim sr As New IO.StreamReader(strFile)
Dim ip As String
Dim port As String
Dim userid As String
Dim password As String
Dim database As String
'reading
ip = sr.ReadLine()
port = sr.ReadLine()
userid = sr.ReadLine()
password = sr.ReadLine()
database = sr.ReadLine()
sr.Close()
'default connection code
Dim serverstring As String = "server=" & ip & ";port=" & port & ";database=" & database & ";user ID=" & userid & ";password=" & password & ";OldGuids=true"
Dim sqlconnection As MySqlConnection = New MySqlConnection
sqlconnection.ConnectionString = serverstring
Try
If sqlconnection.State = ConnectionState.Closed Then
sqlconnection.Open()
MsgBox("Successfully connected to database!")
Dim sqlstatement As String = "Insert int >>>Your Query"
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = sqlstatement
.CommandType = CommandType.Text
.Connection = sqlconnection
.ExecuteNonQuery()
End With
sqlconnection.Close()
MsgBox("Successfully recorded!")
Else
sqlconnection.Close()
MsgBox("connection closed!")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'reading from notepad config file
Else
MessageBox.Show("Do not have any previous configurations!", "Error",
MessageBoxButtons.OK)
End If
End Sub