0

I want to insert an image in .mdf file directly from the Picture Box, without using FileInfo.

Is it possible to do this ?? If yes then plz answer. Thanks in advance Dev..

Treb
  • 19,903
  • 7
  • 54
  • 87
Dev
  • 260
  • 4
  • 18
  • 34
  • The title and the actual question seem to contradict each other... Please elaborate on what exactly you want to do: Where does the picture come from, what do you want to do with it and where dou you want to store it? – Treb Mar 07 '12 at 07:49
  • For me saving image into DB is good idea because of performance. However, here you can find out http://stackoverflow.com/questions/416881/insert-picture-into-sql-server-2005-image-field-using-only-sql – Thit Lwin Oo Mar 07 '12 at 07:50
  • I want to store an image in .mdf file which is present in a picture box. But i want to do this without the help of FileInfo. – Dev Mar 07 '12 at 07:56
  • i have to write "int len = Upload.PostedFile.ContentLength;" in VB. What i have to write ?? – Dev Mar 07 '12 at 07:58
  • I am confused - why does the title of your question mention storing the image in an SQL database, when you want to store it in an image file? Why do you want to avoid using a FileInfo object? Anyway, it would help if you posted some example source code. – Treb Mar 07 '12 at 08:16

2 Answers2

1

Lets say you have in aspx page:

<input id="File1" type="file" runat="server"/>
<asp:Button ID="OnUpload" runat="server" Text="Upload" />

And in vb page

Protected Sub OnUpload_Click(sender As Object, e As EventArgs) Handles OnUpload.Click
    ' Create a byte[] from the input file
    Dim len As Integer = File1.PostedFile.ContentLength
    Dim pic As Byte() = New Byte(len - 1) {}
    File1.PostedFile.InputStream.Read(pic, 0, len)
    ' Insert the image and comment into the database
    Dim connection As New SqlConnection("server=localhost;database=gallery;uid=yourusername;pwd=yourpassword")
    Try
        connection.Open()
        Dim cmd As New SqlCommand("insert into Image " & "(Picture) values (@image)", connection)
        cmd.Parameters.Add("@image", SqlDbType.Image, pic.Length).Value = pic
        cmd.ExecuteNonQuery()
    Finally
        connection.Close()
    End Try

End Sub

To insert an image into a SQL Server first make sure the data type of the column the image is being added to is the Image data type.

Give it a try...

And you can find here some examples: http://infynet.wordpress.com/2010/09/29/store-image-in-sql-database-using-vb-net/

On the other hand it's not recommended to save pictures in the database, your database will turn huge in no time and backups and restore will turn impossible.

Gabriel
  • 2,011
  • 14
  • 14
0

Yes... it is possible!

Once you set the image to the Picture Box, you then can retrieve it the same way.

Image img = myPictureBox.Image

Now take that image and save it in database or save it in hard disk and just save in database the file path.

Romias
  • 13,783
  • 7
  • 56
  • 85