的WinForms:使用C#如何上传图片到SQL Server数据库 [英] Winforms: How to upload a picture into SQL Server database using C#

查看:178
本文介绍了的WinForms:使用C#如何上传图片到SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上传图片到我的SQL Server数据库。我有两个按钮,一个图片框。随着浏览按钮,我能够从磁盘上的文件,并显示到PictureBox。

I want to upload images to my SQL Server database. I have two buttons , One picture box. With the browse button, I am able to select the file from disk, and it is displayed into the picturebox.

现在的问题是,我无法从图片框将图片保存到数据库中。

The problem is that I am not able to save the picture from picturebox into the database.

请帮助我的代码。欣赏它。

Please help me out with the code. Appreciate it.

推荐答案

您可以直接从它的路径保存图像(您有它的话)。

You can save the image directly from its path (you have it already).

试试这个:

    byte[] img = File.ReadAllBytes("your image path");

    mySqlCommand = "INSERT INTO MyTable(Image) VALUES(@Image)";//mySqlCommand is a SqlCommand, and @Image is a parameter 
    mySqlCommand.Parameters.AddWithValue("@Image", img);
    mySqlCommand.ExecuteNonQuery();

这篇关于的WinForms:使用C#如何上传图片到SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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