简体   繁体   English

VB.NET > 如何使用 Using 语句插入图像并检索它以显示?

[英]VB.NET > How to insert an image with Using statement and retrieve it to be displayed?

This is the code I'm working on it to insert an image into database.这是我正在处理的将图像插入数据库的代码。 I think there is nothing wrong with the first part.我认为第一部分没有任何问题。 However, somehow this code is not functioning as it supposed to (no image inserted into database).但是,不知何故,这段代码没有按预期运行(没有图像插入数据库)。 I'm not sure whether the commented part of code is related to the execution or not.我不确定代码的注释部分是否与执行有关。 How should I develop it in a more viable manner?我应该如何以更可行的方式开发它? This command is intended to be executed along with saving a list of typed in data into database.此命令旨在与将键入的数据列表保存到数据库中一起执行。 Any help would be nice.你能帮忙的话,我会很高兴。

    Using con As SqlConnection = New SqlConnection("Data Source=LAPTOP-85ALBAVS\SQLEXPRESS;Initial Catalog=Portal;Integrated Security=True")
        Using cmd As SqlCommand = New SqlCommand("INSERT INTO Photo (Img, Pid) VALUES (@Img, @Pid)", con)
            Using ms As New MemoryStream
                pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat)

                cmd.Parameters.Add("@Img", SqlDbType.Image).Value = ms.ToArray()
                cmd.Parameters.Add("@Pid", SqlDbType.VarChar).Value = TextBox1.Text
            End Using
        End Using
    End Using

    'con.Open()

    'If Command.ExecuteNonQuery() = 1 Then
    '    MessageBox.Show("Profile successfully registered!", "Message", MessageBoxButtons.OK)
    'Else
    '    MessageBox.Show("Failed. Try again.", "Message", MessageBoxButtons.OK)
    'End If
    'con.Close()
End Sub

This is the code I'm using to retrieve image from database and display it.这是我用来从数据库中检索图像并显示它的代码。 There is a glitch with this code.这段代码有问题。 If no image found related to the specific Pid , it would pop-up error 'There is no row at position 0.'如果未找到与特定Pid相关的图像,则会弹出错误'There is no row at position 0.' (means no image was uploaded). (意味着没有上传图片)。 But this doesn't affects the overall program execution much, as long as the image is uploaded along with the typed in data together successfully, it would be fine.但这对整个程序的执行影响不大,只要把图片和输入的数据一起上传成功就可以了。

Dim command As New SqlCommand("Select * From Photo Where Pid = @Pid", con)
    command.Parameters.Add("@Pid", SqlDbType.VarChar).Value = TextBox1.Text
    Dim table As New DataTable()
    Dim adapter As New SqlDataAdapter(command)
    adapter.Fill(table)
    Dim img() As Byte
    img = table.Rows(0)(0)
    Dim ms As New MemoryStream(img)
    pictureBox1.Image = Image.FromStream(ms)

I fixed the issue and this is the code for it.我解决了这个问题,这是它的代码。 Do feel free to check it out and hope it helps someone in future :)请随时查看它,并希望它在将来对某人有所帮助:)

Using con As SqlConnection = New SqlConnection("Data Source=LAPTOP-85ALBAVS\SQLEXPRESS;Initial Catalog=Portal;Integrated Security=True")
            Using cmd As SqlCommand = New SqlCommand("INSERT INTO Photo (Img, Pid) VALUES (@Img, @Pid)", con)
                Using ms As New MemoryStream
                    pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat)

                    cmd.Parameters.Add("@Img", SqlDbType.Image).Value = ms.ToArray()
                    cmd.Parameters.Add("@Pid", SqlDbType.VarChar).Value = TextBox1.Text
                    con.Open()
                    cmd.ExecuteNonQuery()
                    MessageBox.Show("Profile has been successfully registered!", "Thank you", MessageBoxButtons.OK)

                End Using
            End Using
        End Using

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

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