I have MySQL Database and VB.Net project. I have created a sub to execute any SQL statement and it's working well.
Public Sub Me_Sub_GetUpdate(ByVal SqlStr As String, ByVal xPar() As MySqlParameter)
Try
xCMD = New MySqlCommand(SqlStr, Conn)
xCMD.CommandType = CommandType.Text
If xPar IsNot Nothing Then
For i As Integer = 0 To xPar.Length - 1
xCMD.Parameters.Add(xPar(i))
Next
End If
If Conn.State = ConnectionState.Open Then Conn.Close()
Conn.Open()
xCMD.ExecuteNonQuery()
Conn.Close()
xCMD.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
when I use the next subroutine to update a record:
Try
Dim SqlStr As String
Dim xParam As MySqlParameter() = New MySqlParameter(1) {}
xParam(0) = New MySqlParameter("@ID", SqlDbType.TinyInt)
xParam(0).Value = 1
xParam(1) = New MySqlParameter("@TheName1", SqlDbType.NVarChar)
xParam(1).Value = Trim(Me.t1.Text)
SqlStr = "UPDATE tblcominfo Set TheName1=@TheName1 Where ID = @ID"
xCLS.Me_Sub_GetUpdate(SqlStr, xParam)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
I got a message (Input string was not in a correct format)!!
When I deleted the parameters and run the update code with direct values it's working!!
I don't know what's the problem, can you help me?
The problem is that you're using SqlDbType
, not MySqlDbType
. SqlDbType
is used by the SQL Server provider. Since you're using MySQL, you need to use MySqlDbType
.
The constructor you're calling is not the one you expected:
new MySqlParameter("@TheName1", SqlDbType.NVarChar)
...is calling the constructor that takes (name As String, value As Object)
because there's no overload that takes SqlDbType
. You're then overwriting the value, and the MySqlDbType
property is whatever the default is (probably MySqlDbType.Decimal
).
On the other hand, if you pass a MySqlDbType
value:
new MySqlParameter("@TheName1", MySqlDbType.VarChar)
...it will call the constructor that takes (name As String, dbType As MySqlDbType)
and the MySqlDbType
property will be initialized from it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.