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

相关文章

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

Nginx安全防护的多种方法

《Nginx安全防护的多种方法》在生产环境中,需要隐藏Nginx的版本号,以避免泄漏Nginx的版本,使攻击者不能针对特定版本进行攻击,下面就来介绍一下Nginx安全防护的方法,感兴趣的可以了解一下... 目录核心安全配置1.编译安装 Nginx2.隐藏版本号3.限制危险请求方法4.请求限制(CC攻击防御)

MySQL 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更