图片再次自动显示在图片框中的员工ID [英] Picture show automaticlly in picture box again employee ID

查看:75
本文介绍了图片再次自动显示在图片框中的员工ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片框中的图片再次显示员工ID

我会做什么。

FOR EXAMPAL(textBox1.Text = dr.GetString(1)

pictureBox1.image = ???



我的代码在下面

FOR PICTURE SHOWING in picture box again employee id
WHAT WILL I DO.
FOR EXAMPAL ( textBox1.Text = dr.GetString(1)
pictureBox1.image = ???

My Code is below

bool temp = false;
SqlConnection con = new SqlConnection("server=WASEEM\\SQLEXPRESS;database=malik;Trusted_Connection=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Table2 where first='" + textBox1.Text.Trim() + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
textBox2.Text = dr.GetString(1);
textBox3.Text = dr.GetString(2);
pictureBox1.Image = dr.GetString(3);
temp = true;
}
if (temp == false)
MessageBox.Show("not found");
con.Close();
}
}





我的尝试:



图片再次自动显示在图片框中员工ID



What I have tried:

Picture show automaticlly in picture box again employee ID

推荐答案

这将取决于您在数据库中存储的确切内容图像:您将其作为字符串进行检索,因此您可以存储图像的URL,图像作为base64字符串,甚至只是尝试将Image类实例转储到DB中。

我们不知道。



您需要做的是先看看如何存储它以确切了解您的内容存储,然后反转该过程以将Image控件实例设置到PictureBox中。



但是......最常见的错误是通过myImage.ToString()保存图像,或者将其转换为字节数组,并使用myImageData进行调用。 ToString() - 两者都不起作用,这两者都意味着你的DB包含无用的垃圾。看看这里:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它显示了一种正确的方法。



并为自己做几个好处:

1)停止使用 SELECT * 并命名要获取的字段:

It's going to depend on exactly what you stored in your DB as the Image: you are retrieving it as a string, so you could have stored the URL of the image, the image as a base64 string, or even just tried to dump the Image class instance into the DB.
We don't know.

What you need to do is start by looking at how you store it to find out exactly what you store, and then reverse the process to get an Image control instance to set into the PictureBox.

But...the most common mistake is to save the Image via myImage.ToString(), or convert it to a byte array, and sat that with myImageData.ToString() - neither of which will work and both of which will mean your DB contains useless rubbish. Have a look here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it shows one way to do it properly.

And do yourself several favours:
1) Stop using SELECT * and name the fields you want to fetch:
SELECT ID, UserName, UserImage FROM ...

效率更高。

2)不要在DataReader中使用数字索引:命名索引使代码更容易阅读,更可靠。

3)停止使用所有内容的默认名称:Table2,textBox3 - 您现在可能还记得它们包含的内容,但是您需要在三周内更改代码。使用描述他们所做事情的正确名称:tabUsers,tbUserName,......它们使你的代码更加自我记录,并提高可靠性。

4)真的,非常重要:从不连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

It's a lot more efficient.
2) Don't use numeric indexes into the DataReader: named indexes make your code easier to read and much more reliable.
3) Stop using default names for everything: Table2, textBox3 - you may remember now what they contain, but you won't in three weeks when you need to change the code. use "proper" names which describe what they do: tabUsers, tbUserName, ... they make your code more self documenting, and that improves reliability as well.
4) And really, really important: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


这篇关于图片再次自动显示在图片框中的员工ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆