如何将图像插入数据库
本文关键字:插入 数据库 图像 | 更新日期: 2023-09-27 18:00:51
public void InsertAnImage(Guid i)
{
StringBuilder sb = new StringBuilder();
sb.Append("");
Stream stream = FileUpload1.FileContent;
StreamReader reader = new StreamReader(stream);
string myConnectionString = AllQuestionsPresented.connectionString;
using (SqlConnection conn = new SqlConnection(AllQuestionsPresented.connectionString))
{
// sample query with parameters to insert into db
string sqlQuery = "INSERT INTO [UserProfile] (UserID, Picture) Values (@userId, @picture)";
// conn is your db connection
SqlCommand command = new SqlCommand(sqlQuery, conn);
// creating parameters
SqlParameter paramId = new SqlParameter("@userId", SqlDbType.Int, 4);
paramId.Value = 45;
// you picture parameter, and assigning its the value
SqlParameter paramPicture = new SqlParameter("@picture", SqlDbType.Binary, myImage.Length);// red line here
paramPicture.Value = myImage;// red line here
// adding params to command
command.Parameters.Add(paramId);
command.Parameters.Add(paramPicture);
// then execute your command
command.ExecuteNonQuery();
}
}
如何将流读取器而不是文件流读取器放入数据库?
下面是另一篇文章:
使用ASP.NET 2.0和ASP.NET 3.5 从数据库中保存和检索图像
然而,普遍的共识是,最好将图像存储在文件系统中。