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))
                {
                    //获取当前的数据在表中的ID
                    SqlCommand commandInsert = new SqlCommand();
                    commandInsert.Connection = connection;

                    //connection.Close();
                    connection.Open();
                    //插入数据
                    commandInsert.CommandText = "USE db_TXL UPDATE tb_BusicInfo set Groups= @Groups ,[email protected],[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的类型不能写错!!

                    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方法提示参数错误问题解决

时间: 2024-08-13 00:05:36

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

win7使用arp-d提示参数错误的解决办法。

在win7环境下,使用arp -d命令出现无法删除参数错误的解决办法.注意:是提示参数错误,不是提示权限需要提升 1.先运行:netsh i i show in 找到正在使用的网卡idx号 2.绑定ARP:netsh -c i i add neighbors 11 192.168.1.1 00-21-27-bc-89-48 就可以进行绑定了,这里11是idx号. 3.解绑ARP:netsh -c i i delete neighbors 11

phpStudy启动失败时的解决方法 提示缺vc9运行库

php5.3.5.4和apache都是用vc9编译,电脑必须安装vc9运行库才能运行. php5.5.5.6是vc11编译,如用php5.5.5.6必须安装vc11运行库. php7.0.7.1是vc14编译,如用php7.0.7.1必须安装vc14运行库. vc9和vc11运行库下载:https://pan.baidu.com/s/1dF9LslV 密码:v7ap php5.5以上才有64位的,其他均为32位.所以64位的系统最好把32位的运行库也安装上. 如果您下载的是32位的phpStud

机械硬盘提示参数错误文件怎么找回

F盘打不开参数错误,是因为这个I盘的文件系统内部结构损坏导致的.要恢复里面的数据就必须要注意,这个盘不能格式化,否则数据会进一步损坏.具体的恢复方法看正文 工具/软件:星空数据恢复软件 步骤1:先下载并解压软件运行后,直接双击需要恢复的分区,接着右击软件图标选择<以管理员身份运行>(如果是xp系统可以直接双击打开软件)步骤2:程序运行后,直接双击需要恢复的分区步骤3:软件会很快将找出的数据,放到与要恢复盘同名的目录中步骤4:将需要恢复的数据勾选,接着点右上角的保存,<文件保存>按钮

鼠标悬浮图片时弹出透明提示图层的jQuery特效

源码: <!doctype html> <html class="no-js" lang="en"> <head> <meta charset="utf-8"> <title>CollagePlus for jQuery Example</title> <link rel="stylesheet" type="text/css"

Image.FromStream(ms) 提示参数无效

说明ms有问题,首先确保有读到数据,这种情况是保存到库的时候出错的. 原来你可能是这样写的: MemoryStream stream = new MemoryStream();PictureBox1.Image.Save(stream, ImageFormat.Bmp ); //这里出错,原图片格式是JPEGbyte [] bytestream = new byte [ stream.Length ];stream .Read (bytestream ,0,bytestream .Length

有效解决Android加载大图片时内存溢出的问题

首先解析一下基本的知识: 位图模式,bitmap颜色位数是1位 灰度模式,bitmap颜色位数是8位,和256色一样 RGB模式,bitmap颜色位数是24位 在RGB模式下,一个像素对应的是红.绿.蓝三个字节 CMYK模式,bitmap颜色位数是32位  在CMYK模式下,一个像素对应的是青.品.黄.黑四个字节 图像文件的字节数(Byte) = 图像分辨率*颜色深度/8(bit/8) 例如:一幅640*480图像分辨率.RGB色一般为24位真彩色,图像未经压缩的数据容量为:640X480X24

SQL 2005 Errolog过大处理方法

1.    背景 1.1          在SQL数据库系列产品中,运行一段时间后的数据库总会生成不小的错误日志,经常会占用不少空间: 2.    原因 2.1          SQL数据库默认情况下,错误日志位于 Program Files/Microsoft SQL Server/MSSQL.n/MSSQL/LOG/ERRORLOG 和ERRORLOG.n 文件中.默认保留有7个 SQL Server 错误日志文件,分别是:ErrorLog,Errorlog.1-Errorlog.6 ,

安装SQL Server 2008时,提示“重启计算机失败”的解决方法

安装SQL Server 2008时,提示“重启计算机失败”,如何解决呢? (网络借图) 解决方法: 注册表找到如下位置:“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager” 右侧选择删除“PendingFileRenameOperations”. 注意:删除前,最好将此节点导出备份.已备不测.

SQL Server2008安装时提示重启计算机失败怎么办?

安装SQL Server 2008时,经常会遇到这样一个问题,软件提示“重启计算机失败”,如果忽略的话,会给后面的安装带来很大的麻烦,这里如何解决呢? 解决方法 1.在键盘上按下组合键[Win]+[R],调出运行窗口. 2.在窗口中输入“regedit”,点击确定,打开注册表管理界面. 3.在注册表左侧目录栏中找到如下位置:“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager”,然后在右侧选择删除“Pending