导航:首页 > 图片大全 > 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如何导入图片相关的资料

热点内容
炫迈价格及图片 浏览:279
老贝依旧高清图片 浏览:338
美女耳坠图片 浏览:31
手机word文档怎么调整图片位置 浏览:864
刚出生女孩的图片大全 浏览:862
女生跪下道歉的图片 浏览:618
古装动漫汉服图片 浏览:217
跳舞衣服掉图片 浏览:184
word2010转pdf图片失真 浏览:383
关于爱情文字白底图片高清 浏览:628
如何多张图片分别导入不同页 浏览:648
多功能凉衣架图片价格 浏览:314
男生肛门大了图片 浏览:717
羊毛脸的两侧发型图片 浏览:314
模仿穿衣服图片 浏览:989
美女爱爱图片50p 浏览:715
杨颖亮片裙长筒靴高清图片 浏览:78
三个可爱男孩手拉手图片 浏览:571
word里表格如何做成图片 浏览:614
如何将pdf文件转换成图片 浏览:596