关于在数据库中存储和读取图片(Access)

bfdhh 2004-03-17 03:03:17
在数据库access中如何存储图片啊?我设计表时设计了图片字段,但是不会输入图片,而且不知道如何将其读取并显示出来,在网上看到许多上传并显示图片的文章,看不懂,请大家给我指点一下每一步的含义,参数的含义,而且这些文章和我提出的问题并不一样,不过,我想看懂了这些文章对于解决我的问题应该有些用处,所以请大家指点一下.。
...全文
119 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
newman0708 2004-03-17
  • 打赏
  • 举报
回复
不会,学习...
帮你顶!
Uncommon 2004-03-17
  • 打赏
  • 举报
回复
我设计表时设计了图片字段,但是不会输入图片
**************************************
我来回答点简单的:
在设计视图完成后->打开表->选中图象字段->在插入菜单中选择对象->可以新建对象也可以浏览指定的文件作为对象->确定后图象就保存到数据库中了。
然后你可以依据楼上的方法测试以下能否调用

祝你成功
NoReady 2004-03-17
  • 打赏
  • 举报
回复
加入图片可以像这段代码一样的,你先把图片转化为memerystream:

Private Sub SaveStream()
Dim str As String
Dim arr() As Byte

str = "system"
arr = System.Text.Encoding.ASCII.GetBytes(str)

Dim conn As New OleDbConnection("Jet OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\test.mdb")
conn.Open()
Dim para As New OleDbParameter("@content", OleDbType.Binary)
para.Value = arr

Dim cmd As New OleDbCommand

Try

With cmd
.CommandText = "insert into tbl(content) values (@content)"
.Connection = conn
.Parameters.Add(para)
.ExecuteNonQuery()
End With
conn.Close()
conn.Dispose()
conn = Nothing

MsgBox("保存成功!")

Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
jicklv 2004-03-17
  • 打赏
  • 举报
回复
显示图片:aspx.cs文件
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string id=Request.Params["id"];
string sql="SELECT * FROM image WHERE id ='"+id+"'";
SqlConnection connection = new SqlConnection("Server=127.0.0.1;uid=sa;pwd=20030922;Database=image");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.Clear();
Response.AddHeader("Content-Type",dr["type"].ToString());
Response.BinaryWrite((byte[])dr["image"]);
}
dr.Close();
connection.Close();
}
jicklv 2004-03-17
  • 打赏
  • 举报
回复
上传图片:.aspx文件
<asp:label id="Message" Text="选择文件和文件名字:" runat="server" />
<hr>
<b>文件名字:</b><input id="MyFileName" type="text" runat="server" NAME="MyFileName">
<b>文件:</b><input id="MyFile" type="file" runat="server" NAME="MyFile">
<br>
<br>
<asp:Button ID="Submit" Runat="server" Text="开始上传"></asp:Button>

aspx.cs文件:

protected System.Web.UI.HtmlControls.HtmlInputText MyFileName;
protected System.Web.UI.HtmlControls.HtmlInputFile MyFile;
protected System.Web.UI.WebControls.Button Submit;
protected System.Web.UI.WebControls.Label Message;

private void Submit_Click(object sender, System.EventArgs e)
{
//得到提交的文件
Stream fileDataStream = MyFile.PostedFile.InputStream;

//得到文件大小
int fileLength = MyFile.PostedFile.ContentLength;

//创建数组
byte[] fileData = new byte[fileLength];

//把文件流填充到数组
fileDataStream.Read(fileData,0,fileLength);

//得到文件名字
string fileTitle = MyFileName.Value;

//得到文件类型
string fileType = MyFile.PostedFile.ContentType;

//构建数据库连接,SQL语句,创建参数
SqlConnection connection = new SqlConnection("Server=127.0.0.1;uid=sa;pwd=20030922;Database=image");
SqlCommand command = new SqlCommand("INSERT INTO image (title,image,datetime,type)" +
"VALUES (@MyFileName,@MyFile,@FileDate,@FileType)", connection);

SqlParameter paramTitle = new SqlParameter("@MyFileName", SqlDbType.VarChar,50);
paramTitle.Value = fileTitle;
command.Parameters.Add(paramTitle);

SqlParameter paramData = new SqlParameter("@MyFile", SqlDbType.Image);
paramData.Value = fileData;
command.Parameters.Add(paramData);

SqlParameter paramDate = new SqlParameter("@FileDate", SqlDbType.SmallDateTime);
paramDate.Value = DateTime.Now;
command.Parameters.Add(paramDate);

SqlParameter paramType = new SqlParameter ("@FileType", SqlDbType.VarChar,50);
paramType.Value = fileType;
command.Parameters.Add(paramType);

//打开连接,执行查询
connection.Open();
command.ExecuteNonQuery();
connection.Close();

Message.Text="你的文件已经成功上载";
MyFileName.Value = "";
}
zhuwevmfc 2004-03-17
  • 打赏
  • 举报
回复
保存图片的方法有很多,保存路径名是最简单的也是用的最多的!
如果用数据库保存,数据库会很大的,而且还不方便操作!
我有sql保存图片的代码!

111,094

社区成员

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

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

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