Skip to main content
 首页 » 编程设计

c#之将图像文件保存到数据库中

2024年02月23日15duanxz

在用户使用 asp.net 中的 fileupload 控件选择文件位置后,如何使用 c# 将文件保存到 SQL Server 数据库中。

请您参考如下方法:

你可以尝试这样的事情:

if (this.fileUploader.PostedFile == null || 
    this.fileUploader.PostedFile.ContentLength < 1) 
{ 
   this.LabelError.Text = this.GetGlobalResourceObject("Messages", "NoFileToUpload") 
                              .ToString(); 
   return; 
} 
 
MyTableWithImageField i = new MyTableWithImageField(); 
 
i.ImageData = this.fileUploader.FileBytes; 
 
command.CommandText = @"InsertMyTableWithImageField"; 
command.CommandType = CommandType.StoredProcedure; 
 
command.Parameters.AddWithValue("@ImageData", i.ImageData); 

您可能还想从 MSDN 中查看:Uploading Files in ASP.NET 2.0