Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing an integer variable from C# to Microsoft SQL server and then comparing the retrieved data
    text
    copied!<p>I am trying to develop a CBIR application and thus I am taking an input image, calculating its RGB values in three different variables and modifying the values, then storing them in three variables: <code>r1</code>, <code>g1</code>, <code>b1</code>.</p> <p>Then I am trying to pass these values to database so that I can retrieve images by comparing them using comparison operators, but I am unable to get any data, though I think I am using parameters in a right way.</p> <p>Here is my code:</p> <pre><code>double r1, r2, g1, g2, b1, b2; r1 = aveR -(.1 * aveR); r2 = aveR +(.1 * aveR); g1 = aveG -(.1 * aveG); g2 = aveG +(.1 * aveG); b1 = aveB -(.1 * aveB); b2 = aveB +(.1 * aveB); cn = new SqlConnection(@"Data Source=HOME;Initial Catalog=test_image;Integrated Security=True"); cmd = new SqlCommand("select path from image where red BETWEEN @r1 AND @r2 AND green BETWEEN @g1 AND @g2 AND blue BETWEEN @b1 AND @b2" , cn); cmd.Parameters.AddWithValue("@r1", r1); cmd.Parameters.AddWithValue("@r2", r2); cmd.Parameters.AddWithValue("@g1", g1); cmd.Parameters.AddWithValue("@g2", g2); cmd.Parameters.AddWithValue("@b1", b1); cmd.Parameters.AddWithValue("@b2", b2); cn.Open(); SqlDataReader dr; try { dr = cmd.ExecuteReader(); while (dr.Read()) { progressBar1.Value = progressBar1.Value + 9; listBox1.Items.Add(dr["path"].ToString()); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { cn.Close(); } /*after this i dont get any data in my list box, where i am expecting the paths of image("path" col in my DB) which should be returned after the comparison query.*/ </code></pre> <p>Here is the code that populates (hope I don't get it wrong, i.e. this code takes the images one by one and then inserts them into the database).</p> <pre><code>int width = 256; int height = 128; int sumr = 0, sumg = 0, sumb = 0; int rank = 0; openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png; *.gif"; DialogResult dr = openFileDialog1.ShowDialog(); listBox1.Items.Add("Pixel RED GREEN BLUE "); listBox2.Items.Add("avgR avgG avgB "); if (dr == System.Windows.Forms.DialogResult.OK) { foreach(string file in openFileDialog1.FileNames) { try { Image img = Image.FromFile(file); Image img1 = Resizeimage(img, width, height); pictureBox1.Image = img1; textBox1.Text = file; Bitmap img2 = new Bitmap(img1); Color c; for (int i = 0; i &lt; img2.Width; i++) { for (int j = 0; j &lt; img2.Height; j++) { c = img2.GetPixel(i, j); int r = Convert.ToInt16(c.R); int g = Convert.ToInt16(c.G); int b = Convert.ToInt16(c.B); sumr = sumr + r; sumg = sumg + g; sumb = sumb + b; listBox1.Items.Add(i.ToString() + "," + j.ToString() + " " + r.ToString() + " " + g.ToString() + " " + b.ToString()); } } int total = img2.Height * img2.Width; int aveR = sumr / total; int aveG = sumg / total; int aveB = sumb / total; listBox2.Items.Add(aveR.ToString() + " " + aveG.ToString() + " " + aveB.ToString()); Application.DoEvents(); Thread.Sleep(5000); SqlConnection con = new SqlConnection(@"Data Source=HOME;Initial Catalog=test_image;Integrated Security=True"); MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] pic_arr = new byte[ms.Length]; ms.Position = 0; ms.Read(pic_arr, 0, pic_arr.Length); SqlCommand cmd = new SqlCommand("insert into image(path,image,red,green,blue,rank)values(@path,@image,@red, @green, @blue, @rank)", con); cmd.Parameters.AddWithValue("@path", textBox1.Text); cmd.Parameters.AddWithValue("@image", pic_arr); cmd.Parameters.AddWithValue("@red", aveR); cmd.Parameters.AddWithValue("@green", aveG); cmd.Parameters.AddWithValue("@blue", aveB); cmd.Parameters.AddWithValue("@rank", rank); con.Open(); try { int rs = cmd.ExecuteNonQuery(); if (rs &gt; 0) { MessageBox.Show("image stored successfully"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } catch (Exception ex) { MessageBox.Show("cannot display the image"+ ex.Message); } listBox1.Items.Clear(); listBox2.Items.Clear(); } } } public static Image Resizeimage(Image img, int width, int height) { Image newimg = new Bitmap(width, height); Graphics g = Graphics.FromImage(newimg); g.DrawImage(img, 0, 0, width, height); return newimg; } /** I am sorry if braces make you all little upset, I am new to this site, so editing might be poor**/ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload