vb.net 命令执行过程中遇到致命错误 MySQL VB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22521908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal Error Encounter During Command Execution MySQL VB
提问by kolapopo
ijust finish my code for inserting data using the vb and mySQL but when i run my webpage it seem have an error Fatal Error Encounter During Command Execution . Please help some how to solve it. below is my code.
我刚刚完成了使用 vb 和 mySQL 插入数据的代码,但是当我运行我的网页时,它似乎在命令执行期间遇到致命错误。请帮助一些如何解决它。下面是我的代码。
Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient
Partial Class Request
Inherits System.Web.UI.Page
Dim MessageBox As Object
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
txt1.Focus()
txt2.Focus()
txt3.Focus()
txt4.Focus()
txt5.Focus()
txt6.Focus()
txt7.Focus()
ddl1.Focus()
ddl2.Focus()
ddl3.Focus()
ddl4.Focus()
End Sub
Protected Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
'Create sql connection and fetch data from database based on employee id
Dim conn As New MySql.Data.MySqlClient.MySqlConnection
Dim strConnectionString As String = ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString
Try
conn.ConnectionString = strConnectionString
conn.Open()
Catch ex As MySql.Data.MySqlClient.MySqlException
MessageBox.Show(ex.Message)
End Try
' Dim cr_id As String
' cr_id = "CR004"
Dim iReturn As Boolean
Using SQLConnection As New MySqlConnection(strConnectionString)
Using sqlCommand As New MySqlCommand()
sqlCommand.Connection = SQLConnection
With sqlCommand
.CommandText = "INSERT INTO cr_record(idcr_record,Emplid,Nama,date,DeptDesc,email,change,reasonchange,problem,priority,reasondescription,systemrequest) VALUES (@IDCR,@Emplid,@Nama,@date,@DeptDesc,'@email,@change,@reasonchange,@problem,@priority,@reasondescription,@systemrequest)"
' .CommandTimeout = 5000000
.CommandType = Data.CommandType.Text
.Parameters.AddWithValue("@Emplid", txt1.Text)
.Parameters.AddWithValue("@Nama", TextBox1.Text)
.Parameters.AddWithValue("@date", txt5.Text)
.Parameters.AddWithValue("@DeptDesc", txt2.Text)
.Parameters.AddWithValue("@email", txt4.Text)
.Parameters.AddWithValue("@change", ddl2.Text)
.Parameters.AddWithValue("@reasonchange", txt6.Text)
.Parameters.AddWithValue("@problem", ddl3.Text)
.Parameters.AddWithValue("@priority", rbl1.Text)
.Parameters.AddWithValue("@reasondescription", txt7.Text)
.Parameters.AddWithValue("@systemrequest", ddl4.Text)
End With
Try
SQLConnection.Open()
' sqlCommand.ExecuteNonQuery()
sqlCommand.ExecuteNonQuery()
iReturn = True
MsgBox("Added Successfully")
Catch ex As MySqlException
MsgBox(ex.Message.ToString & Err.Description)
iReturn = False
Finally
SQLConnection.Close()
End Try
End Using
End Using
Return
End Sub
End Class
回答by Codemunkeee
you probably forgot to add this parameter @IDCR
您可能忘记添加此参数 @IDCR
.Parameters.AddWithValue("@IDCR", toyourvariable)
回答by Marc B
Syntax error in your query:
查询中的语法错误:
[...snip...]tDesc,'@email,@change,@rea[...snip...]
^---mis-placed quote.
Reserved words:
保留字:
[...snip...]c,email,change,reasonc[...snip...]
^^^^^^---- quote with backticks: `change`
回答by Momodu Deen Swarray
Solution that i used and it really works.This error is mostly caused by a MISSING or Incorrectly Spelled Parameter declaration. eg. @FirstName mistakenly spelled for @FirtName.
我使用的解决方案,它确实有效。此错误主要是由 MISSING 或拼写错误的参数声明引起的。例如。@FirstName 错误地拼写为 @FirtName。
Make sure that all the parameters that are declared in the sql query are all declared in the AddwithValue Parameter declaration. (It helps to count the query versus the Addwithvalues).
确保在 sql 查询中声明的所有参数都在 AddwithValue 参数声明中声明。(它有助于计算查询与 Addwithvalues)。
The best solution is for visual studio to provide information about the missing Parameter. Use a Try-Catch block. In the catch block use Messagebox.show(ex.Innerexception.Message) instead of Messagebox.show(ex.message). This will show the exactParameter that is missing. eg. below
最好的解决方案是让 Visual Studio 提供有关缺失参数的信息。使用 Try-Catch 块。在 catch 块中使用 Messagebox.show(ex.Innerexception.Message) 而不是 Messagebox.show(ex.message)。这将显示缺少的确切参数。例如。以下
Try
conCommand.Parameters.Addwithvalue("@FirstName", txtFirstName.text)
conCommand.Parameters.Addwithvalue("@MiddleName", txtMiddleName.text)
conCommand.Parameters.Addwithvalue("@LastName", txtLastName.text)
conCommand.Parameters.Addwithvalue("@PhoneNo", txtPhoneno.text)
catch ex as exception
Messagebox.show(ex.innerexception.Message)
End Try
Hope this helps. Its really great that we share our ideas in the world of programming.
希望这可以帮助。我们在编程世界中分享我们的想法真的很棒。

