简体   繁体   English

使用 VB.Net 和 SQL Server 上传图片

[英]Upload Image using VB.Net and SQL Server

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim command As New SqlCommand("insert into rent(Image,Status)values(@Image,@Status)", connection)
    Dim ms As New MemoryStream
    PictureBox1.Image.Save("ms", PictureBox1.Image.RawFormat)

    command.Parameters.Add("@Image", SqlDbType.VarChar).Value = ms.ToArray
    command.Parameters.Add("@Status", SqlDbType.VarChar).Value = TextBox5.Text

    connection.Open()

    If command.ExecuteNonQuery = 1 Then
        MessageBox.Show("Successfully uploaded")
    Else
        MessageBox.Show("Not uploaded")

    End If
    connection.Close()

End Sub

I'm trying to upload an image into my SQL Server using Visual Studio;我正在尝试使用 Visual Studio 将图像上传到我的 SQL Server; everything is working except when I click the upload button, I keep getting the following error:一切正常,除了当我单击上传按钮时,我不断收到以下错误:

在此处输入图片说明

I tried every possible solution and no luck, I tried enabling the tcp and changing the ip even in SQL Server.我尝试了所有可能的解决方案,但没有运气,我尝试启用 tcp 并更改 ip,即使在 SQL Server 中也是如此。

The error you get means that you can't connect to SQL Server.您收到的错误意味着您无法连接到 SQL Server。 Make sure your connection string is correct, and you don't have a firewall blocking the connection between the computer that runs the code and the computer that hosts SQL Server.确保您的连接字符串正确,并且您没有防火墙阻止运行代码的计算机与托管 SQL Server 的计算机之间的连接。

However, once you sort the connection error, you still have a few problems with your code.但是,一旦对连接错误进行排序,您的代码仍然存在一些问题。

  • change PictureBox1.Image.Save("ms", PictureBox1.Image.RawFormat) to PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat) to save the image into the memory stream.PictureBox1.Image.Save("ms", PictureBox1.Image.RawFormat)更改为PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)以将图像保存到内存流中。

  • Change command.Parameters.Add("@Image", SqlDbType.VarChar).Value = ms.ToArray to command.Parameters.Add("@Image", SqlDbType.VarBinary).Value = ms.ToArray because memoryStream.ToArray returns a byte array, not a string.command.Parameters.Add("@Image", SqlDbType.VarChar).Value = ms.ToArray改为command.Parameters.Add("@Image", SqlDbType.VarBinary).Value = ms.ToArray因为 memoryStream.ToArray 返回一个字节数组,而不是字符串。

  • make sure the Image column in your table is, in fact, VarBinary.确保表中的Image列实际上是 VarBinary。

  • SqlCommand , SqlConnection and MemoryStream all implements the IDisposable interface, therefor you should use all of them as local variable inside the using statement. SqlCommandSqlConnectionMemoryStream都实现了IDisposable接口,因此您应该将它们都用作using语句中的局部变量。 Your code suggest you are using a class level SqlConnecion instance.您的代码建议您使用类级别的SqlConnecion实例。 That should be changed.那应该改变。

  • All communication with the database should be inside a try...catch block, since too many things you can't control can go wrong (network disconnected, for instance).与数据库的所有通信都应该在 try...catch 块内,因为太多您无法控制的事情可能会出错(例如,网络断开连接)。

Your code should look more like this:您的代码应该更像这样:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim RowsEffected as int = 0
    Using Dim connection As NewSqlConnection(ConnectionString
        Using Dim command As New SqlCommand("insert into rent(Image,Status)values(@Image,@Status)", connection)
            Using Dim ms As New MemoryStream
                PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)

                command.Parameters.Add("@Image", SqlDbType.VarBinary).Value = ms.ToArray
                command.Parameters.Add("@Status", SqlDbType.VarChar).Value = TextBox5.Text
                Try
                connection.Open()
                RowsEffected = command.ExecuteNonQuery()
                End Try
                Catch Exception ex
                    MessageBox.Show("Failed to upload image:"& VbCrLf & ex.Message)
                End Catch           
            End Using 
        End Using                                         
    End Using

    If RowsEffected = 1 Then
        MessageBox.Show("Successfully uploaded")
    Else
        MessageBox.Show("Not uploaded")
    End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM