導航:首頁 > 圖片大全 > Openwrite如何導入圖片

Openwrite如何導入圖片

發布時間:2023-07-11 18:01:51

如何在資料庫中同時保存文本和圖片

  1. 向資料庫中保存不同類型的文件,和在資料庫中保存圖片是一樣的。就是向資料庫以byte形式存入

  2. 向資料庫中保存不同類型的文件,和在資料庫中保存圖片是一樣的。就是向資料庫以byte形式存入

  3. 然後就是寫入資料庫,代碼如下:

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;
}
}

4.而讀出的代碼如下:

// Get new file name

stringstrFullName =
dlgFBSave.SelectedPath;

if( strFullName[strFullName.Length - 1] != '//'
)

strFullName
+= @"/";

strFullName +=
lsvFileInfo.SelectedItems[0].Text;

stringstrQuery = "SELECT FileData FROM FileInfo
"

+
" WHERE FileID = " + lsvFileInfo.SelectedItems[0].Tag.ToString();

SqlDataAdapter
sqlDAdapter =newSqlDataAdapter(strQuery,sqlConn);

DataSet
sqlRecordSet =newDataSet();

byte[] bData =null;

//Get file data from DB

try

{

sqlDAdapter.Fill(
sqlRecordSet, "FileInfo" );

foreach( DataRow drinsqlRecordSet.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 =newFileInfo( 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);

}

}

}

閱讀全文

與Openwrite如何導入圖片相關的資料

熱點內容
簡單服裝素描畫圖片大全 瀏覽:34
苞苞頭發型圖片 瀏覽:717
如何判斷自己有無智齒圖片 瀏覽:657
可愛的馮哥圖片 瀏覽:573
如何在word中的圖片上覆蓋圖片 瀏覽:956
兒童櫻花發型圖片 瀏覽:692
如何將圖片跟文字組合 瀏覽:633
古代女主子衣服裝飾動漫圖片 瀏覽:935
和平精英如何發送自己圖片動態 瀏覽:47
如何保存網頁鏈接圖片 瀏覽:951
簡單手繪海報圖片大全 瀏覽:578
動漫王子和公主圖片 瀏覽:255
男生放鬆一切的圖片 瀏覽:781
美國超市女孩圖片 瀏覽:385
邁銳寶玻璃價格及圖片 瀏覽:477
動漫蝴蝶飛舞圖片 瀏覽:973
如何拍攝前景亮的圖片 瀏覽:655
沙宣方圓三角發型圖片 瀏覽:672
玳瑁珠子價格及圖片 瀏覽:532
女生兒童舞蹈服裝圖片 瀏覽:798