标题:vb.net连接并操作mysql
只看楼主
wuj
Rank: 2
等 级:论坛游民
帖 子:11
专家分:14
注 册:2009-9-26
 问题点数:0 回复次数:2 
vb.net连接并操作mysql
连接并操作mysql ,最好有个实例,我是亲手,只是连接成功了,还不知道怎么操作,谢谢大家的帮助.
搜索更多相关主题的帖子: mysql 
2010-12-13 15:42
lhep041
Rank: 2
等 级:论坛游民
帖 子:7
专家分:20
注 册:2012-4-15
得分:0 
我也是个新手,一个高手给我写的一段参考,也供你参考下。

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 编辑 ]
2012-04-16 08:56
wuj
Rank: 2
等 级:论坛游民
帖 子:11
专家分:14
注 册:2009-9-26
得分:0 
谢了
2012-09-12 18:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-328863-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.065307 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved