从字符串(文本)转换为varbinary

时间:2017-05-07 10:25:52

标签: string vb.net casting varbinary

我将字符串转换为varbinary有问题。在这段代码中,我想在数据库中写一些数据:

Private Sub insert_Click(sender As Object, e As EventArgs) Handles insert.Click
        Dim myconnect As New SqlClient.SqlConnection
        myconnect.ConnectionString = "Data Source=(localdb)\Projects;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False"


        Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
        mycommand.Connection = myconnect
        mycommand.CommandText = "INSERT INTO Table (name, answer1, answer2, answer3, picture, voice) VALUES (@mname, @ans1, @ans2, @ans3, @pic, @sound)"
        myconnect.Open()

        Try
            mycommand.Parameters.Add("@mname", SqlDbType.VarChar).Value = TextBox1.Text
            mycommand.Parameters.Add("@ans1", SqlDbType.VarChar).Value = TextBox2.Text
            mycommand.Parameters.Add("@ans2", SqlDbType.VarChar).Value = TextBox3.Text
            mycommand.Parameters.Add("@ans3", SqlDbType.VarChar).Value = TextBox4.Text
            mycommand.Parameters.Add("@pic", SqlDbType.Image).Value = TextBox5.Text
            mycommand.Parameters.Add("@sound", SqlDbType.VarBinary).Value = TextBox6.Text
            mycommand.ExecuteNonQuery()
            MsgBox("Success")
        Catch ex As System.Data.SqlClient.SqlException
            MsgBox(ex.Message)
        End Try
        myconnect.Close()
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
    End Sub

我收到this错误。

我试图用这段代码解决问题,但我得到了同样的错误:

Dim value As String = TextBox6.Text
Dim array() As Byte = System.Text.Encoding.ASCII.GetBytes(value)

此外,此代码对我不起作用:

INSERT INTO Table (name, answer1, answer2, answer3, picture, voice) VALUES (@mname, @ans1, @ans2, @ans3, @pic, convert(varbinary(max), @sound))

知道如何解决这个问题吗?

0 个答案:

没有答案