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

热点内容
粉色毛衣图片男生 浏览:511
男生耳环图片霸气 浏览:39
女生南半球图片 浏览:601
美女展下体艺术图片 浏览:933
宝宝满月的图片可爱 浏览:704
知书达理女生图片动漫 浏览:387
摸男生漫画图片 浏览:429
qq头像男生酷的图片 浏览:516
如何删除项目符号里面导入的图片 浏览:264
简单折星星方法图片 浏览:478
小女孩拿玫瑰安稳图片 浏览:225
壁纸男生专用横屏图片 浏览:834
小女生风衣图片 浏览:518
什么是主角的高清图片 浏览:886
穿白衣服男人图片 浏览:484
情侣简单图片墙纸 浏览:50
如何在word中插入图片箭头 浏览:337
女孩尿被拍图片 浏览:83
文字画迷图片 浏览:746
word插入的图片改变表格大小 浏览:816