使用C#(winforms)将图像上传(保存)到SqlServer2008数据库 [英] uploading(saving) image to SqlServer2008 DB using C#(winforms)

查看:91
本文介绍了使用C#(winforms)将图像上传(保存)到SqlServer2008数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Winforms(C#)将图像上传到sqlserver2008



i am trying to Upload an image into sqlserver2008 using Winforms(C#)

// sourcecode

string strFileFullPath="this is my image location";

in Database i took Varbinary(max) as DataType

FileStream stream = new FileStream(strFileFullPath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);

byte[] photo = reader.ReadBytes((int)stream.Length);




// sourcecode
Error : Failed to convert parameter value from a String to a Byte[].



你能来吗请帮帮我怎样才能解决这个问题



提前谢谢


can you please help me how can i solve this issue

Thanks in advance

推荐答案

你好试试这段代码。如果您发现任何简单的解决方案,请回复我。谢谢...



Hello try this code. If you find any easy solution than of mine then please reply me. Thanks...

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = open.FileName;
            }
            cn.Open();
            string image = textBox1.Text;
            Bitmap bmp = new Bitmap(image);
            FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
            byte[] bimage = new byte[fs.Length];
            fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            SqlCommand cmd = new SqlCommand("insert into ImageUpload(imgdata) values(@imgdata)",cn);
            cmd.Parameters.AddWithValue("@imgdata",SqlDbType.Image).Value=bimage;
            cmd.ExecuteNonQuery();
            cn.Close();
        }


这篇关于使用C#(winforms)将图像上传(保存)到SqlServer2008数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆