We usually write this.
using (SqlDataReader DRM = Sqlcomm.executereader ()) { DRM. Read ();//The following shows the image stream in the database as it appears in the picture frame. MemoryStream ms = new MemoryStream ((byte[]) drm["Logo"]); Image img = image.fromstream (ms); This.pictureBox1.Image = img; }
My writing data
private void Btnok_click (object sender, EventArgs e) {string name = ""; if (tbxUserName.Text.Trim () = = "") {MessageBox.Show ("name cannot be empty, please re-enter!") "," prompt "); Return } else {name = TbxUserName.Text.Trim (); } String group = ""; if (cbxGroup.Text.Trim () = = "") {group = "not grouped"; } else {group = CbxGroup.Text.Trim (); } String phone = TbxTel.Text.Trim (); String workunit = TbxWorkUnit.Text.Trim (); string email = TbxEmail.Text.Trim (); String QQ = TbxQQ.Text.Trim (); Byte[] B = null; if (Txtfilepath! = "") {try {FileStream fs = new FileStream ( Txtfilepath, FileMode.Open, FileAccess.Read);//type for Open Data permission for read-only not write data int length = Convert.ToInt32 (fs. Length); b = new Byte[length]; Fs. Read (b, 0, length);//data reads into the B array from number NO. 0 to length-bit fs. Close ();//Turn off input/output stream} catch (Exception ex) {b = null; MessageBox.Show (ex. Message); }} else {b = pixdata; } try {using (SqlConnection connection = new SqlConnection (connectionString)) {//Get the current data in the table ID SqlCommand commandinsert = new SqlCommand (); Commandinsert.connection = Connection; Connection. Close (); Connection. Open (); Insert Data Commandinsert.commandtext = "Use DB_TXL UPDATE tb_busicinfo set groups= @Groups, [Email pro Tected],[email protected],[email protected],[email pRotected],[email protected],[email protected] "; Commandinsert.commandtext + = "where [email protected]"; COMMANDINSERT.PARAMETERS.ADD ("@ID", SqlDbType.Int); COMMANDINSERT.PARAMETERS.ADD ("@Groups", SqlDbType.VarChar, 50); COMMANDINSERT.PARAMETERS.ADD ("@Name", SqlDbType.VarChar, 20); COMMANDINSERT.PARAMETERS.ADD ("@WorkUnit", SqlDbType.VarChar, 50); COMMANDINSERT.PARAMETERS.ADD ("@Phone", SqlDbType.VarChar, 14); COMMANDINSERT.PARAMETERS.ADD ("@Email", SqlDbType.VarChar, 50); COMMANDINSERT.PARAMETERS.ADD ("@QQ", SqlDbType.VarChar, 20); COMMANDINSERT.PARAMETERS.ADD ("@Picture", sqldbtype.image),//image type can not be written wrong!! commandinsert.parameters["@ID"]. Value = ID; commandinsert.parameters["@UserName"]. Value = strUserName; commandinsert.parameters["@Groups"]. Value = Group; commandinsert.parameters["@Name"]. Value = name; commandinsert.parameters["@WorkUnit"]. Value = Workunit; commandinsert.parameters["@Phone"]. Value = phone; commandinsert.parameters["@Email"]. Value = email; commandinsert.parameters["@QQ"]. Value = QQ; Commandinsert.parameters[""]. value=; if (Txtfilepath = = "" && pixdata==null) {commandinsert.parameters["@Pic Ture "]. Value = Dbnull.value;//dbnull does not exist, null} else { commandinsert.parameters["@Picture"]. Value = b; } commandinsert.executenonquery (); Connection. Close (); DialogResult = DialogResult.OK; }} catch (Exception ex) {MessageBox.Show (ex). Message); } }
My reading data
private void Frmedit_load (object sender, EventArgs e) {try {using (SqlC onnection connection = new SqlConnection (connectionString)) {string commandstring = "se Lect * from Tb_busicinfo where id= "+ ID. ToString () + ""; SqlCommand command = new SqlCommand (commandstring, connection); Connection. Open (); SqlDataReader reader = command. ExecuteReader (); if (reader. Read ()) {Tbxusername.text = convert.tostring (reader["Name"]); Cbxgroup.text = convert.tostring (reader["Groups"]); Tbxtel.text = convert.tostring (reader["Phone"]); Tbxworkunit.text = convert.tostring (reader["Workunit"]); Tbxemail.text = convert.tostring (reader["Email"]); Tbxqq.text = convert.tostring (reader["QQ"]); if (reader["picture"] = = DBNull.Value) {pbxpicture.i Mage = TongXunLu.Properties.Resources.defaultPix; } else {//string a=reader["picture"]. ToString (); Byte[] B = (byte[]) ((reader["Picture"])); MemoryStream buf = new MemoryStream ((byte[]) reader["picture"]); Image image = Image.fromstream (BUF); Bitmap BT = new Bitmap (image); Pbxpicture.image = BT; Pbxpicture.image = Image; Pbxpicture.image = Image.fromstream (new MemoryStream (b));//read out the binary stream?? Problem} reader. Close (); Connection. Close (); }} catch (Exception ex) { MessageBox.Show (ex. Message); } }
While reading the data, some errors may occur while writing the data.
Sure enough, I was searching for an array of byte[] when I found that there were more than 20,000 reads when there were only 50.
So I thought maybe there was something wrong at the time of writing, so
COMMANDINSERT.PARAMETERS.ADD ("@Picture", sqldbtype.image,50);
Change to CommandInsert.Parameters.Add ("@Picture", sqldbtype.image);
The photos are displayed properly.
Reference to the http://blog.csdn.net/zystory/article/details/4399338
There's something wrong with your data, and it doesn't matter what you read.
New SqlParameter ("@L_RolePic", Sqldbtype.image, 16) changed to New SqlParameter ("@L_RolePic", Sqldbtype.image),