ad pic into database

Upload: vipin-gopal

Post on 09-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Ad Pic Into Database

    1/3

    How to Store or Save Image in SQL Server table

    To store an image in to sql server, you need to read image file into a byte array. Once you have image data in byte array, you can easity store this image datain sql server using sql parameters. Following code explains you how to do this.

    private void cmdSave_Click(object sender, EventArgs e){

    try{

    //Read Image Bytes into a byte arraybyte[] imageData = ReadFile(textboxname.Text);

    //Initialize SQL Server ConnectionSqlConnection CN = new SqlConnection(txtConnectionString.Text);

    //Set insert querystring 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();

    }

    Following code explains how to read image file in to a byte array.

    //Open file into 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 fileFileStream fStream = new FileStream(sPath, FileMode.Open,

    FileAccess.Read);

  • 8/8/2019 Ad Pic Into Database

    2/3

    //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;

    }

    How to read image data bytes from SQL Server table

    To read images from SQL Server, prepare a dataset first which will hold data from SQL Server table. Bind this dataset with a gridview control on form.

    void GetImagesFromDatabase(){

    try{

    //Initialize SQL Server connection.SqlConnection CN = new SqlConnection(txtConnectionString.Text);

    //Initialize SQL adapter.SqlDataAdapter ADAP = new SqlDataAdapter("Select * from ImagesStore"

    , CN);

    //Initialize Dataset.DataSet DS = new DataSet();

    //Fill dataset with ImagesStore table.ADAP.Fill(DS, "ImagesStore");

    //Fill Grid with dataset.dataGridView1.DataSource = DS.Tables["ImagesStore"];

    }catch(Exception ex){

    MessageBox.Show(ex.ToString());}

    }

    Once you have image data in grid, get image data from grid cell. Alternatively you can also get image data from Dataset table cell.

    //Store image to a local file.pictureBox1.Image.Save("c:\test_picture.jpg",

    System.Drawing.Imaging.ImageFormat.Jpeg);

    If you want you can extend this code to save image from Picture Box to a local image file.

    //Store image to a local file.

  • 8/8/2019 Ad Pic Into Database

    3/3

    pictureBox1.Image.Save("c:\test_picture.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);