上传图片

时间:2009-09-28 09:17:58

标签: c#

美好的一天

创建库存系统

在sql中创建了一个db,在visual c#中,我创建了一个表单,用于输入产品并允许用户上传项目的图像,我该如何去做?

我是一名c ++程序员,刚进入visual c#

由于

2 个答案:

答案 0 :(得分:0)

以上是关于上传文件的older post on uploading

答案 1 :(得分:0)

假设您还想将图像保存在数据库中......您需要允许用户从他/她自己的硬盘中选择一个图像(打开文件对话框),然后读取字节并发送它们到数据库(ADO.NET的DbCommand)。 ADO.NET支持BLOBS的流。

以下是“打开文件”对话框的示例:

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "images (*.png)|*.png|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (Stream myStream = openFileDialog1.OpenFile())
                    {
                        if (myStream != null)
                        {
                            // do something with the stream bytes here....
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }