首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

把远程数据里文件路径的文件以二进制流形式存到本地数据库,该如何处理

2012-01-30 
把远程数据里文件路径的文件以二进制流形式存到本地数据库知道文件路径FilePath怎么已二进制流形式存到本

把远程数据里文件路径的文件以二进制流形式存到本地数据库
知道文件路径   FilePath  

怎么已二进制流形式存到本地数据库,还有怎么获得文件字节大小



[解决办法]
首先,介绍一下保存文件到数据库中。
将文件保存到数据库中,实际上是将文件转换成二进制流后,将二进制流保存到数据库相应的字段中。在SQL Server中该字段的数据类型是Image,在Access中该字段的数据类型是OLE对象。
//保存文件到SQL Server数据库中
FileInfo fi=new FileInfo(fileName);
FileStream fs=fi.OpenRead();
byte[] bytes=new byte[fs.Length];
fs.Read(bytes,0,Convert.ToInt32(fs.Length));
SqlCommand cm=new SqlCommand();
cm.Connection=cn;
cm.CommandType=CommandType.Text;
if(cn.State==0) cn.Open();
cm.CommandText= "insert into "+tableName+ "( "+fieldName+ ") values(@file) ";
SqlParameter spFile=new SqlParameter( "@file ",SqlDbType.Image);
spFile.Value=bytes;
cm.Parameters.Add(spFile);
cm.ExecuteNonQuery()

//保存文件到Access数据库中

FileInfo fi=new FileInfo(fileName);
FileStream fs=fi.OpenRead();
byte[] bytes=new byte[fs.Length];
fs.Read(bytes,0,Convert.ToInt32(fs.Length));
OleDbCommand cm=new OleDbCommand();
cm.Connection=cn;
cm.CommandType=CommandType.Text;
if(cn.State==0) cn.Open();
cm.CommandText= "insert into "+tableName+ "( "+fieldName+ ") values(@file) ";
OleDbParameter spFile=new OleDbParameter( "@file ",OleDbType.Binary);
spFile.Value=bytes;
cm.Parameters.Add(spFile);
cm.ExecuteNonQuery()
//保存客户端文件到数据库
sql= "update t_mail set attachfilename=@attachfilename,attachfile=@attachfile where mailid= "+mailid;
myCommand = new SqlCommand(sql, new SqlConnection(ConnStr));
string path = fl_name.PostedFile.FileName;
string filename=path.Substring(path.LastIndexOf( "\\ ")+1,path.Length-path.LastIndexOf( "\\ ")-1);
myCommand.Parameters.Add( "@attachfilename ",SqlDbType.VarChar);
myCommand.Parameters[ "@attachfilename "].Value=filename;

myCommand.Parameters.Add( "@attachfile ",SqlDbType.Image);
Stream fileStream = fl_name.PostedFile.InputStream;
int intFileSize = fl_name.PostedFile.ContentLength;
byte[] fileContent = new byte[intFileSize];
int intStatus = fileStream.Read(fileContent,0,intFileSize); //文件读取到fileContent数组中
myCommand.Parameters[ "@attachfile "].Value=((byte[])fileContent);
fileStream.Close();
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();


代码中的fileName是文件的完整名称,tableName是要操作的表名称,fieldName是要保存文件的字段名称。

两段代码实际上是一样的,只是操作的数据库不同,使用的对象不同而已。

接着,在说说将文件从数据库中读取出来,只介绍从SQL Server中读取。

SqlDataReader dr=null;
SqlConnection objCn=new SqlConnection();
objCn.ConnectionString= "Data Source=(local);User ID=sa;Password=;Initial Catalog=Test ";
SqlCommand cm=new SqlCommand();
cm.Connection=cn;
cm.CommandType=CommandType.Text;
cm.CommandText= "select "+fieldName+ " from "+tableName+ " where ID=1 ";
dr=cm.ExecuteReader();
byte[] File=null;
if(dr.Read())
{
File=(byte[])dr[0];
}
FileStream fs;
FileInfo fi=new System.IO.FileInfo(fileName);
fs=fi.OpenWrite();
fs.Write(File,0,File.Length);
fs.Close();

上面的代码是将保存在数据库中的文件读取出来并保存文fileName指定的文件中。

在使用上面的代码时,别忘了添加System.Data.SqlClient和System.IO引用。


修改:
将读文件的下面部分的代码
FileStream fs;
FileInfo fi=new System.IO.FileInfo(fileName);
fs=fi.OpenWrite();
fs.Write(File,0,File.Length);
fs.Close();
修改为
FileStream fs=new FileStream(fileName,FileMode.CreateNew);


BinaryWriter bw=new BinaryWriter(fs);
bw.Write(File,0,File.Length);
bw.Close();
fs.Close();
[解决办法]
/// <summary>
/// 将照片转换为二进制数组
/// </summary>
/// <param name= "path "> </param>
/// <returns> </returns>
private byte[] PhotoToArray( string path )
{
FileStream stream = new FileStream( path , FileMode.Open , FileAccess.Read ) ;
byte[] bufferPhoto =new byte[stream.Length] ;
stream.Read( bufferPhoto,0,Convert.ToInt32( stream.Length ) ) ;
stream.Flush();
stream.Close();

return bufferPhoto ;
}

//把二进制的图片插到数据库
private void Save(byte[] image)
{
string sql = "insert into table2(aaa,photo) values(@aaa,@photo) ";
SqlParameter[] param=new SqlParameter[2];
param[0] =new SqlParameter( "@aaa ",SqlDbType.Int);
param[0].Value = 1;
param[1]= new SqlParameter( "@photo ",SqlDbType.Image);
param[1].Value= image;

SqlConnection conn= new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings[0];

SqlCommand commd= new SqlCommand(sql,conn);
commd.Parameters.Add(param[0]);
commd.Parameters.Add(param[1]);

try
{
conn.Open();
commd.ExecuteNonQuery();
MessageBox.Show( "把图片成功的插入数据库 ");
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}

热点排行