美好的一天/晚上! 我正在使用Mysql作为我的数据库创建一个.Net应用程序,目前我的查询遇到了问题。我允许用户上传图片,但是我在'ImageData'变量上得到了这个错误,这是一个Byte数据类型
单击按钮将其保存到数据库时,在我的控制台上运营商'&'没有为类型'Object'和'1维数组字节'
定义
。
这是关于点击事件的代码
'CREATE A SUB PROCEDURE IN SAVING THE DATA WITH THE PARAMETER TYPE OF STRING
Public Sub SAVING(ByVal sqlQuery As String)
Try
'OPENING THE CONNECTION
strcon.Open()
'INITIALIZE YOUR COMMANDS
'IT HOLDS THE DATA TO BE EXECUTED
With cmd
'PASS ON THE VALUE OF STRCON TO THE MYSQL COMMANND WHICH IS THE CONNECTION
.Connection = strcon
'THE FUNCTION OF THIS IS TO RETURN THE TEXT REPRESENTED BY A COMMAND OBJECT
.CommandText = sqlQuery
End With
'IT EXECUTES THE DATA THAT HAS TO BE SAVE IN THE DATABASE.
res = cmd.ExecuteNonQuery
'DATA WILL NOT BE SAVED IF IT EXECUTES LESS THAN 0
'BUT IF IT EXECUTES GREATER THAN 0, THE DATA WILL BE SAVED.
If res = 0 Then
MsgBox("Error in saving!", MsgBoxStyle.Exclamation)
Else
MsgBox("The data has been saved.")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
'CLOSING THE CONNECTION
strcon.Close()
End Sub
'SAVING'子程序放在一个模块中,以便它可以重复使用。
DirectCast(ImageData, Byte)
我尝试使用{{1}},但它返回
“字节1维数组”类型的值无法转换为“字节”。
我目前是VB.Net的新手,所以我对数据类型及其转换没有广泛的了解
谢谢!
答案 0 :(得分:1)
我不会深入讨论最佳实践/使用参数化查询等。您需要自己阅读这些内容。
在您发布的代码中,我会避免使用字符串连接,而是使用String.Format
,这样可以使代码更清晰,并避免出现类似于您所遇到的问题。
自己看看差异。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'DECLARE THE STRING VARIABLE THAT YOU'RE GOING TO USE.
Dim sqlQUERY As String
'FORMAT IMAGE TO BE ABLE TO SAVE TO DATABASE
Dim fs As FileStream
Dim br As BinaryReader
Try
If txtbox_image.Text.Length > 0 Then
Dim FileName As String = txtbox_image.Text
Dim ImageData() As Byte
fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
br = New BinaryReader(fs)
ImageData = br.ReadBytes(CType(fs.Length, Integer))
br.Close()
fs.Close()
'STORE YOUR QUERY TO A VARIABLE THAT YOU HAVE DECLARED.
sqlQUERY = String.Format("INSERT INTO vids.vehicles VALUES ( NULL, '{0}', '{1}', '{2}', '{3:yyyy}', '{4:yyyy-MM-dd}', (SELECT vehicle_type_id from vehicle_types where type_name='{5}'), '{6:yyyy-MM-dd}', '{7:yyyy-MM-dd}', {8}, now(), now());", _
txtbox_plateno.Text, txtbox_model.Text, txtbox_manufacturer, dtp_year.Value, dtp_dateacq.Value, DirectCast(cb_vehicletype.SelectedItem, DataRowView).Item("type_name"), dtp_reg.Value, dtp_regexpire.Value, ImageData)
'CALL THE METHOD THAT YOU HAVE CREATED AND PUT YOUR SQLQUERY IN THE PARAMETERS' LIST
SAVING(sqlQUERY)
RELOAD(Form1.dgv_vehicles)
Me.Close()
Else
MsgBox("Incomplete data!", MsgBoxStyle.Critical, "")
End If
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
答案 1 :(得分:0)
公共类表格1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim colours(9) As String
Dim totals(9) As Integer
End Sub
Private Sub populatelists(ByRef colours() As String, ByRef totals() As Integer)
For row = 0 To 9
colours = InputBox("please enter the colour of car you saw?")
totals = InputBox("please enter how many times you saw this colour")
Next
End Sub
Private Sub findmostpopular(ByVal colours() As String, ByVal totals() As Integer, ByRef highest As Integer, ByRef mostpopcolour As String)
mostpopcolour = colours(0)
highest = totals(0)
For row = 1 To 9
If totals > highest Then
highest = totals(row)
colours = colours(row)
End If
Next
End Sub
Private Sub findleastpopular(ByVal colours() As String, ByVal totals() As Integer, ByRef lowest As Integer, ByRef leastpopcolour As String)
leastpopcolour = colours(0)
lowest = totals(0)
For row = 1 To 9
If totals < lowest Then
lowest = totals(row)
colours = colours(row)
End If
Next
End Sub
Private Sub displayresults(ByVal highest As Integer, ByVal mostpopcolour As String, ByVal lowest As Integer, ByVal leastpopcolour As String)
output.Items.Add("the highest amount of cars seen were " & highest & " " & mostpopcolour & " cars")
output.Items.Add(" ")
output.Items.Add("the lowest amount of cars seen were " & lowest & " " & leastpopcolour & " cars")
End Sub
结束班级