如何编写更新代码vb.net和sql极客

时间:2014-04-19 00:53:19

标签: mysql vb.net

我使用vb.net为mysql服务器编写了这个更新代码,但它无法正常工作。

Dim sqlstring = "Update initial_nom set f_name = '" & 
  TextBox1.Text & "',s_name = '" &     
  TextBox2.Text & "',th_name = '" &
  TextBox3.Text & "',fo_name = '" &
  TextBox4 & "',adm_type  = '" & 
  ComboBox4.SelectedItem.ToString() & "' 
    where (app_no = " & TextBox5.Text & ")"

cmd.ExecuteNonQuery()

1 个答案:

答案 0 :(得分:0)

试试这个:

Dim connectionString As String = "Server=my_server;Database=my_db;Uid=my_username;Pwd=my_password;"
Dim SQLConnection As New MySqlConnection(connectionString)
Dim SQLCommand As New MySqlCommand()
With SQLCommand
    .CommandText = "UPDATE initial_nom SET f_name =  @f_name, s_name = @s_name, th_name = @th_name, fo_name = @fo_name, adm_type = @adm_type WHERE app_no = @app_no"
    .Connection = SQLConnection
    .Parameters.AddWithValue("@f_name", TextBox1.Text)
    .Parameters.AddWithValue("@s_name", TextBox2.Text)
    .Parameters.AddWithValue("@th_name", TextBox3.Text)
    .Parameters.AddWithValue("@fo_name", TextBox4.Text)
    .Parameters.AddWithValue("@app_no", TextBox5.Text)
    .Parameters.AddWithValue("@adm_type", ComboBox4.SelectedItem.ToString)
End With
Try
    SQLConnection.Open()
    SQLCommand.ExecuteNonQuery()
Catch ex As MySqlException
    MsgBox(ex.Message.ToString)
Finally
    SQLConnection.Close()
End Try

不要忘记在连接字符串中输入您自己的详细信息。

在回答您的其他closed问题时,这应该是您想要的:

Dim connectionString As String = "Server=my_server;Database=my_db;Uid=my_username;Pwd=my_password;"
Dim SQLConnection As New MySqlConnection(connectionString)
Dim SQLCommand As New MySqlCommand()
With SQLCommand
    .CommandText = "INSERT INTO nomination (f_name, s_name, th_name, fo_name, app_no, adm_type) values (@f_name, @s_name, @th_name, @fo_name, @app_no, @adm_type)"
    .Connection = SQLConnection
    .Parameters.AddWithValue("@f_name", TextBox1.Text)
    .Parameters.AddWithValue("@s_name", TextBox2.Text)
    .Parameters.AddWithValue("@th_name", TextBox3.Text)
    .Parameters.AddWithValue("@fo_name", TextBox4.Text)
    .Parameters.AddWithValue("@app_no", TextBox5.Text)
    .Parameters.AddWithValue("@adm_type", ComboBox4.SelectedItem.ToString)
End With
Try
    SQLConnection.Open()
    SQLCommand.ExecuteNonQuery()
Catch ex As MySqlException
    MsgBox(ex.Message.ToString)
Finally
    SQLConnection.Close()
End Try

更新

我在你的另一个问题中注意到你正在努力使用ADODB和连接DSN,使用MySQL Connector可以更容易地做到这一点。

http://dev.mysql.com/downloads/connector/net/

下载并安装MySQL Connector

您现在需要在项目中添加对此的引用:

Add reference

选择浏览并导航到MySQL Connector的 Assemblies 目录:

Path

选择 MySQL.Data.dll ,然后单击确定:

enter image description here

现在您需要做的就是将Imports MySql.Data.MySqlClient添加到表单代码的最顶层,一旦创建了正确的连接字符串,您就可以访问数据库。

以下是一些示例代码,它将为您提供有关如何在数据库上执行某些简单操作的信息,这些代码已经过测试和工作(今天早上我很无聊!)。

Imports MySql.Data.MySqlClient

Public Class Form1
    Dim connectionString As String = "Server=my_server;Database=my_db;Uid=my_username;Pwd=my_password;"
    Dim SQLConnection As New MySqlConnection(connectionString)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 5
            ComboBox4.Items.Add(i)
        Next
        ComboBox4.SelectedIndex = 0
    End Sub

    Private Sub update_row()
        Dim SQLCommand As New MySqlCommand()
        With SQLCommand
            .CommandText = "UPDATE nomination SET f_name =  @f_name, s_name = @s_name, th_name = @th_name, fo_name = @fo_name, adm_type = @adm_type WHERE app_no = @app_no"
            .Connection = SQLConnection
            .Parameters.AddWithValue("@f_name", TextBox1.Text)
            .Parameters.AddWithValue("@s_name", TextBox2.Text)
            .Parameters.AddWithValue("@th_name", TextBox3.Text)
            .Parameters.AddWithValue("@fo_name", TextBox4.Text)
            .Parameters.AddWithValue("@app_no", TextBox5.Text)
            .Parameters.AddWithValue("@adm_type", ComboBox4.SelectedItem.ToString)
        End With
        Try
            SQLConnection.Open()
            SQLCommand.ExecuteNonQuery()
        Catch ex As MySqlException
            MsgBox(ex.Message.ToString)
        Finally
            SQLConnection.Close()
        End Try
    End Sub

    Private Sub insert_row()
        Dim SQLCommand As New MySqlCommand()
        With SQLCommand
            .CommandText = "INSERT INTO nomination (f_name, s_name, th_name, fo_name, app_no, adm_type) values (@f_name, @s_name, @th_name, @fo_name, @app_no, @adm_type)"
            .Connection = SQLConnection
            .Parameters.AddWithValue("@f_name", TextBox1.Text)
            .Parameters.AddWithValue("@s_name", TextBox2.Text)
            .Parameters.AddWithValue("@th_name", TextBox3.Text)
            .Parameters.AddWithValue("@fo_name", TextBox4.Text)
            .Parameters.AddWithValue("@app_no", TextBox5.Text)
            .Parameters.AddWithValue("@adm_type", ComboBox4.SelectedItem.ToString)
        End With
        Try
            SQLConnection.Open()
            SQLCommand.ExecuteNonQuery()
        Catch ex As MySqlException
            MsgBox(ex.Message.ToString)
        Finally
            SQLConnection.Close()
        End Try
    End Sub

    Private Sub select_from()
        Dim SQLCommand As New MySqlCommand
        Dim SQLAdapter As New MySqlDataAdapter
        Dim DTable As New DataTable
        With SQLCommand
            .CommandText = "SELECT * FROM nomination"
            .Connection = SQLConnection
        End With
        Try
            SQLConnection.Open()
            SQLAdapter.SelectCommand = SQLCommand
            SQLAdapter.Fill(DTable)
            DataGridView1.DataSource = DTable
        Catch ex As MySqlException
            MsgBox(ex.Message.ToString)
        Finally
            SQLConnection.Close()
        End Try
    End Sub

    Private Sub create_table()
        Dim SQLCommand As New MySqlCommand
        With SQLCommand
            .CommandText = "CREATE TABLE nomination (f_name varchar(50),s_name varchar(50),th_name varchar(50),fo_name varchar(50),app_no varchar(50),adm_type varchar(50))"
            .Connection = SQLConnection
        End With
        Try
            SQLConnection.Open()
            SQLCommand.ExecuteNonQuery()
        Catch ex As MySqlException
            MsgBox(ex.Message.ToString)
        Finally
            SQLConnection.Close()
        End Try
    End Sub

    Private Sub drop_table()
        If MsgBox("Are you really sure you want to DROP this table?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Dim SQLCommand As New MySqlCommand
            With SQLCommand
                .CommandText = "DROP TABLE nomination"
                .Connection = SQLConnection
            End With
            Try
                SQLConnection.Open()
                SQLCommand.ExecuteNonQuery()
            Catch ex As MySqlException
                MsgBox(ex.Message.ToString)
            Finally
                SQLConnection.Close()
            End Try
        End If
    End Sub

    Private Sub truncate_table()
        If MsgBox("Are you really sure you want to TRUNCATE this table?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Dim SQLCommand As New MySqlCommand
            With SQLCommand
                .CommandText = "TRUNCATE TABLE nomination"
                .Connection = SQLConnection
            End With
            Try
                SQLConnection.Open()
                SQLCommand.ExecuteNonQuery()
            Catch ex As MySqlException
                MsgBox(ex.Message.ToString)
            Finally
                SQLConnection.Close()
            End Try
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        create_table()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        drop_table()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        insert_row()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        update_row()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        select_from()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        truncate_table()
    End Sub
End Class

这是我使用的表单,它需要Button1 - Button6,TextBox1 - TextBox5,ComboBox4和DataGridView控件。

Form1

希望这可以帮助你。