命令执行期间遇到致命错误MySQL VB

时间:2014-03-20 01:53:57

标签: mysql asp.net vb.net

我完成了使用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

3 个答案:

答案 0 :(得分:2)

您可能忘记添加此参数@IDCR

.Parameters.AddWithValue("@IDCR", toyourvariable)

答案 1 :(得分:1)

查询中的语法错误:

[...snip...]tDesc,'@email,@change,@rea[...snip...]
                  ^---mis-placed quote.

保留字:

[...snip...]c,email,change,reasonc[...snip...]
                    ^^^^^^---- quote with backticks: `change`

答案 2 :(得分:0)

我使用的解决方案,它确实有效。 此错误主要是由MISSING或错误拼写的参数声明引起的。例如。 @FirstName错误地拼写为@FirtName。

确保在sql查询中声明的所有参数都在AddwithValue参数声明中声明。 (它有助于计算查询与Addwithvalues的比较)。

最佳解决方案是让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


希望这可以帮助。我们在编程领域分享我们的想法真是太棒了。