从 C# 问题在 SQL 数据库中保存和检索图像

发布于 2024-09-02 02:14:24 字数 1568 浏览 6 评论 0原文

我使用此代码在数据库中的人员表中插入记录,

System.IO.MemoryStream ms = new System.IO.MemoryStream();
                Image img = Image_Box.Image;
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                this.personTableAdapter1.Insert(NIC_Box.Text.Trim(), Application_Box.Text.Trim(), Name_Box.Text.Trim(), Father_Name_Box.Text.Trim(), DOB_TimePicker.Value.Date, Address_Box.Text.Trim(), City_Box.Text.Trim(), Country_Box.Text.Trim(), ms.GetBuffer());

但是当我使用此代码

    byte[] image = (byte[])Person_On_Application.Rows[0][8];
                        MemoryStream Stream = new MemoryStream();
                        Stream.Write(image, 0, image.Length);
                        Bitmap Display_Picture = new Bitmap(Stream);

Image_Box.Image = Display_Picture;

检索此记录时它工作得很好,但如果我用自己生成的查询更新它,就像

System.IO.MemoryStream ms = new System.IO.MemoryStream();
                Image img = Image_Box.Image;
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                Query = "UPDATE Person SET Person_Image ='"+ms.GetBuffer()+"'  WHERE (Person_NIC = '"+NIC_Box.Text.Trim()+"')";

下次我使用相同的代码来检索图像并按上面使用的方式显示它一样。程序抛出异常

替代文本 http://www.freeimagehosting.net/image.php?3bb5f0d4b3.jpg][img]http://www.freeimagehosting.net/uploads/th.3bb5f0d4b3.jpg

所以谁能告诉我为什么我无法弄清楚。

I used this code for inserting records in a person table in my DB

System.IO.MemoryStream ms = new System.IO.MemoryStream();
                Image img = Image_Box.Image;
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                this.personTableAdapter1.Insert(NIC_Box.Text.Trim(), Application_Box.Text.Trim(), Name_Box.Text.Trim(), Father_Name_Box.Text.Trim(), DOB_TimePicker.Value.Date, Address_Box.Text.Trim(), City_Box.Text.Trim(), Country_Box.Text.Trim(), ms.GetBuffer());

but when i retrieve this with this code

    byte[] image = (byte[])Person_On_Application.Rows[0][8];
                        MemoryStream Stream = new MemoryStream();
                        Stream.Write(image, 0, image.Length);
                        Bitmap Display_Picture = new Bitmap(Stream);

Image_Box.Image = Display_Picture;

it works perfectly fine but if i update this with my own generated Query like

System.IO.MemoryStream ms = new System.IO.MemoryStream();
                Image img = Image_Box.Image;
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                Query = "UPDATE Person SET Person_Image ='"+ms.GetBuffer()+"'  WHERE (Person_NIC = '"+NIC_Box.Text.Trim()+"')";

the next time i use the same code for retrieving the image and displaying it as used above . Program throws an exception

alt text http://www.freeimagehosting.net/image.php?3bb5f0d4b3.jpg][img]http://www.freeimagehosting.net/uploads/th.3bb5f0d4b3.jpg

So can any one tell me why i can't figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末 2024-09-09 02:14:24
Query = "UPDATE Person SET Person_Image =@pic  WHERE (Person_NIC = '" + NIC_Box.Text.Trim() + "')";
                    SqlCommand cmd = new SqlCommand(Query);
                    SqlParameter picparameter = new SqlParameter();
                    picparameter.SqlDbType = SqlDbType.Image;
                    picparameter.ParameterName = "pic";
                    picparameter.Value = ms.GetBuffer();
                    cmd.Parameters.Add(picparameter);

                    db.ExecuteSQL(Query);

这东西与这个代码一起工作万岁哈哈

Query = "UPDATE Person SET Person_Image =@pic  WHERE (Person_NIC = '" + NIC_Box.Text.Trim() + "')";
                    SqlCommand cmd = new SqlCommand(Query);
                    SqlParameter picparameter = new SqlParameter();
                    picparameter.SqlDbType = SqlDbType.Image;
                    picparameter.ParameterName = "pic";
                    picparameter.Value = ms.GetBuffer();
                    cmd.Parameters.Add(picparameter);

                    db.ExecuteSQL(Query);

This thing worked with this code hurray lolz

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文