向数据库中保存不同类型的文件,和在数据库中保存是一样的。就是向数据库以byte形式存入
向数据库中保存不同类型的文件,和在数据库中保存是一样的。就是向数据库以byte形式存入
然后就是写入数据库,代码如下:
FileInfo fi = new FileInfo( txtFileNameText );// Replace with your file name
if ( fiExists
{
byte[] bData = null;
int nNewFileID = 0;
// Read file data into buffer
using ( FileStream fs = fiOpenRead() )
{
bData = new byte[fiLength];
int nReadLength = fsRead( bData,0, (int)(fiLength) );
}
// 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 );
sqlCommParametersAdd( "@FileName", fiName );
sqlCommParametersAdd( "@FullName", fiFullName );
sqlCommParametersAdd( "@FileData", bData );
// Get new file ID
SqlDataReader sqlReader = sqlCommExecuteReader();
if( sqlReaderRead() )
{
nNewFileID = intParse(sqlReaderGetValue(0)ToString());
}
sqlReaderClose();
sqlCommDispose();
if( nNewFileID > 0 )
{
// Add new item in list view
ListViewItem itmNew = lsvFileInfoItemsAdd( fiName );
itmNewTag = nNewFileID;
}
}
4而读出的代码如下:
// Get new file name
string strFullName =
dlgFBSaveSelectedPath;
if( strFullName[strFullNameLength - 1] != '//'
)
strFullName
+= @"/";
strFullName +=
lsvFileInfoSelectedItems[0]Text;
string strQuery = "SELECT FileData FROM FileInfo
"
+
" WHERE FileID = " + lsvFileInfoSelectedItems[0]TagToString();
SqlDataAdapter
sqlDAdapter = new SqlDataAdapter(strQuery,sqlConn);
DataSet
sqlRecordSet = new DataSet();
byte[] bData = null;
//Get file data from DB
try
{
sqlDAdapterFill(
sqlRecordSet, "FileInfo" );
foreach( DataRow dr in sqlRecordSetTables["FileInfo"]Rows)
{
if( dr["FileData"] != DBNullValue )
bData
= ( byte[] )dr["FileData"];
}
}
catch(SqlException sqlErr)
{
MessageBoxShow(
sqlErrMessage );
}
catch
{
MessageBoxShow(
"Failed to read data from DB!" );
}
sqlRecordSetDispose();
sqlDAdapterDispose();
if( bData != null )
{
// Save file
FileInfo
fi = new FileInfo( strFullName
);
if( !fiExists )
{
//Create the file
using (FileStream fs = fiCreate())
{
fsWrite(
bData, 0, bDataLength);
}
}
else
{
//Create the file
using (FileStream fs =
fiOpenWrite())
{
fsWrite(
bData, 0, bDataLength);
}
}
}
建议存路径
下面是上传并保存路径到数据库
显示有很多中方法,如果在gridview里面显示的就如截图这样设置
protected void Button4_Click(object sender, EventArgs e){
string FilePath = "";
if (FileUpload1FileName != "")
{
if (FileUpload1PostedFileContentLength <= 0)
{
// PublicFunPublicFunctionshowMsg(this, "上传文件为空,请重新选择");
labmsgText = "上传文件为空,请重新选择";
return;
}
else
{
}
if (FileUpload1HasFile)
{
if (FileUpload1PostedFileContentLength > 4196304)
{
// PublicFunPublicFunctionshowMsg(this, "上传文件过大");
// return;
}
else
{
// FilePath = ServerMapPath("~/Download/Dload1");//服务器文件路径
}
FilePath = ServerMapPath("~/DownLoad/SignImg");//服务器文件路径
FileLoadFunUpLoad(FilePath, FileUpload1, DropDownList2SelectedValue);
UploadURL = "~/DownLoad/SignImg/" + DropDownList2SelectedValue + "_" + FileUpload1FileName;
UploadURL2 = UploadURL;
sql = " update yp_insproom_base_t set SignURL='" + UploadURL2 + "' " +
" where UserID='" + DropDownList2SelectedValue + "' ";
MySqlHelperExecuteNonQuery(PublicFunPublicFunctionGetDBconstr("ce_manage_db"), sql);
labmsgText = "上传成功";
databind();
}
}
else
{
// PublicFunPublicFunctionshowMsg(this, "上传文件路径错误");
labmsgText = "上传文件路径错误";
return;
}
}
以上就是关于如何在数据库中同时保存文本和图片全部的内容,包括:如何在数据库中同时保存文本和图片、亲啊我怎么把图片存入到数据库呢,然后再显示出来、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)