Imports System.Data
Imports Microsoft.SqlServer.Server
Imports MySql.Data.MySqlClient
Imports System.Data.OleDb
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
'显示MYSQL数据
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cellTempValue = DBNull.Value
Dim cnn As String = "Server=localhost;User ID=root;Password=;Database=emp;"
Dim SDA = New MySqlDataAdapter("select * from emp", cnn)
Dim DT = New DataTable
SDA.Fill(DT) '将查到的数据传到DataTable中
DataGridView1.DataSource = DT '将DataTable中的数据传给DataGridView1显示
End Sub
'编辑DGV里数据同步至数据库
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Try
Dim cnn As String = "Server=localhost;User ID=root;Password=;Database=emp;"
Dim sqlConnection As New MySqlConnection(cnn)
Dim myCmd As MySqlCommand
sqlConnection.Open()
Dim strcolumn = DataGridView1.Columns(e.ColumnIndex).HeaderText '获取列标题
Dim strrow = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString() '获取焦点触发行的第一个值
Dim value = DataGridView1.CurrentCell.Value.ToString() '获取当前点击的活动单元格的值
Dim strcomm = "update emp set " + strcolumn + "='" + value + "'where empno = " + strrow
myCmd = New MySqlCommand(strcomm, sqlConnection)
myCmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
【vb.net】DataGridView 显示,编辑并同步至 MYSQL数据库
猜你喜欢
转载自blog.csdn.net/qq_21277361/article/details/81234672
今日推荐
周排行