hai expert............
How to insert image to access database using c# code?
thanks in advance...
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Suthish NairRank #92 · 22.8K reputationPosted Oct 19, 2010, 10:21 PM EDT
Access does not handle images very efficiently.
You can store the images to a physical folder and then path with file name to DB Table.
If for learning purpose, then go for above samples.
Posted Oct 19, 2010, 9:58 PM EDT
Please try this ....
//select the file here
openFileDialog1.ShowDialog();
System.IO.FileStream imagefs = new System.IO.FileStream(openFileDialog1.FileName, System.IO.FileMode.Open);
byte[] buffer = new byte[imagefs.Length];
BinaryReader mbr = new BinaryReader(imagefs);
mbr.Read(buffer, 0,Convert.ToInt32( imagefs.Length));
string connectionstirng = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb;User Id=admin;Password=;";
string query = " insert into table1 (Images) values (?)";
OleDbConnection con = new OleDbConnection();
con.ConnectionString = connectionstirng;
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = query;
OleDbParameter param = new OleDbParameter("@image", OleDbType.Binary);
param.Value = buffer;
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
con.Close();
Manikavelu VelayuthamRank #413 · 6K reputationPosted Oct 19, 2010, 9:58 PM EDT
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
Manikavelu VelayuthamRank #413 · 6K reputationPosted Oct 19, 2010, 10:01 PM EDT
http://programming.top54u.com/post/Store-and-Display-Images-from-MS-Access-Database-Using-C-Sharp.aspx
http://www.aspfree.com/c/a/ASP.NET/Uploading-Images-to-a-Database--C---Part-I/
Soumya Surendran84 reputationPosted Oct 19, 2010, 10:55 PM EDT