Insert image in a database
Imports System.Data.SqlClient Imports System.IO Public Class Form1 Dim fName As String Dim cnn As SqlConnection Dim connectionString As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click connectionString = "Data Source=servername; Initial Catalog=databasename; User ID=sa; Password=password" cnn = New SqlConnection(connectionString) fName = "D:\picfile.jpg" If File.Exists(fName) Then Dim id As Integer = 1 Dim content As Byte() = ImageToStream(fName) cnn.Open() Dim cmd As New SqlCommand("insert into imgtable (id,img) values ( @id,@img)", cnn) cmd.Parameters.AddWithValue("@id", id) cmd.Parameters.AddWithValue("@img", content) cmd.ExecuteNonQuery() cnn.Close() MsgBox("Image inserted") Else MsgBox(fName & " not found ") End If End Sub Private Function ImageToStream(ByVal fileName As String) As Byte() Dim stream As New MemoryStream() tryagain: Try Dim image As New Bitmap(fileName) image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg) Catch ex As Exception GoTo tryagain End Try Return Stream.ToArray() End Function End Class
VB.NET Dataset - Related Contents
- What is ADO.NET Dataset
- How to Dataset with Sql Server
- How to Dataset with OLEDB Data Source
- Search Tables in a Dataset Sql Server
- Search Tables in a Dataset OLEDB Data Source
- Dataset table row count in SQL Server
- Dataset table row count - OLEDB Data Source
- How to find column definition - Sql Server
- How to find column definition - OLEDB Data Source
- How to create DataSet without Databse
- How to multiple tables in Dataset - Sql Server
- How to multiple tables in Dataset - OLEDB Data Source
- How to add relations between tables in a Dataset
- How to merge tables in a Dataset - Sql Server
- How to merge tables in a Dataset - OLEDB Data Source
- How to populate a combo box from a dataset in VB.Net , binding combobox with enum values , binding combobox with dictionary values
- how many tables exist in a database - VB.NET
VB.NET Dataset - Related Programs