使用vb.net在SQL Server数据库中保存文档文件

时间:2018-04-02 15:17:35

标签: sql-server vb.net

我需要你的帮助。我试图在SQL Server数据库中保存文件,但它显示此错误

  

不允许从数据类型varchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。

这是我的代码

  Private Sub Button70_Click(sender As Object, e As EventArgs) Handles Button70.Click
    fna = content.Text
    fna.Split("\")
    ffn = fna.Last.ToString

    conn.Open()
    Dim com As New SqlCommand
    com.Connection = conn
    com.CommandText = "insert into meeting (Date_Entry, Title, Chairperson, Venue, File_Name, [Content]) values( '" & date5.Text & "', '" & title.Text & "', '" & cp.Text & "', '" & vn.Text & "', '" & f_name.Text & "', '@[Content]')"
    With com.Parameters

        .AddWithValue("Content", SqlDbType.VarBinary).Value = File.ReadAllBytes(content.Text)
    End With
    com.ExecuteNonQuery()
    conn.Close()
    MsgBox("File saved")
    content.Clear()


End Sub

Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click
    '// open dilogue to add file to save
    If op.ShowDialog = Windows.Forms.DialogResult.OK Then
        content.Text = op.FileName

    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您应该在查询字符串中使用@Content而不是@[Content]