ToBe

ToBe

  • NA
  • 164
  • 94.2k

Uploading multi images to SQL Server

May 29 2013 2:26 AM
Hi every one

I have this code included in my Save button to save image in the the Database.

it's working perfect. but i want to do it for 5 FileUpload Controller how can i do it ?

  string cn_str = ConfigurationManager.ConnectionStrings["REGConnectionString"].ConnectionString;

            SqlConnection con = new SqlConnection(cn_str);

            con.Open();

            SqlCommand cmd_sql = new SqlCommand("imageAdd", con);
            cmd_sql.CommandType = CommandType.StoredProcedure;


            string filePath = this.FileUpload1.PostedFile.FileName;
            string filename = Path.GetFileName(filePath);
            string ext = Path.GetExtension(filename);

            Stream imgStream = FileUpload1.PostedFile.InputStream;
            BinaryReader imgBinary = new BinaryReader(imgStream);
            Byte[] bytes = imgBinary.ReadBytes((Int32)imgStream.Length);
            imgBinary.Close();
            imgStream.Close();

            cmd_sql.Parameters.Add("@imgName", SqlDbType.Text).Value = filename;
            cmd_sql.Parameters.Add("@imgType", SqlDbType.Text).Value = ext.ToString() ;
            cmd_sql.Parameters.Add("@imgData", SqlDbType.Binary).Value = bytes;

            cmd_sql.ExecuteNonQuery();

Answers (6)