本文介绍了使用文本框中的值从数据库中搜索图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我从数据库中检索图片但是我的代码给了我错误: - 无法将字节转换为字节[] 这个tis代码: - s =选择Driver_ID,Driver_Name,Tel1,Tel2,地址,Date_S_Work,Place_id,License_type_id,Date_S_License,Date_E_License,Notes; s = s +FROM Drivers其中Driver_ID = @Driver_ID; sCommand = new SqlCommand(s,con); sCommand.Parameters.AddWithValue(@ Driver_ID,txt_code.Text); sdReader = sCommand.ExecuteReader(); if(sdReader.Read()) { txt_Driver_Name.Text = sdReader.GetString(1) ; txt_Tel1.Text = sdReader.GetString(2); txt_Tel2.Text = sdReader.IsDBNull(3)? null:sdReader.GetString(3); txt_Address.Text = sdReader.GetString(4); dtp_S_work.Value = sdReader.GetDateTime(5); CB_WorkPlace.SelectedValue = sdReader.GetInt32(6); CB_License_type.SelectedValue = sdReader.GetInt32(7); dtp_StartLicense.Value = sdReader.GetDateTime (8); dTP_Date_e_License.Value = sdReader.GetDateTime(9); txt_Notes.Text = sdReader.IsDBNull(10)? null:sdReader.GetString(10); byte [] DriverImg =(byte [])sdReader.GetByte(11); MemoryStream ms = new MemoryStream(DriverImg); pictureBox_IMG.Image = Image.FromStream(ms); 我有什么尝试过: 如何从数据库中检索图片?或者我如何解决这个问题?i retrive picture from database but my code give me error:- cannot convert byte to byte[]this tis code:- s = " Select Driver_ID,Driver_Name, Tel1, Tel2, Address, Date_S_Work, Place_id, License_type_id, Date_S_License, Date_E_License, Notes "; s = s + " FROM Drivers where Driver_ID = @Driver_ID "; sCommand = new SqlCommand(s, con); sCommand.Parameters.AddWithValue("@Driver_ID", txt_code.Text); sdReader = sCommand.ExecuteReader(); if (sdReader.Read()) { txt_Driver_Name.Text = sdReader.GetString(1); txt_Tel1.Text = sdReader.GetString(2); txt_Tel2.Text = sdReader.IsDBNull(3) ? null : sdReader.GetString(3); txt_Address.Text = sdReader.GetString(4); dtp_S_work.Value = sdReader.GetDateTime(5); CB_WorkPlace.SelectedValue = sdReader.GetInt32(6); CB_License_type.SelectedValue = sdReader.GetInt32(7); dtp_StartLicense.Value = sdReader.GetDateTime(8); dTP_Date_e_License.Value = sdReader.GetDateTime(9); txt_Notes.Text = sdReader.IsDBNull(10) ? null : sdReader.GetString(10); byte[] DriverImg = (byte[])sdReader.GetByte(11); MemoryStream ms = new MemoryStream(DriverImg); pictureBox_IMG.Image = Image.FromStream(ms);What I have tried:How i retrive picture from database? or how i solve this problem ?推荐答案 试试这个 c# - 从SqlDataReader获取byte []? - 堆栈溢出 [ ^ ] 如果你看一下GetByte的定义: SqlDataReader.GetByte方法(Int32)(System.Data.SqlClient) [ ^ ]它从一列返回一个字节,而不是一个字节数组。 /> 你不能把一个字节转换成一个数组 - 这是行不通的。 看看这里:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它显示了如何操作,并解释了您可能遇到的一些陷阱。If you look at the definition of GetByte: SqlDataReader.GetByte Method (Int32) (System.Data.SqlClient)[^] it returns a single byte from a column, not an array of bytes.And you can't cast a byte to an array - that just won't work.Have a look here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it shows how to do it, and explains some of the pitfalls you might meet. 这篇关于使用文本框中的值从数据库中搜索图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 08:42