Inserting And Retrieve Image

Posted by imomins on April 5, 2011 at 2:17 AM

 private void mpriiInsertIntoImage()

        {

            // int liReturnValue = -1;

            try

            {

                if (DatabaseConnector.gmpubstasqlconnSQLConnection.State == ConnectionState.Open)

                {

                    DatabaseConnector.gmpubstasqlconnSQLConnection.Close();

                }

                DatabaseConnector.gmpubstasqlconnSQLConnection.Open();

                SqlCommand lsqlcommSQLCommand = DatabaseConnector.gmpubstasqlconnSQLConnection.CreateCommand();

                byte[] lbyteEmployeePhoto = ReadFile(txtBrowse.Text);

                mpristrSQLQuery = "insert into  SAM_CUSTOMER_IMAGE(FORM_NUMBER, CUSTOMER_IMAGE) values ('" + txtFormNo.Text + "', @ImageData)";

                SqlCommand SqlCom = new SqlCommand(mpristrSQLQuery, DatabaseConnector.gmpubstasqlconnSQLConnection);

                SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)lbyteEmployeePhoto));

                SqlCom.ExecuteNonQuery();

                mpristrSQLQuery = null;

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

                //return liReturnValue;

            }

        }

 

        byte[] ReadFile(string sPath)

        {

            byte[] data = null;

 

            FileInfo fInfo = new FileInfo(sPath);

            long numBytes = fInfo.Length;

            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            BinaryReader br = new BinaryReader(fStream);

            data = br.ReadBytes((int)numBytes);

            return data;

        }

 





if (mpribIsExists())

                    {

                        pb1Photo.Image = null;

                        pb2Photo.Image = null;

                        pb3Photo.Image = null;

                        pb4Photo.Image = null;

                        pb5Photo.Image = null;

                        int i = 0;

                        Byte[] byteBLOBData1 = new Byte[0];

                        Byte[] byteBLOBData2 = new Byte[0];

                        Byte[] byteBLOBData3 = new Byte[0];

                        Byte[] byteBLOBData4 = new Byte[0];

                        Byte[] byteBLOBData5 = new Byte[0];

                        string lstrSQL = "SELECT LAND_CONFIRM_PI_DOLIL_ATTACMENT FROM LAND_CONFIRM_PI_DOLIL_REG WHERE LAND_CONFIRM_PI_DOLIL_REG_NO='" + txtDoliRegNo.Text.Trim() + "'";

                        DatabaseConnector.gmpubstasqlconnSQLConnection.Open();

                        SqlCommand thisCommand = DatabaseConnector.gmpubstasqlconnSQLConnection.CreateCommand();

                        thisCommand.CommandText = lstrSQL;

                        SqlDataReader thisReader = thisCommand.ExecuteReader();

                      

                        while (thisReader.Read())

                        {

                          

                            if (i == 0)

                            {

                                byteBLOBData1 = (Byte[])(thisReader["LAND_CONFIRM_PI_DOLIL_ATTACMENT"]);

                                MemoryStream ms1 = new MemoryStream(byteBLOBData1);

                                pb1Photo.Image = Image.FromStream(ms1);

                           

                            }

                            if (i == 1)

                            {

                                byteBLOBData2 = (Byte[])(thisReader["LAND_CONFIRM_PI_DOLIL_ATTACMENT"]);

                                MemoryStream ms2 = new MemoryStream(byteBLOBData2);

                                pb2Photo.Image = Image.FromStream(ms2);

                             

                            }

                            if (i == 2)

                            {

                                byteBLOBData3 = (Byte[])(thisReader["LAND_CONFIRM_PI_DOLIL_ATTACMENT"]);

                                MemoryStream ms3 = new MemoryStream(byteBLOBData3);

                                pb3Photo.Image = Image.FromStream(ms3);

                              

                            }

                            if (i == 3)

                            {

                                byteBLOBData4 = (Byte[])(thisReader["LAND_CONFIRM_PI_DOLIL_ATTACMENT"]);

                                MemoryStream ms4 = new MemoryStream(byteBLOBData4);

                                pb4Photo.Image = Image.FromStream(ms4);

                              

                            }

                            if (i == 4)

                            {

                                byteBLOBData5 = (Byte[])(thisReader["LAND_CONFIRM_PI_DOLIL_ATTACMENT"]);

                                MemoryStream ms5 = new MemoryStream(byteBLOBData5);

                                pb5Photo.Image = Image.FromStream(ms5);

                            }

                            i++;

                        }

 


private bool mpribIsExists()

        {

 

            try

            {

                OperationGateway operationGateway = new OperationGateway();

                return operationGateway.mpubiIsExistsRecord("LAND_CONFIRM_PI_DOLIL_REG", "LAND_CONFIRM_PI_DOLIL_REG_NO", txtDoliRegNo.Text.Trim());

            }

            catch (Exception pexpIsExistsException)

            {

                MessageBox.Show(pexpIsExistsException.Message);

                return false;

            }

        }

 

 


Categories: C#, SQL, Personal

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register

0 Comments