Saving & Retrieving An Image
-
I need some help with saving and retrieving images with microsoft sql server. I have created a form that captures personal information and a picture. I am having problems with how to saving this data with the picture. I will also like to display this data at a later time or in another form. Please help.
-
I need some help with saving and retrieving images with microsoft sql server. I have created a form that captures personal information and a picture. I am having problems with how to saving this data with the picture. I will also like to display this data at a later time or in another form. Please help.
-
I need some help with saving and retrieving images with microsoft sql server. I have created a form that captures personal information and a picture. I am having problems with how to saving this data with the picture. I will also like to display this data at a later time or in another form. Please help.
hi, follow step by step 1. first of all i assume PictureBox1 hold the picture data. So use following statement
Dim PictureBox1Data As Byte() If PictureBox1.Image Is Nothing Then PictureBox1Data = Nothing Else PictureBox1Data = imageToByteArray(PictureBox1.Image)'See below this function End If
2.Now write the SQL query as followingDim QueryText As String Dim QueryValue As String Dim command As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand() QueryText = "INSERT INTO Settings(PrintCompany,IsUploadLogo,LogoGraphic )" QueryValue = "Values(@PrintCompany, @IsUploadLogo,@LogoGraphic)" command.CommandText = QueryText & QueryValue
3. Supply Value to parameter which is in above query, as followingcommand.Parameters.Add("@PrintCompany", SqlDbType.TinyInt, 10).Value = True command.Parameters.Add("@IsUploadLogo", SqlDbType.TinyInt, 10).Value =True If Not PictureBox1Data Is Nothing Then command.Parameters.Add("@LogoGraphic", SqlDbType.Image, PictureBox1Data.Length).Value = PictureBox1Data Else command.Parameters.Add("@LogoGraphic", SqlDbType.Image, 0).Value = System.DBNull.Value End If
4. Execute query as followingcommand.ExecuteNonQuery()
step 1 to 4 read data of picture box,convert them byte array and store in related table.Step 1 use following function to convert an image into byte arrayPublic Function imageToByteArray(ByVal ImageIn As System.Drawing.Image) As Byte() Dim ms As MemoryStream = New MemoryStream() Dim FormatImage1 As Imaging.ImageFormat = ImageIn.RawFormat ImageIn.Save(ms, FormatImage1) 'ImageIn.Save(ms, Imaging.ImageFormat.Bmp) Return ms.ToArray() End Function
5. Now Use following statement to get datasetstr = "select * from Settings" Dim ds As New DataSet ds = mdIns.GetResultFromQuery(str)'this function return dataset
6. use dataset record as followingIf ds.Tables(0).Rows.Count > 0 Then chkPrintCompanyName.Checked = CBool(ds.Tables(0).Rows(0).Item(0)) chkLogoGraphic.Checked = CBool(ds.Tables(0).Rows(0).Item(1)) If Not IsDBNull(ds.Tables(0).Rows(0).Item(4)) And Not ds.Tables(0).Rows(0).Item(2) Is Nothing Then PictureBox1.Image = ByteArrayToimage(ds.Tables(0).Rows(0).Item(2))
-
hi, follow step by step 1. first of all i assume PictureBox1 hold the picture data. So use following statement
Dim PictureBox1Data As Byte() If PictureBox1.Image Is Nothing Then PictureBox1Data = Nothing Else PictureBox1Data = imageToByteArray(PictureBox1.Image)'See below this function End If
2.Now write the SQL query as followingDim QueryText As String Dim QueryValue As String Dim command As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand() QueryText = "INSERT INTO Settings(PrintCompany,IsUploadLogo,LogoGraphic )" QueryValue = "Values(@PrintCompany, @IsUploadLogo,@LogoGraphic)" command.CommandText = QueryText & QueryValue
3. Supply Value to parameter which is in above query, as followingcommand.Parameters.Add("@PrintCompany", SqlDbType.TinyInt, 10).Value = True command.Parameters.Add("@IsUploadLogo", SqlDbType.TinyInt, 10).Value =True If Not PictureBox1Data Is Nothing Then command.Parameters.Add("@LogoGraphic", SqlDbType.Image, PictureBox1Data.Length).Value = PictureBox1Data Else command.Parameters.Add("@LogoGraphic", SqlDbType.Image, 0).Value = System.DBNull.Value End If
4. Execute query as followingcommand.ExecuteNonQuery()
step 1 to 4 read data of picture box,convert them byte array and store in related table.Step 1 use following function to convert an image into byte arrayPublic Function imageToByteArray(ByVal ImageIn As System.Drawing.Image) As Byte() Dim ms As MemoryStream = New MemoryStream() Dim FormatImage1 As Imaging.ImageFormat = ImageIn.RawFormat ImageIn.Save(ms, FormatImage1) 'ImageIn.Save(ms, Imaging.ImageFormat.Bmp) Return ms.ToArray() End Function
5. Now Use following statement to get datasetstr = "select * from Settings" Dim ds As New DataSet ds = mdIns.GetResultFromQuery(str)'this function return dataset
6. use dataset record as followingIf ds.Tables(0).Rows.Count > 0 Then chkPrintCompanyName.Checked = CBool(ds.Tables(0).Rows(0).Item(0)) chkLogoGraphic.Checked = CBool(ds.Tables(0).Rows(0).Item(1)) If Not IsDBNull(ds.Tables(0).Rows(0).Item(4)) And Not ds.Tables(0).Rows(0).Item(2) Is Nothing Then PictureBox1.Image = ByteArrayToimage(ds.Tables(0).Rows(0).Item(2))