将文件以二进制形式存储到Sql Server中

ContractedBlock.gif ExpandedBlockStart.gif /DatSet读取文件/ #region /DatSet读取文件/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据aaId,使用DataSet 读取**文件内容,还原恢复。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ioId">**的ID号</param>
InBlock.gif        
/// <param name="tableName">表名</param>
ExpandedSubBlockEnd.gif        
/// <param name="fieldName">存储**文件内容的字段</param>

InBlock.gif        public bool ReadFileOutFormSqlServer(int ioId,string tableName,string fieldName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool flag=false;
InBlock.gif
InBlock.gif            
object tempfileName=null;
InBlock.gif
InBlock.gif            
string strSql="select fullpath,"+fieldName+" from "+tableName+" where infoID="+infoId;
InBlock.gif
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif    
InBlock.gif            Database database
=new Database(p_sqlConnString);
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ds
=database.DBDataSet(strSql);
InBlock.gif            
InBlock.gif                
byte[] tmpFile=null;  
InBlock.gif                
foreach(DataRow dr in ds.Tables[0].Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(dr[0]!=DBNull.Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        tempfileName
=dr[0];
InBlock.gif                        tmpFile
=(byte[])dr[1];
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                ds.Dispose();
InBlock.gif
InBlock.gif
InBlock.gif                
if(tmpFile!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                
InBlock.gif                    
string fileName=tempfileName.ToString();
InBlock.gif                    FileInfo fi
=new System.IO.FileInfo(fileName);
InBlock.gif                    
if(!fi.Exists)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//新建文件
InBlock.gif
                        using (FileStream fstream= fi.Create()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
int length=tmpFile.Length;
InBlock.gif                            fstream.Write(tmpFile,
0,tmpFile.Length);
InBlock.gif                            fstream.Close();
InBlock.gif                            flag
=true;
ExpandedSubBlockEnd.gif                        }

InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
using(FileStream fs=fi.OpenWrite())
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
int length=tmpFile.Length;
InBlock.gif                            fs.Write(tmpFile,
0,tmpFile.Length);
InBlock.gif                            fs.Close();
InBlock.gif                            flag
=true;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif            
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw err;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                database.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion


ContractedBlock.gif ExpandedBlockStart.gif /***保存文件***/ #region /***保存文件***/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 保存文件到SQL Server数据库中
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="RRR">**文件类</param>
InBlock.gif        
/// <param name="tableName">表名</param>
ExpandedSubBlockEnd.gif        
/// <param name="fieldName">存储**文件内容的字段</param>

InBlock.gif        public bool SaveIntoSqlServer(RRR accredit,string tableName,string fieldName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool flag=false;
InBlock.gif            
//保存文件到SQL Server数据库中
InBlock.gif
            string fileName=accredit.FullPath;
InBlock.gif
InBlock.gif            FileInfo fi
=new FileInfo(fileName);
InBlock.gif            
if (fi.Exists)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlConnection cn
=null;
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
byte[] dData=null;
InBlock.gif            
InBlock.gif                    
//读文件,放入buffer
InBlock.gif
                    using(FileStream fs=fi.OpenRead())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        dData
=new byte[fi.Length];
InBlock.gif                        
int nReadLength=fs.Read(dData,0,(int)(fi.Length));
InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
string sqlStr="insert into "+tableName+"(ioID,path,cTime,"+fieldName+") values("
InBlock.gif                        
+accredit.IoID+",'"+accredit.Path+"','"+accredit.CTime+"',@file)";
InBlock.gif
InBlock.gif                    cn
=ConnectToSqlSever();
InBlock.gif                    
if(cn.State==0) cn.Open();
InBlock.gif                    SqlCommand cm
=new SqlCommand(sqlStr,cn);
InBlock.gif                    cm.Parameters.Add(
"@file",dData);
InBlock.gif                    cm.ExecuteNonQuery();
InBlock.gif                    cm.Dispose();
InBlock.gif                    flag
=true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw err;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    cn.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedBlockEnd.gif        
#endregion

数据字段为varbinary

转载于:https://www.cnblogs.com/flashicp/archive/2007/05/14/746136.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值