本文介绍了如何在Windows应用程序中使用C#从sql数据库中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void lstSearch_SelectedIndexChanged(对象发件人,EventArgs e)
{
string n1 = ;
string name = lstSearch.SelectedItem.ToString();
// char name2;
for ( int i = 0 ; i < = name.Length-1; i ++)
{
if (Convert.ToInt16(Convert.ToChar(name) .Substring(i, 1 )))!= 32 )
{
n1 = name.Substring( 0 ,i + 1);
}
其他
{
break ;
}
}
con.Open();
cmd = new SqlCommand( select *来自v2,其中empid =' + n1 + ',con);
dr = cmd.ExecuteReader();
byte [] b = new byte [ 0 ];
while (dr.Read())
{
try
{
EmpID.Text = dr.GetString( 0 );
FName.Text = dr.GetString( 1 );
MName.Text = dr.GetString( 2 );
LName.Text = dr.GetString( 3 );
Age.Text = dr.GetString( 4 );
Salary.Text = dr.GetString( 5 );
MgrNo.Text = dr.GetString( 6 );
DeptID.Text = dr.GetString( 7 );
Gndr.Text = dr [ 8 ]。ToString();
byte [] img =( byte [])(dr [ 9 跨度>]);
b =( Byte [])(dr [ 9 ]);
MemoryStream m = new MemoryStream(b);
StreamReader sr = new StreamReader(m);
pictureBox1.Image = Image.FromStream(m);
// PictureBox1.Image = dr.GetString(9);
}
catch (例外情况)
{
// MessageBox.Show(ex.ToString());
}
}
dr.Close();
con.Close();
解决方案
private void lstSearch_SelectedIndexChanged(object sender, EventArgs e) { string n1=""; string name = lstSearch.SelectedItem.ToString(); //char name2; for (int i = 0; i <= name.Length-1; i++) { if (Convert.ToInt16(Convert.ToChar(name.Substring(i, 1))) != 32) { n1 = name.Substring(0, i+1); } else { break; } } con.Open(); cmd = new SqlCommand("select * from v2 where empid = '" + n1 + "'" , con); dr = cmd.ExecuteReader(); byte[] b = new byte[0]; while (dr.Read()) { try { EmpID.Text = dr.GetString(0); FName.Text = dr.GetString(1); MName.Text = dr.GetString(2); LName.Text = dr.GetString(3); Age.Text = dr.GetString(4); Salary.Text = dr.GetString(5); MgrNo.Text = dr.GetString(6); DeptID.Text = dr.GetString(7); Gndr.Text = dr[8].ToString(); byte[] img = (byte[])(dr[9]); b = (Byte[])(dr[9]); MemoryStream m = new MemoryStream(b); StreamReader sr = new StreamReader(m); pictureBox1.Image = Image.FromStream(m); // PictureBox1.Image = dr.GetString(9); } catch (Exception ex) { //MessageBox.Show(ex.ToString()); } } dr.Close(); con.Close();
解决方案
这篇关于如何在Windows应用程序中使用C#从sql数据库中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!