Imports MySql.Data.MySqlClient
Public Class manageAdminAccounts
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Dim dbDataSet As New DataTable
Dim SDA As New MySqlDataAdapter
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
manageAccounts.Show()
Me.Hide()
End Sub
Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "insert into ilycean.users(name,email,password) values ('" & txtName.Text & "', '" & txtEmail.Text & "', '" & txtPassword.Text & "')"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MessageBox.Show("Data inserted")
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
txtPassword.ResetText()
RefreshData()
READER.Close()
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
COMMAND.Dispose()
End Try
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "update ilycean.users Set name='" & txtName.Text & "', email='" & txtEmail.Text & "', password='" & txtPassword.Text & "' where id='" & txtID.Text & "' "
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MessageBox.Show("Data Updated")
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
txtPassword.ResetText()
RefreshData()
READER.Close()
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
COMMAND.Dispose()
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "Delete from ilycean.users where id='" & txtID.Text & "'"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MessageBox.Show("Data Deleted")
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
txtPassword.ResetText()
RefreshData()
READER.Close()
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
COMMAND.Dispose()
End Try
End Sub
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim SDA As New MySqlDataAdapter
Dim dbDataSet As New DataTable
Dim bSource As New BindingSource
Try
MysqlConn.Open()
Dim Query As String
Query = "select * from ilycean.users"
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA.SelectCommand = COMMAND
SDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
dataGridViewAdminAccounts.DataSource = bSource
SDA.Update(dbDataSet)
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
RefreshData()
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
Private Sub loadTable()
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "Server=localhost;Uid=root;Pwd=;Database=ilycean;"
Dim SDA As New MySqlDataAdapter
Dim bSource As New BindingSource
Try
MysqlConn.Open()
Dim Query As String
Query = "select * from ilycean.users"
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA.SelectCommand = COMMAND
SDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
dataGridViewAdminAccounts.DataSource = bSource
SDA.Update(dbDataSet)
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
Private Sub dataGridViewAdminAccounts_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataGridViewAdminAccounts.CellContentClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.dataGridViewAdminAccounts.Rows(e.RowIndex)
txtID.Text = row.Cells("id").Value.ToString
txtName.Text = row.Cells("name").Value.ToString
txtEmail.Text = row.Cells("email").Value.ToString
txtPassword.Text = row.Cells("password").Value.ToString
End If
End Sub
Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
Dim DV As New DataView(dbDataSet)
DV.RowFilter = String.Format("name Like'%{0}%'", txtSearch.Text)
dataGridViewAdminAccounts.DataSource = DV
End Sub
Private Sub manageAdminAccounts_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim dialog As DialogResult
dialog = MessageBox.Show("Do you really want to exit?", "Exit", MessageBoxButtons.YesNo)
If dialog = DialogResult.No Then
e.Cancel = True
Else
Application.ExitThread()
End If
End Sub
Private Sub manageAdminAccounts_Load(sender As Object, e As EventArgs) Handles MyBase.Load
loadTable()
txtID.ResetText()
txtName.ResetText()
txtEmail.ResetText()
txtPassword.ResetText()
txtPassword.ResetText()
End Sub
Public Sub RefreshData()
Dim Query As String
Query = "select * from ilycean.users"
dbDataSet.Clear()
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA.SelectCommand = COMMAND
SDA.Fill(dbDataSet)
dataGridViewAdminAccounts.DataSource = dbDataSet
End Sub
End Class
每次Insert
,Update
或Delete
时,都会出现以下错误:
已经有一个与此命令关联的打开DataReader,必须先关闭它。
我已经尝试在我的连接字符串上添加MultipleActiveResultSets=True;
,但它仍无效。
答案 0 :(得分:0)
在ExecuteReader
中使用CommandBehavior.CloseConnection
如
MySqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
同样使用using
构造适用于非连接资源,如SQL连接
Using resource As New resourceType
' Insert code to work with resource.
End Using