Hi,
I want to know How can I add image to the SQL database through the form in my Vb.Net project.
Can you guys please help me with the same?
Regards,
Hi,
I want to know How can I add image to the SQL database through the form in my Vb.Net project.
Can you guys please help me with the same?
Regards,
This source code is use to insert picture to SQL Server to binary format that is make the database....
Code:Function SaveImage _ (ByVal FileName As String) As Integer ' Create a FileInfo instance ' to retrieve the files information Dim fi As New FileInfo(FileName) ' Open the Image file for Read Dim imgStream As Stream = _ fi.OpenRead ' Create a Byte array the size ' of the image file for the Read ' methods buffer() byte array Dim imgData(imgStream.Length) As Byte imgStream.Read(imgData, 0, fi.Length) Dim cn As New SqlConnection _ (ConfigurationSettings.AppSettings("cn")) Dim cmd As New SqlCommand("Images_ins", cn) cmd.CommandType = CommandType.StoredProcedure With cmd.Parameters .Add("@FileName", VarChar, 100).Value = _ fi.Name .Add("@FileType", VarChar, 10).Value = + fi.Extension .Add("@Image", Image).Value = imgData .Add("@FileSize", Int, 4).Value = fi.Length End With cn.Open() cmd.ExecuteNonQuery() cn.Close() cn.Dispose() End Function
See the following links. Hope it will help u.
http://www.codeproject.com/KB/web-image/PicManager.aspx
http://www.vbdotnetheaven.com/Upload...SqlServer.aspx
ASP.NET/VB.NET Code to insert images in MS SQL Server Database.
Follow this link!
http://www.dotnetjunkies.com/WebLog/...les/39751.aspx
Here is the code to insert images to a Ms Access database!
A file will be inserted in the Database each time the code is executed.Code:File name is Image.vb Imports System Imports System.IO Imports System.Data Public Class SaveImage Shared Sub main() Dim o As System.IO.FileStream Dim r As StreamReader Dim gifFile As String Console.Write("Enter a Valid .Gif file path") gifFile = Console.ReadLine If Dir(gifFile) = "" Then Console.Write("Invalid File Path") Exit Sub End If o = New FileStream(gifFile, FileMode.Open, FileAccess.Read, FileShare.Read) r = New StreamReader(o) Try Dim FileByteArray(o.Length - 1) As Byte o.Read(FileByteArray, 0, o.Length) Dim Con As New _ System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=Test.mdb") Dim Sql As String = "INSERT INTO Images (Pic,FileSize) VALUES (?,?)" Dim Cmd As New System.Data.OleDb.OleDbCommand(Sql, Con) Cmd.Parameters.Add("@Pic", System.Data.OleDb.OleDbType.Binary, o.Length).Value = FileByteArray Cmd.Parameters.Add("@FileSize", System.Data.OleDb.OleDbType.VarChar, 100).Value = o.Length Con.Open() Cmd.ExecuteNonQuery() Con.Close() Catch ex As Exception Console.Write(ex.ToString) End Try End Sub End Class
Bookmarks