我也是个新手,一个高手给我写的一段参考,也供你参考下。
Imports MySql.Data.MySqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim m_strConnection As String = "server=192.9.200.59;uid=root;pwd=XXX;database=redboardppe; pooling=false"
Dim m_SqlConn As MySqlConnection = New MySqlConnection(m_strConnection)
Dim SQL As String
SQL = "SELECT * FROM newtable"
Dim daAdapter As MySqlDataAdapter = New MySqlDataAdapter(SQL, m_SqlConn)
Dim myCmd As MySqlCommand
myCmd = New MySqlCommand(SQL, m_SqlConn)
daAdapter.SelectCommand = myCmd
Dim myCbd As MySqlCommandBuilder
myCbd = New MySqlCommandBuilder(daAdapter)
Dim mySet As New DataSet()
daAdapter.TableMappings.Add("newtable", "newtable")
daAdapter.TableMappings(0).ColumnMappings.Add("id", "id")
daAdapter.TableMappings(0).ColumnMappings.Add("name", "name")
'Dim dsTable As DataTable = New DataTable("newtable")
daAdapter.Fill(mySet)
Dim dr As DataRow
dr = mySet.Tables(0).NewRow()
dr("name") = Me.TextBox2.Text '' System.Guid.NewGuid().ToString()
mySet.Tables(0).Rows.Add(dr)
daAdapter.Update(mySet)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim m_strConnection As String = "server=192.9.200.59;uid=root;pwd=XXX;database=redboardppe; pooling=false"
Dim m_SqlConn As MySqlConnection = New MySqlConnection(m_strConnection)
Dim SQL As String
SQL = "SELECT * FROM newtable"
Dim daAdapter As MySqlDataAdapter = New MySqlDataAdapter(SQL, m_SqlConn)
Dim myCmd As MySqlCommand
myCmd = New MySqlCommand(SQL, m_SqlConn)
daAdapter.SelectCommand = myCmd
Dim myCbd As MySqlCommandBuilder
myCbd = New MySqlCommandBuilder(daAdapter)
Dim mySet As New DataSet()
daAdapter.TableMappings.Add("newtable", "newtable")
daAdapter.TableMappings(0).ColumnMappings.Add("id", "id")
daAdapter.TableMappings(0).ColumnMappings.Add("name", "name")
'Dim dsTable As DataTable = New DataTable("newtable")
daAdapter.Fill(mySet)
For Each dr As DataRow In mySet.Tables(0).Rows
dr("name") = Me.TextBox1.Text
Next
daAdapter.Update(mySet)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim m_strConnection As String = "server=192.9.200.59;uid=root;pwd=XXX;database=redboardppe; pooling=false"
Dim m_SqlConn As MySqlConnection = New MySqlConnection(m_strConnection)
Dim SQL As String
SQL = "delete FROM newtable"
Dim daAdapter As MySqlDataAdapter = New MySqlDataAdapter(SQL, m_SqlConn)
Dim myCmd As MySqlCommand
myCmd = New MySqlCommand(SQL, m_SqlConn)
daAdapter.SelectCommand = myCmd
m_SqlConn.Open()
myCmd.ExecuteNonQuery()
m_SqlConn.Close()
End Sub
End Class
[
本帖最后由 lhep041 于 2012-4-16 09:00 编辑 ]