hi,,,,,
i want to store image in database using application programming.
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.
Munesh SharmaPosted May 5, 2011, 12:26 AM
write these code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace StoreImagesInSQLServer
{
public partial class frmNewImage : Form
{
public frmNewImage()
{
InitializeComponent();
}
private void cmdSave_Click(object sender, EventArgs e)
{
try
{
//Read Image Bytes into a byte array
byte[] imageData = ReadFile(txtImagePath.Text);
//Initialize SQL Server Connection
SqlConnection CN = new SqlConnection(txtConnectionString.Text);
//Set insert query
string qry = "insert into ImagesStore (OriginalPath,ImageData) values(@OriginalPath, @ImageData)";
//Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, CN);
//We are passing Original Image Path and Image byte data as sql parameters.
SqlCom.Parameters.Add(new SqlParameter("@OriginalPath", (object)txtImagePath.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)imageData));
//Open connection and execute insert query.
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close();
//Close form and return to list or images.
this.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void cmdBrowse_Click(object sender, EventArgs e)
{
//Ask user to select file.
OpenFileDialog dlg = new OpenFileDialog();
DialogResult dlgRes = dlg.ShowDialog();
if (dlgRes != DialogResult.Cancel)
{
//Set image in picture box
pictureBox1.ImageLocation = dlg.FileName;
//Provide file path in txtImagePath text box.
txtImagePath.Text = dlg.FileName;
}
}
//Open file in to a filestream and read data in a byte array.
byte[] ReadFile(string sPath)
{
//Initialize byte array with a null value initially.
byte[] data = null;
//Use FileInfo object to get file size.
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
//Open FileStream to read file
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
//Use BinaryReader to read file stream into byte array.
BinaryReader br = new BinaryReader(fStream);
//When you use BinaryReader, you need to supply number of bytes to read from file.
//In this case we want to read entire file. So supplying total number of bytes.
data = br.ReadBytes((int)numBytes);
return data;
}
private void cmdCancel_Click(object sender, EventArgs e)
{
//Close this form if user clicks cancel.
this.Close();
}
private void frmNewImage_Load(object sender, EventArgs e)
{
}
}
}
i hope your problem best solution.