【发布时间】:2011-04-02 16:20:53
【问题描述】:
我想用 C# 将用户图像保存到数据库中。我该怎么做?
【问题讨论】:
标签: c# sql asp.net-mvc ado.net
我想用 C# 将用户图像保存到数据库中。我该怎么做?
【问题讨论】:
标签: c# sql asp.net-mvc ado.net
试试这个方法。当您要存储图像的字段为byte 类型时,它应该可以工作。
首先它为图像创建byte[]。然后它使用binary 类型的IDataParameter 将其保存到数据库中。
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
public static void PerisitImage(string path, IDbConnection connection)
{
using (var command = connection.CreateCommand ())
{
Image img = Image.FromFile (path);
MemoryStream tmpStream = new MemoryStream();
img.Save (tmpStream, ImageFormat.Png); // change to other format
tmpStream.Seek (0, SeekOrigin.Begin);
byte[] imgBytes = new byte[MAX_IMG_SIZE];
tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);
command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
IDataParameter par = command.CreateParameter();
par.ParameterName = "payload";
par.DbType = DbType.Binary;
par.Value = imgBytes;
command.Parameters.Add(par);
command.ExecuteNonQuery ();
}
}
【讨论】:
DbType.Binary 处理 varbinary(MAX),即使帮助工具提示说它限制为 8000 字节。
您需要在 C# 中将图像转换为 byte[],然后您的数据库列将是 varbinary(MAX)
之后,就像保存任何其他数据类型一样。
【讨论】:
这是在asp.net中使用FileUpload控件的方法:
byte[] buffer = new byte[fu.FileContent.Length];
Stream s = fu.FileContent;
s.Read(buffer, 0, buffer.Length);
//Then save 'buffer' to the varbinary column in your db where you want to store the image.
【讨论】:
您需要将图像序列化为可以存储在 SQL BLOB 列中的二进制格式。假设您使用的是 SQL Server,这里有一篇关于这个主题的好文章:
【讨论】:
我个人的偏好是不将图像保存到数据库中。将图像保存在文件系统中的某个位置,并在数据库中保存参考。
【讨论】:
您可以将图像的路径保存在数据库中或将图像本身保存为 BLOB(二进制 - 字节数组)。这取决于您的情况,如果您的应用程序是 Web 应用程序,则保存图像的路径要好得多。但是如果您有一个基于客户端的应用程序连接到一个集中式数据库,那么您必须将其保存为二进制文件。
【讨论】:
由于您使用的是 SQL,因此建议您不要使用 adhoc(“在字符串中编写语句”),特别是考虑到您正在加载图像。
ADO.NET 可以为您完成映射、转义等所有繁重的工作。
要么创建存储过程,要么使用 SqlParameter 进行绑定。
正如其他海报所说,使用 VARBINARY(MAX) 作为您的存储类型 - IMAGE 正在被弃用。
【讨论】:
我认为这里已经回答了这个有效的问题。我也试过了。我的问题只是使用图片编辑(来自 DevExpress)。这就是我解决它的方法:
再次感谢您。查格伯特
【讨论】:
//Arrange the Picture Of Path.***
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
string[] PicPathArray;
string ArrangePathOfPic;
PicPathArray = openFileDialog1.FileName.Split('\\');
ArrangePathOfPic = PicPathArray[0] + "\\\\" + PicPathArray[1];
for (int a = 2; a < PicPathArray.Length; a++)
{
ArrangePathOfPic = ArrangePathOfPic + "\\\\" + PicPathArray[a];
}
}
// Save the path Of Pic in database
SqlConnection con = new SqlConnection("Data Source=baqar-pc\\baqar;Initial Catalog=Prac;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("insert into PictureTable (Pic_Path) values (@Pic_Path)", con);
cmd.Parameters.Add("@Pic_Path", SqlDbType.VarChar).Value = ArrangePathOfPic;
cmd.ExecuteNonQuery();
***// Get the Picture Path in Database.***
SqlConnection con = new SqlConnection("Data Source=baqar-pc\\baqar;Initial Catalog=Prac;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Pic_Path where ID = @ID", con);
SqlDataAdapter adp = new SqlDataAdapter();
cmd.Parameters.Add("@ID",SqlDbType.VarChar).Value = "1";
adp.SelectCommand = cmd;
DataTable DT = new DataTable();
adp.Fill(DT);
DataRow DR = DT.Rows[0];
pictureBox1.Image = Image.FromFile(DR["Pic_Path"].ToString());
【讨论】: