用Process啊 Process p = New Process(); p.StartInfo.FileName=@"d:\123.doc"; p.start(); '这样就是默认的方式打开
不知道你数据库里的文件保存的是什么格式,如果是byte那就读出来创建文件 比喻一个word文件 filestream ft = new filestream(@"d:\123.doc",openOrCreate); streamwirter sw = new streamwriter(ft);sw.writebybyte = 你从数据库读出来的bytesw.close(); ft.close();
string filePath=this.saveFileDialog1.FileName;
SqlConnection myConnection=new SqlConnection(); SqlCommand myCommand=new SqlCommand(); myConnection.ConnectionString=ConnectionString; myConnection.Open(); string sql="select FileContent from Sys_NoStructureDatas where Sys_ID="+id; myCommand.Connection=myConnection; myCommand.CommandType=CommandType.Text; myCommand.CommandText=sql;
然后就是写入数据库,代码如下: FileInfo fi = new FileInfo( txtFileName.Text );// Replace with your file name if ( fi.Exists) { byte[] bData = null; int nNewFileID = 0; // Read file data into buffer using ( FileStream fs = fi.OpenRead() ) { bData = new byte[fi.Length]; int nReadLength = fs.Read( bData,0, (int)(fi.Length) ); }
// Add file info into DB string strQuery = "INSERT INTO FileInfo " + " ( FileName, FullName, FileData ) " + " VALUES " + " ( @FileName, @FullName, @FileData ) " + " SELECT @@IDENTITY AS 'Identity'"; SqlCommand sqlComm = new SqlCommand( strQuery, sqlConn ); sqlComm.Parameters.Add( "@FileName", fi.Name ); sqlComm.Parameters.Add( "@FullName", fi.FullName ); sqlComm.Parameters.Add( "@FileData", bData );
// Get new file ID SqlDataReader sqlReader = sqlComm.ExecuteReader(); if( sqlReader.Read() ) { nNewFileID = int.Parse(sqlReader.GetValue(0).ToString()); } sqlReader.Close(); sqlComm.Dispose();
if( nNewFileID > 0 ) { // Add new item in list view ListViewItem itmNew = lsvFileInfo.Items.Add( fi.Name ); itmNew.Tag = nNewFileID; } }
而读出的代码如下: // Get new file name string strFullName = dlgFBSave.SelectedPath; if( strFullName[strFullName.Length - 1] != '\\' ) strFullName += @"\"; strFullName += lsvFileInfo.SelectedItems[0].Text;
string strQuery = "SELECT FileData FROM FileInfo " + " WHERE FileID = " + lsvFileInfo.SelectedItems[0].Tag.ToString();
SqlDataAdapter sqlDAdapter = new SqlDataAdapter(strQuery,sqlConn); DataSet sqlRecordSet = new DataSet();
byte[] bData = null;
//Get file data from DB try { sqlDAdapter.Fill( sqlRecordSet, "FileInfo" ); foreach( DataRow dr in sqlRecordSet.Tables["FileInfo"].Rows) { if( dr["FileData"] != DBNull.Value ) bData = ( byte[] )dr["FileData"]; } } catch(SqlException sqlErr) { MessageBox.Show( sqlErr.Message ); } catch { MessageBox.Show( "Failed to read data from DB!" ); } sqlRecordSet.Dispose(); sqlDAdapter.Dispose();
if( bData != null ) { // Save file FileInfo fi = new FileInfo( strFullName ); if( !fi.Exists ) { //Create the file. using (FileStream fs = fi.Create()) { fs.Write( bData, 0, bData.Length); } } else { //Create the file. using (FileStream fs = fi.OpenWrite()) { fs.Write( bData, 0, bData.Length); } } }
Process p = New Process();
p.StartInfo.FileName=@"d:\123.doc";
p.start(); '这样就是默认的方式打开
比喻一个word文件
filestream ft = new filestream(@"d:\123.doc",openOrCreate);
streamwirter sw = new streamwriter(ft);sw.writebybyte = 你从数据库读出来的bytesw.close();
ft.close();
SqlConnection myConnection=new SqlConnection();
SqlCommand myCommand=new SqlCommand(); myConnection.ConnectionString=ConnectionString;
myConnection.Open(); string sql="select FileContent from Sys_NoStructureDatas where Sys_ID="+id;
myCommand.Connection=myConnection;
myCommand.CommandType=CommandType.Text;
myCommand.CommandText=sql;
SqlDataReader myDataReader=myCommand.ExecuteReader(); if(myDataReader.Read())
{
System.IO.FileInfo myFileInfo=new FileInfo(filePath);
System.IO.FileStream myFileStream=myFileInfo.Open(System.IO.FileMode.OpenOrCreate); byte[] imgData=(byte[])myDataReader["FileContent"];
foreach(byte a in imgData)
{
myFileStream.WriteByte(a); } myFileStream.Close();
myDataReader.Close(); } myConnection.Close();
MessageBox.Show("文件保存成功!",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Information);
System.Diagnostics.process.Start (filename);
字段名 类型 备注
FileID Int 自增字段
FileName Varchar(256)
FullName Varchar(1024)
FileData Image
然后就是写入数据库,代码如下:
FileInfo fi = new FileInfo( txtFileName.Text );// Replace with your file name
if ( fi.Exists)
{
byte[] bData = null;
int nNewFileID = 0;
// Read file data into buffer
using ( FileStream fs = fi.OpenRead() )
{
bData = new byte[fi.Length];
int nReadLength = fs.Read( bData,0, (int)(fi.Length) );
}
// Add file info into DB
string strQuery = "INSERT INTO FileInfo "
+ " ( FileName, FullName, FileData ) "
+ " VALUES "
+ " ( @FileName, @FullName, @FileData ) "
+ " SELECT @@IDENTITY AS 'Identity'";
SqlCommand sqlComm = new SqlCommand( strQuery, sqlConn );
sqlComm.Parameters.Add( "@FileName", fi.Name );
sqlComm.Parameters.Add( "@FullName", fi.FullName );
sqlComm.Parameters.Add( "@FileData", bData );
// Get new file ID
SqlDataReader sqlReader = sqlComm.ExecuteReader();
if( sqlReader.Read() )
{
nNewFileID = int.Parse(sqlReader.GetValue(0).ToString());
}
sqlReader.Close();
sqlComm.Dispose();
if( nNewFileID > 0 )
{
// Add new item in list view
ListViewItem itmNew = lsvFileInfo.Items.Add( fi.Name );
itmNew.Tag = nNewFileID;
}
}
而读出的代码如下:
// Get new file name
string strFullName = dlgFBSave.SelectedPath;
if( strFullName[strFullName.Length - 1] != '\\' )
strFullName += @"\";
strFullName += lsvFileInfo.SelectedItems[0].Text;
string strQuery = "SELECT FileData FROM FileInfo "
+ " WHERE FileID = " + lsvFileInfo.SelectedItems[0].Tag.ToString();
SqlDataAdapter sqlDAdapter = new SqlDataAdapter(strQuery,sqlConn);
DataSet sqlRecordSet = new DataSet();
byte[] bData = null;
//Get file data from DB
try
{
sqlDAdapter.Fill( sqlRecordSet, "FileInfo" );
foreach( DataRow dr in sqlRecordSet.Tables["FileInfo"].Rows)
{
if( dr["FileData"] != DBNull.Value )
bData = ( byte[] )dr["FileData"];
}
}
catch(SqlException sqlErr)
{
MessageBox.Show( sqlErr.Message );
}
catch
{
MessageBox.Show( "Failed to read data from DB!" );
}
sqlRecordSet.Dispose();
sqlDAdapter.Dispose();
if( bData != null )
{
// Save file
FileInfo fi = new FileInfo( strFullName );
if( !fi.Exists )
{
//Create the file.
using (FileStream fs = fi.Create())
{
fs.Write( bData, 0, bData.Length);
}
}
else
{
//Create the file.
using (FileStream fs = fi.OpenWrite())
{
fs.Write( bData, 0, bData.Length);
}
}
}