sql读写图片时Image.FromStream方法提示参数错误问题解决

本文主要是介绍sql读写图片时Image.FromStream方法提示参数错误问题解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们通常这么写

using (SqlDataReader drm = sqlComm.ExecuteReader()){drm.Read();//以下把数据库中读出的Image流在图片框中显示出来.MemoryStream ms = new MemoryStream((byte[])drm["Logo"]);Image img = Image.FromStream(ms);this.pictureBox1.Image = img;}


 

 

我的写数据

        private void btnOK_Click(object sender, EventArgs e){string name = "";if (tbxUserName.Text.Trim() == ""){MessageBox.Show("姓名不能为空,请重新输入!", "提示");return;}else{name = tbxUserName.Text.Trim();}string group = "";if (cbxGroup.Text.Trim() == ""){group = "未分组";}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);//类型为打开数据 权限为只读 不呢写数据int length = Convert.ToInt32(fs.Length);b = new byte[length];fs.Read(b, 0, length);//数据读入b数组中 从0号到length位fs.Close();//关闭输入输出流}catch (Exception ex){b = null;MessageBox.Show(ex.Message);}}else {b = pixData;}try{using (SqlConnection connection = new SqlConnection(connectionString)){//获取当前的数据在表中的IDSqlCommand commandInsert = new SqlCommand();commandInsert.Connection = connection;//connection.Close();connection.Open();//插入数据commandInsert.CommandText = "USE db_TXL UPDATE tb_BusicInfo set Groups= @Groups ,Name=@Name,WorkUnit=@WorkUnit,Phone=@Phone,Email=@Email,QQ=@QQ,Picture=@Picture";commandInsert.CommandText += "  where ID=@ID ";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的类型不能写错!!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["@Picture"].Value = DBNull.Value;//DBNull  不存在的值NULL}else{commandInsert.Parameters["@Picture"].Value = b;}commandInsert.ExecuteNonQuery();connection.Close();DialogResult = DialogResult.OK;}}catch (Exception ex) { MessageBox.Show(ex.Message); }}


 

我的读数据

     private void frmEdit_Load(object sender, EventArgs e){try{using (SqlConnection connection = new SqlConnection(connectionString)){string commandString = "select * 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.Image = 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));//把二进制流读出来??问题}}reader.Close();connection.Close();}}catch (Exception ex){MessageBox.Show(ex.Message);}}


 

读数据的时候,但是在写数据的时候可能发生了一些错误。

果然我在追寻byte[]的数组的时候发现 写的时候有2万多个 读的时候只有50个

于是我想可能是写的时候出了问题,于是

 

commandInsert.Parameters.Add("@Picture", SqlDbType.Image,50);

改为commandInsert.Parameters.Add("@Picture", SqlDbType.Image);

照片可以正常显示了

参考了http://blog.csdn.net/zystory/article/details/4399338

你存数据的时候出了问题,和读数据没有关系
new SqlParameter("@L_RolePic", SqlDbType.Image, 16) 改为 new SqlParameter("@L_RolePic", SqlDbType.Image),

这篇关于sql读写图片时Image.FromStream方法提示参数错误问题解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/988302

相关文章

Spring三级缓存解决循环依赖的解析过程

《Spring三级缓存解决循环依赖的解析过程》:本文主要介绍Spring三级缓存解决循环依赖的解析过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、循环依赖场景二、三级缓存定义三、解决流程(以ServiceA和ServiceB为例)四、关键机制详解五、设计约

史上最全nginx详细参数配置

《史上最全nginx详细参数配置》Nginx是一个轻量级高性能的HTTP和反向代理服务器,同时也是一个通用代理服务器(TCP/UDP/IMAP/POP3/SMTP),最初由俄罗斯人IgorSyso... 目录基本命令默认配置搭建站点根据文件类型设置过期时间禁止文件缓存防盗链静态文件压缩指定定错误页面跨域问题

解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException: org.junit.Test问题

《解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException:org.junit.Test问题》:本文主要介绍解决tomcat启动时报Junit相... 目录tomcat启动时报Junit相关错误Java.lang.ClassNotFoundException

Linux搭建单机MySQL8.0.26版本的操作方法

《Linux搭建单机MySQL8.0.26版本的操作方法》:本文主要介绍Linux搭建单机MySQL8.0.26版本的操作方法,本文通过图文并茂的形式给大家讲解的非常详细,感兴趣的朋友一起看看吧... 目录概述环境信息数据库服务安装步骤下载前置依赖服务下载方式一:进入官网下载,并上传到宿主机中,适合离线环境

解决Maven项目报错:failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题

《解决Maven项目报错:failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题》这篇文章主要介... 目录Maven项目报错:failed to execute goal org.apache.maven.pl

nginx负载均衡及详细配置方法

《nginx负载均衡及详细配置方法》Nginx作为一种高效的Web服务器和反向代理服务器,广泛应用于网站的负载均衡中,:本文主要介绍nginx负载均衡及详细配置,需要的朋友可以参考下... 目录一、 nginx负载均衡策略1.1 基本负载均衡策略1.2 第三方策略1.3 策略对比二、 nginx配置2.1

Java调用Python的四种方法小结

《Java调用Python的四种方法小结》在现代开发中,结合不同编程语言的优势往往能达到事半功倍的效果,本文将详细介绍四种在Java中调用Python的方法,并推荐一种最常用且实用的方法,希望对大家有... 目录一、在Java类中直接执行python语句二、在Java中直接调用Python脚本三、使用Run

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

MySQL主从同步延迟问题的全面解决方案

《MySQL主从同步延迟问题的全面解决方案》MySQL主从同步延迟是分布式数据库系统中的常见问题,会导致从库读取到过期数据,影响业务一致性,下面我将深入分析延迟原因并提供多层次的解决方案,需要的朋友可... 目录一、同步延迟原因深度分析1.1 主从复制原理回顾1.2 延迟产生的关键环节二、实时监控与诊断方案

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField