字段名
类型
备注
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)
}
}
}
不过需要提的一点,如果把大量的文件存入数据库的话,会造成数据库的臃肿,而且访问量也会增大。所以现在比较流行的做法,是把文件上传到服务器上,而在数据库上只保存文件的相对路径即可。那么访问的时候,先通过数据库得到文件的相对路径,然后再访问服务器上的文件
public DataSet ds = nullpublic SqlDataAdapter sda = null
public static SqlConnection conn = null
//连接数据库
public void OpenLink()
{
conn = new SqlConnection()
conn.ConnectionString = "Server=192.168.1.5uid=sapwd=555dataBase=数据库名"
try
{
conn.Open()
}
catch
{
MessageBox.Show("连接数据库丢失!")
}
}
//查询数据表
public void link(String sql)
{
if (conn != null)
{
ds = new DataSet()
sda = new SqlDataAdapter()
sda.SelectCommand = new SqlCommand(sql, conn)
SqlCommandBuilder builder = new SqlCommandBuilder(sda)
sda.Fill(ds)
}
}
public void Pix(PictureBox px, String name) //显示图片
{
String sql = "Select 识别码 from 下单表 where 订票人='" + name + "'"
byte[] imagebytes = null
SqlCommand com = new SqlCommand(sql, conn)
SqlDataReader dr = com.ExecuteReader()
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(0)
}
MemoryStream ms = new MemoryStream(imagebytes)
Bitmap bmpt = new Bitmap(ms)
px.Image = bmpt
px.SizeMode = PictureBoxSizeMode.StretchImage
}
放工程启动的时候就OpenLink(),把上面代码存储在类文件里面,Pix
()函数就是显示图片。
简单说下:图片存入数据库很少人会做,应该是把图片在服务器上的路径存到数据库中,图片从客户端到服务端需要图片上传功能,而图片显示这比较简单了,弄个image控件,有个imageurl属性,设置为imageurl=<%#Eval("pic")%>,就能把图片读出来!欢迎分享,转载请注明来源:内存溢出
评论列表(0条)