简体   繁体   中英

How Do I Find A Specific Image From My Database

private void button1_Click(object sender, EventArgs e)
{
   SqlConnection cn = new SqlConnection(@"Data Source=SAZ-PC\SQLEXPRESS;Initial Catalog=Voted;Integrated Security=True");
   SqlCommand cmd1 = new SqlCommand("select FINGERPRINT from Regdmem ", cn);
   cn.Open();

   Byte[] barrImg = (Byte[])cmd1.ExecuteScalar();

   foreach (byte fp in barrImg)
   {
        Byte[] bytes = File.ReadAllBytes("D:\\Image.bmp");
        bool cmp = barrImg.SequenceEqual(bytes);

        if (cmp == true)
        {
            Form3 f3 = new Form3();
            f3.Show();
            this.Hide();
        }
        else
        {
            Application.Exit();
        }
    }

    cn.Close();
}

In my database I have a table with a column named FINGERPRINT . In that column, multiple images are stored.

I also have an image on my harddrive ( D:\\\\Image.bmp ).

My question is, how do I check whether this image is already stored in my database and if so, go to the next form of my application.

ExecuteScalar returns the value from first column of first row. You must use the ExecuteReader to get all images.

Byte[] bytes = File.ReadAllBytes("D:\\Image.bmp");
SqlDataReader reader = cmd1.ExecuteReader();

while (reader.Read())
{
    Byte[] barrImg = (Byte[])reader[0];

    bool cmp = barrImg.SequenceEqual(bytes);

    if (cmp == true)
    {
        Form3 f3 = new Form3();
        f3.Show();
        this.Hide();
    }
    else
    {
        Application.Exit();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM