我的项目中的 VB.NET + MYSQL + CRUD 出错
Posted
技术标签:
【中文标题】我的项目中的 VB.NET + MYSQL + CRUD 出错【英文标题】:Error in VB.NET + MYSQL + CRUD in my project 【发布时间】:2021-04-28 20:07:44 【问题描述】:您好,我正在处理一个 crud 项目,但出现错误显示一条消息:Vbmysql_SMART_CRUD.exe 中发生了“System.TypeInitializationException”类型的未处理异常
和指向此的黄色区域: sql = "SELECT auto_id, first_name, last_name, CONCAT(first_name, ' ', last_name) AS 'full_name', gender FROM tbl_smart_crud" & "WHERE CONCAT(first_name, ' ', last_name) LIKE @keyword1 OR gender = @keyword2 ORDER BY auto_id ASC"
这是完整的代码,请告诉我这里出了什么问题:
Option Explicit On
Option Strict On
Imports MySql.Data.MySqlClient
Public Class Form1
Private ID As String = ""
Private intRow As Integer = 0
Private Sub Form1_Load(sender As Object, e As
EventArgs) Handles MyBase.Load
ResetMe()
LoadData()
End Sub
Private Sub ResetMe()
Me.ID = ""
FirstNameTextBox.Text = ""
LastNameTextBox.Text = ""
If GenderComboBox.Items.Count > 0 Then
GenderComboBox.SelectedIndex = 0
End If
UpdateButton.Text = "Update()"
DeleteButton.Text = "Delete()"
KeywordTextBox.Clear()
KeywordTextBox.Select()
End Sub
Private Sub Execute(Mysql As String, Optional Parameter As String = "")
cmd = New MySqlCommand(Mysql, con)
Addparameters(Parameter)
performcrud(cmd)
End Sub
Private Sub Addparameters(str As String)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("FirstName", FirstNameTextBox.Text.Trim())
cmd.Parameters.AddWithValue("LastName", LastNameTextBox.Text.Trim())
cmd.Parameters.AddWithValue("Gender", GenderComboBox.SelectedItem.ToString())
If str = "Update" Or str = "Delete" And Not String.IsNullOrEmpty(Me.ID) Then
cmd.Parameters.AddWithValue("ID", Me.ID)
End If
End Sub
Private Sub InsertButton_Click(sender As Object, e As EventArgs) Handles InsertButton.Click
If String.IsNullOrEmpty(FirstNameTextBox.Text.Trim()) Or String.IsNullOrEmpty(LastNameTextBox.Text.Trim()) Then
MsgBox("Por Favor Escreva As Informaçoes Em Falta. ", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation,
"Nova Estrela - Dados Do Produto")
Exit Sub
End If
sql = "INSERT INTO tbl_smart_crud(first_name, last_name, gender) VALUES(@FirstName, @LastName, @Gender)"
Execute(sql, "Insert")
MsgBox("Muito Obrigado... O registro Acima Foi Salvo Com Exito! ", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
"Nova Estrela - Dados Do Produto")
LoadData()
ResetMe()
End Sub
Private Sub LoadData(Optional keyword As String = "")
sql = "SELECT auto_id, first_name, last_name, CONCAT(first_name, ' ', last_name) AS 'full_name', gender FROM tbl_smart_crud" &
"WHERE CONCAT(first_name, ' ', last_name) LIKE @keyword1 OR gender = @keyword2 ORDER BY auto_id ASC"
Dim strKeyword As String = String.Format("%0%", keyword)
cmd = New MySqlCommand(sql, con)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("keyword1", strKeyword)
cmd.Parameters.AddWithValue("keyword2", keyword)
Dim dt As DataTable = performcrud(cmd)
If dt.Rows.Count > 0 Then
intRow = Convert.ToInt32(dt.Rows.Count.ToString())
Else
intRow = 0
End If
ToolStripStatusLabel1.Text = "Numero De Registro(s): " & intRow.ToString()
With DataGridView1
.MultiSelect = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.AutoGenerateColumns = True
.DataSource = dt
.Columns(0).HeaderText = "ID"
.Columns(0).HeaderText = "FirstName"
.Columns(0).HeaderText = "LastName"
.Columns(0).HeaderText = "FullName"
.Columns(0).HeaderText = "Preco De Compra"
.Columns(0).HeaderText = "Gender"
.Columns(0).Width = 85
.Columns(0).Width = 170
.Columns(0).Width = 170
.Columns(0).Width = 220
.Columns(0).Width = 100
End With
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Try
Dim dgv As DataGridView = DataGridView1
If e.RowIndex <> -1 Then
Me.ID = Convert.ToString(dgv.CurrentRow.Cells(0).Value).Trim()
UpdateButton.Text = "Update(" & Me.ID & ")"
DeleteButton.Text = "Delete(" & Me.ID & ")"
FirstNameTextBox.Text = Convert.ToString(dgv.CurrentRow.Cells(1).Value).Trim()
LastNameTextBox.Text = Convert.ToString(dgv.CurrentRow.Cells(2).Value).Trim()
GenderComboBox.SelectedItem = Convert.ToString(dgv.CurrentRow.Cells(4).Value).Trim()
End If
Catch ex As Exception
End Try
End Sub
Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
If DataGridView1.Rows.Count = 0 Then
Exit Sub
End If
If String.IsNullOrEmpty(Me.ID) Then
MsgBox("Por Favor Escolha Um Produto ou Artigo Na Lista.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation,
"Nova Estrela - Actualizando Dados Do Produto")
Exit Sub
End If
If String.IsNullOrEmpty(FirstNameTextBox.Text.Trim()) Or String.IsNullOrEmpty(LastNameTextBox.Text.Trim()) Then
MsgBox("Por Favor Escreva As Informaçoes Em Falta. ", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation,
"Nova Estrela - Actualizando Dados Do Produto")
Exit Sub
End If
sql = "Update tbl_smart_crud Set first_name = @FirstName, last_name = @LastName, gender = @Gender WHERE auto_id = @ID"
Execute(sql, "Update")
MsgBox("O registro Acima Foi Actualizado Com Exito!", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
"Nova Estrela - Actualizando Dados Do Produto")
LoadData()
ResetMe()
End Sub
Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
If DataGridView1.Rows.Count = 0 Then
Exit Sub
End If
If String.IsNullOrEmpty(Me.ID) Then
MsgBox("Por Favor Escolha Um Produto ou Artigo Na Lista.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation,
"Nova Estrela - Apagando Dados e Registro Do Produto")
Exit Sub
End If
If MessageBox.Show("Queres Mesmo Deletar O Registro Seleccionado?", "Nova Estrela - Apagando Dados e Registro Do Produto",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
sql = "DELETE FROM add_product WHERE Codigo Do Produto = @Codigo Do Produto"
Execute(sql, "Delete")
MsgBox("O registro Foi Apagado Com Exito. ", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
"Nova Estrela - Apagando Dados e Registro Do Produto")
LoadData()
ResetMe()
End If
End Sub
Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
If Not String.IsNullOrEmpty(KeywordTextBox.Text.Trim()) Then
LoadData(KeywordTextBox.Text.Trim())
Else
LoadData()
End If
ResetMe()
End Sub
结束类
【问题讨论】:
我猜你给了我们整个程序的代码。您未能提供 performcrud 方法。无论如何,我们只需要发生错误的代码。尝试在其他表单级别变量所在的位置添加Private sql As String = ""
。很抱歉,这段代码有很多问题。
【参考方案1】:
你没有声明变量sql
在Private intRow As Integer = 0
之后写Private sql As String = ""
或者
在任何之前
sql = "something"
写var sql = ""
或var sql As String = ""
【讨论】:
我的朋友没用,它添加了 8 个错误(未声明 var,方法参数必须括在括号中)还有其他建议吗?以上是关于我的项目中的 VB.NET + MYSQL + CRUD 出错的主要内容,如果未能解决你的问题,请参考以下文章
在 sql 语法中出现错误,vb.net 中的 mysql update 语句