这样将图片加入数据库中(Access)?

hierce 2003-08-22 01:00:58
这样将图片加入数据库中(Access)?
多谢帮忙!
...全文
58 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tongzhenhua 2003-08-22
  • 打赏
  • 举报
回复
我只有sqlserver的。

图片的存储:
private void OnAddPhoto(object sender, System.EventArgs e)
{
// Show the file open dialog
if( DialogResult.OK == FileOpenDlg.ShowDialog() )
{
// Retrieve the treeitem for the selected parent node
TreeItem item = (TreeItem)treeAlbum.SelectedNode.Tag;

// We allow multiple selections so loop through each one
foreach( string file in FileOpenDlg.FileNames )
{
// Create a new stream to load this photo into
System.IO.FileStream stream = new System.IO.FileStream(file,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);
// Create a buffer to hold the stream bytes
byte[] buffer = new byte[stream.Length];
// Read the bytes from this stream
stream.Read(buffer, 0, (int)stream.Length);
// Now we can close the stream
stream.Close();

// Extract out the name of the file an use it for the name
// of the photo
string strName = System.IO.Path.GetFileNameWithoutExtension(file);

// Insert the image into the database and add it to the tree
InsertImage(ref buffer, strName, item.Id);
buffer = null;
}
// Select the last child node under this album
treeAlbum.SelectedNode = treeAlbum.SelectedNode.LastNode;
}
}

private void InsertImage(ref byte[] buffer, string strName, int nAlbum)
{
try
{
// Create a stored procedure command
SqlCommand cmd = new SqlCommand("sp_InsertPhoto", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;

// Add the return value parameter
SqlParameter param = cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
param.Direction = ParameterDirection.ReturnValue;

// Add the name paramter and set the value
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;
// Add the image paramter and set the value
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
// Add the album paramter and set the value
cmd.Parameters.Add("@album", SqlDbType.Int).Value = nAlbum;

// Execute the insert
cmd.ExecuteNonQuery();

// Return value will be the index of the newly added photo
int nID = (int)cmd.Parameters["RETURN_VALUE"].Value;

// Create a new node
TreeNode node = new TreeNode(strName);
// Create new treeitem to store the info about this photo
node.Tag = new TreeItem(ItemType.Photo, nID, "Enter description");

// Get the index of the album we are adding to
// and insert the new photo node
nID = treeAlbum.SelectedNode.Index;
treeAlbum.Nodes[nID].Nodes.Add(node);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}


CREATE PROCEDURE sp_InsertPhoto
@name AS VARCHAR(50),
@image AS IMAGE,
@album AS INT
AS

INSERT INTO Photos ([name], photo, album_id)
VALUES (@name, @image, @album)

RETURN @@identity

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧