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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Suthish NairPosted Oct 20, 2010, 2:21 AM
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.
Soumya SurendranPosted Oct 20, 2010, 2:55 AM
Manikavelu VelayuthamPosted Oct 20, 2010, 2:01 AM
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/
Manikavelu VelayuthamPosted Oct 20, 2010, 1:58 AM
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
Posted Oct 20, 2010, 1:58 AM
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();