使用MySql作为后端在VB 2010上更新/编辑/删除记录 [英] Updating/editing/deleting records on VB 2010 with MySql as back end

查看:91
本文介绍了使用MySql作为后端在VB 2010上更新/编辑/删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VB2010上进行此练习,这是一个登录,另一个是添加/编辑/删除项目.
我已经对连接进行了编码,并以某种方式成功完成了连接,现在,即使得到确认已添加记录的确认,我也无法更新数据库.

我不知道错误在哪里.
到目前为止,我所拥有的只是到目前为止的该代码块(添加/编辑/删除):


I''m doing this exercise on VB2010, it''s a log-in and the other is an add/edit/delete project.
I''ve coded the connection and been successful at it somehow, now, the thing is I can''t update the database even if I get the confirmation that the record has been added.

I have no idea where the error is.
All I have for now is this block of code for the registration (add/edit/delete) so far:


Imports MySql.Data.MySqlClient
Public Class register
    Private Sub register_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        firstnametb.Focus()
        Try
            If conn.State = ConnectionState.Closed Then
                Dim connection As New MySqlConnection("Server = localhost; User Id = root; Password = root; Database = add_edit_delete")
                connection.Open()
            End If
        Catch ex As Exception
            MsgBox("Database Error!", MsgBoxStyle.Information, "System Message")
            Application.Exit()
        End Try
    End Sub
 
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clrbtn.Click
        lastnametb.Text = ""
        firstnametb.Text = ""
        middletb.Text = ""
        agetb.Text = ""
        loctb.Text = ""
        unametb.Text = ""
        pwordtb.Text = ""
        cpwordtb.Text = ""
    End Sub
    Private Sub addbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbtn.Click
        If lastnametb.Text = "" Or firstnametb.Text = "" Or agetb.Text = "" Or loctb.Text = "" Or unametb.Text = "" Or pwordtb.Text = "" Or cpwordtb.Text = "" Then
            MsgBox("Please complete the fields!", MsgBoxStyle.Exclamation, "System Message")
        Else
            If conn.State = ConnectionState.Closed Then
                Dim connection As New MySqlConnection("Server = localhost; User Id = root; Password = root; Database = add_edit_delete")
                connection.Open()
            End If
            Try
                strSql = "INSERT INTO register(LastName, FirstName, MiddleInitial, Age, Location, Uname, Pword) VALUES(''" & lastnametb.Text & "'',''" & firstnametb.Text & "'',''" & middletb.Text & "'',''" & agetb.Text & "'',''" & loctb.Text & "'',''" & unametb.Text & "'',''" & pwordtb.Text & "'')"
                cmd.CommandText = strSql
                cmd.Connection = conn
                cmd.ExecuteNonQuery()
                MsgBox("Record has been saved!", MsgBoxStyle.Information, "System Message")
                conn.Close()
                Me.Dispose()
            Catch ex As Exception
                MsgBox("Unable to save record!", MsgBoxStyle.Critical, "System Message")
                conn.Close()
            End Try
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Dispose()
    End Sub
End Class



希望有人可以帮助我. :) 谢谢!上帝保佑!



Hope someone could help me. :) THANKS! God bless!

推荐答案

这段代码是一团糟.我知道您只是在学习,但是如果做得不好,您将如何学习?没有SQL代码的集中化,每个方法中都有一个连接字符串,容易受到SQL注入攻击...

第一步是调试并弄清楚代码为SQL字符串创建的内容.然后针对您的数据库手动运行该错误,并查看数据库报告的错误.
This code is a mess. I know you''re just learning, but how will you learn if you do things badly ? No centralisation of SQL code, a connection string in each methods, open to SQL injection attacks...

The first step is to debug and work out what your code is creating for a string of SQL. Then run that against your DB manually and see what errors the DB reports.


这篇关于使用MySql作为后端在VB 2010上更新/编辑/删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆