上传图片到数据库和从数据库下载下来

None.gif private   void  UploadFile()
None.gif           
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////得到用户要上传的文件名
InBlock.gif            string strFilePathName = loFile.PostedFile.FileName;
InBlock.gif            
string strFileName = Path.GetFileName(strFilePathName);
InBlock.gif            
int FileLength = loFile.PostedFile.ContentLength;
InBlock.gif               
InBlock.gif            
if(FileLength<=0)
InBlock.gif                
return;
InBlock.gif       
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////上传文件
InBlock.gif            //                    try
InBlock.gif            
//                    
InBlock.gif            
//                    {            
ExpandedSubBlockStart.gifContractedSubBlock.gif
            /**//**//**////图象文件临时储存Byte数组
InBlock.gif            Byte[] FileByteArray = new Byte[FileLength];
InBlock.gif                    
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////建立数据流对像
InBlock.gif            Stream StreamObject = loFile.PostedFile.InputStream; 
InBlock.gif                    
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
InBlock.gif            StreamObject.Read(FileByteArray,0,FileLength); 
InBlock.gif                   
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////建立SQL Server链接
InBlock.gif            //                    string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
InBlock.gif
            SqlConnection Con = new SqlConnection("server=.;uid=sa;pwd=sa;database=wjoa");
InBlock.gif            String SqlCmd 
= "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
InBlock.gif            SqlCommand CmdObj 
= new SqlCommand(SqlCmd, Con);
InBlock.gif            CmdObj.Parameters.Add(
"@Image",SqlDbType.Image, FileLength).Value = FileByteArray;
InBlock.gif            CmdObj.Parameters.Add(
"@ContentType", SqlDbType.VarChar,2000).Value = loFile.PostedFile.ContentType; //记录文件类型
InBlock.gif
                     
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////把其它单表数据记录上传
InBlock.gif            CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,2000).Value = tbDescription.Text;
InBlock.gif                           
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////记录文件长度,读取时使用
InBlock.gif            CmdObj.Parameters.Add("@ImageSize", SqlDbType.Int).Value = FileLength;
InBlock.gif            Con.Open();
InBlock.gif            CmdObj.ExecuteNonQuery(); 
InBlock.gif            Con.Close();
InBlock.gif                    
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////跳转页面
InBlock.gif            //                        Response.Redirect("ShowAll.aspx");
InBlock.gif            
//                    }
InBlock.gif            
//               
InBlock.gif            
//                    catch(Exception ex)
InBlock.gif            
//                
InBlock.gif            
//                        {
InBlock.gif            
//                                throw ex;
InBlock.gif            
//                        }
ExpandedBlockEnd.gif
        }

None.gif
None.gif        
private   void  ShowImages()
None.gif            
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////ID为图片ID
InBlock.gif            int ImgID = int.Parse(this.TextBox1.Text.Trim());  
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////建立数据库连接
InBlock.gif           
InBlock.gif            SqlConnection Con 
= new SqlConnection("server=.;uid=sa;pwd=sa;database=wjoa");
InBlock.gif            String SqlCmd 
= "SELECT * FROM ImageStore WHERE id = @ImageID";
InBlock.gif            SqlCommand CmdObj 
= new SqlCommand(SqlCmd, Con);
InBlock.gif            CmdObj.Parameters.Add(
"@ImageID", SqlDbType.Int).Value = ImgID;
InBlock.gif           
InBlock.gif            Con.Open();
InBlock.gif            SqlDataReader SqlReader 
= CmdObj.ExecuteReader();
InBlock.gif            SqlReader.Read(); 
InBlock.gif       
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////设定输出文件类型
InBlock.gif            Response.ContentType = (string)SqlReader["ImageContentType"];
InBlock.gif           
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**////输出图象文件二进制数制
InBlock.gif            Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]); 
InBlock.gif            Response.End();
InBlock.gif       
InBlock.gif            Con.Close();    
ExpandedBlockEnd.gif        }

None.gif

转载于:https://www.cnblogs.com/gjahead/archive/2006/06/24/434528.html

好的,我可以帮你解决这个问题。 首先,让我们来看一下如何将图片保存数据库中。假设你有一个名为`image`的`byte[]`数组,它包含了你要保存图片数据。以下是一个例子,用于将这个数组保存到 SQL Server 数据库中的一个名为`image_table`的表中: ```csharp using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = "INSERT INTO image_table (image_data) VALUES (@ImageData)"; SqlCommand command = new SqlCommand(sql, connection); SqlParameter imageDataParam = new SqlParameter("@ImageData", SqlDbType.VarBinary, -1); imageDataParam.Value = image; command.Parameters.Add(imageDataParam); command.ExecuteNonQuery(); } ``` 上面的代码中,`connectionString`是你的数据库连接字符串,`image_table`是你要保存图片的表名,`image_data`是你要保存图片的列名。 接下来是如何从数据库中读取图片并显示到窗体上。假设你有一个名为`pictureBox1`的`PictureBox`控件,用于显示图片。以下是一个例子,用于从 SQL Server 数据库中的`image_table`表中读取图片数据,并将其显示到`pictureBox1`上: ```csharp using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = "SELECT image_data FROM image_table WHERE id = @Id"; SqlCommand command = new SqlCommand(sql, connection); SqlParameter idParam = new SqlParameter("@Id", SqlDbType.Int); idParam.Value = id; command.Parameters.Add(idParam); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { byte[] image = (byte[])reader["image_data"]; MemoryStream stream = new MemoryStream(image); Image img = Image.FromStream(stream); pictureBox1.Image = img; } } ``` 上面的代码中,`id`是你要读取图片的行的 ID。`pictureBox1`是你要显示图片的控件。 希望这些代码可以帮助你实现图片上传和显示的功能。如果你有其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值