如何将文件以二进制保存到数据库

yardyg 2003-11-14 04:22:15
如何将文件以二进制保存到数据库,有如何读出来,请帮帮小弟我搞了一天了头昏脑胀真的需要你提供下相关参考资料,当然源代码更好
...全文
60 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yardyg 2003-11-14
  • 打赏
  • 举报
回复
谢谢
jianglinchun 2003-11-14
  • 打赏
  • 举报
回复
给你一段例子代码,上传图片进入数据库的,不懂可以继续提问:
private void UploadImage_Click(object sender, System.EventArgs e)
{
/// <summary>
/// ContentType holds the content type of the file that was added to the ImageToUpload
/// control, and HTML input control. </summary>
string ContentType = ImageToUpload.PostedFile.ContentType;
/// <summary>
/// Length holds the size of the file that was added to the ImageToUpload control </summary>
int Length = System.Convert.ToInt32(ImageToUpload.PostedFile.InputStream.Length);

/// <summary>
/// Content will hold the image. It is a byte array of size Length </summary>
byte[] Content = new byte[Length];

/// <summary>
/// The Read method is used to read the file from the ImageToUpload control </summary>
ImageToUpload.PostedFile.InputStream.Read(Content,0,Length);

/// <summary>
/// Open a connection to the SQL Server </summary>
SqlConnection Connection = new SqlConnection("server=localhost;uid=sa;pwd=;database=ImageUpload");
/// <summary>
/// The SqlCommand will be used to insert the image into the Images table </summary>
SqlCommand Command = new SqlCommand("INSERT Into Images(Description, ImageFile, ImageSize, ImageType) Values(@Description, @ImageFile, @ImageSize, @ImageType)", Connection);

/// <summary>
/// The Description parameter is used to add the image file description to the database
SqlParameter imageDescriptionParameter = new SqlParameter("@Description", SqlDbType.NVarChar);
imageDescriptionParameter.Value = imageDescription.Text;
Command.Parameters.Add(imageDescriptionParameter);

/// <summary>
/// The ImageFile parameter is used to add the image file to the database
SqlParameter imageFileParameter = new SqlParameter("@ImageFile", SqlDbType.Image);
imageFileParameter.Value = Content;
Command.Parameters.Add(imageFileParameter);

/// <summary>
/// The ImageSize parameter is used to add the image file size to the database
SqlParameter imageSizeParameter = new SqlParameter("@ImageSize", SqlDbType.Int);
imageSizeParameter.Value = Length;
Command.Parameters.Add(imageSizeParameter);

/// <summary>
/// The ImageType parameter is used to add the image file type to the database
SqlParameter imageTypeParameter = new SqlParameter("@ImageType", SqlDbType.NVarChar);
imageTypeParameter.Value = ContentType;
Command.Parameters.Add(imageTypeParameter);

/// <summary>
/// Open the connection in order to retrieve the record </summary>
Connection.Open();
/// <summary>
/// The SQL statement is executed. ExecuteNonQuery is used since no records
/// will be returned. </summary>
Command.ExecuteNonQuery();
/// <summary>
/// The connection is closed </summary>
Connection.Close();

}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧