Sunil Kumar Gautam
How can store image file in sql server through C# coding?
By Sunil Kumar Gautam in .NET on Aug 27 2006
  • uday kiran
    Aug, 2006 28

    (Select Image file (bmp, jpg).....

    Requirement:- pictuebox, 3 buttons

    -------------------------------------

    private void btnOpen_Click(object sender, System.EventArgs e)

    {OpenFileDialog opnFileBoxImage = new OpenFileDialog();

    if (opnFileBoxImage.ShowDialog() == DialogResult.OK)

    {strBLOBFilePath = opnFileBoxImage.FileName;

    pictureBox1.Image = Image.FromFile(strBLOBFilePath);

    }}

    ==================================================

    (Save Image file in to SQL server)

    note :- ( create table imageTable (empid int, img image))

    private void button1_Click(object sender, System.EventArgs e)

    {

    MemoryStream ms = new MemoryStream();
    PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat);
    byte[] bytes = ms.GetBuffer;
    SqlConnection con = new SqlConnection("server=SERVERNAME;database=DATABASENAME;user id=SA; password=");
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "insert imagetable values(@img)";
    cmd.Parameters.Add(new SqlParameter("@img", SqlDbType.Image)).Value = bytes;
    cmd.ExecuteNonQuery();
    con.Close();
    }

    =========================================

    (retreive image from SQL server)

    (button click)

    sqlconnection con = new sqlconnection(connectionPath);
    sqlcommand cmd1 = new sqlcommand("Select img from imageTable", con);
    sqldataadapter da = new sqldataadapter(cmd1);
    dataset ds = new dataset();
    da.fill(ds, "ima");
    byte[] bytes = ((Bytes)(ds.tables(0).rows(0)(0)));
    memortstream ms = new memortstream(bytes);
    picturebox1.image = picturebox1.image.fromstream(ms);
    picturebox1.refresh();

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS