数据库可以存图片吗

数据库可以存图片吗,第1张

都可以的,至于怎么实现,建议你多去wwwcsdnnet看看吧,里面有很多高手,他们会教你的。
access是可以存储的,不是存储网址,至于如何用C#向ACCESS数据库插入?给你个例子,你慢慢研究一下吧,不过这样例子的版本是VS2003,不过是差不多的。
-------------------------------------
<%@ Page language="c#" Debug="true" Codebehind="Image2Accessaspxcs" AutoEventWireup="false" Inherits="eMengExamImage2Access" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 40 Transitional//EN" >
<HTML>
<HEAD>
<title>上传文件到 Access 数据库</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 70">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemasmicrosoftcom/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="DataGridShowImage" method="post" runat="server" enctype="multipart/form-data">
<h3 align="center">上传文件到 Access 数据库</h3>
<asp:DataGrid ID="DG_Persons" AutoGenerateColumns="False" Width="99%" HeaderStyle-BackColor="#ff0000"
HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#ffffff" ItemStyle-BackColor="Beige" BorderColor="#000000"
Runat="server" HeaderStyle-HorizontalAlign="Center">
<Columns>
<asp:TemplateColumn HeaderText="姓名">
<ItemTemplate>
<asp:Label Runat="server" Text='<%# DataBinderEval(ContainerDataItem, "PersonName") %>' ID="Label1"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="电子邮件">
<ItemTemplate>
<asp:Label Runat="server" Text='<%# DataBinderEval(ContainerDataItem, "PersonEmail") %>' ID="Label2"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="性别">
<ItemTemplate>
<asp:Label Runat="server" Text='<%# DataBinderEval(ContainerDataItem, "PersonSex") %>' ID="Label3"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="照片">
<ItemTemplate>
<asp:Image Runat=server ID="Image1" ImageUrl='<%# FormatURL(DataBinderEval(ContainerDataItem, "PersonID")) %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<b>文件名字:</b><input id="MyFileName" type="text" runat="server" NAME="MyFileName">
<P>
<b>文件:</b><input id="MyFile" type="file" runat="server" NAME="MyFile">
<br>
<br>
<input type="submit" value="开始上传" runat="server" ID="Submit1" NAME="Submit1">
</P>
</form>
</body>
</HTML>
----------------------------------------
using System;
using SystemCollections;
using SystemComponentModel;
using SystemData;
using SystemDataOleDb;
using SystemDrawing;
using SystemWeb;
using SystemIO;
using SystemWebSessionState;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIHtmlControls;

namespace eMengExam
{
/// <summary>
/// Image2Access 的摘要说明。
/// </summary>
public class Image2Access : SystemWebUIPage
{
protected SystemWebUIHtmlControlsHtmlInputText MyFileName;
protected SystemWebUIHtmlControlsHtmlInputFile MyFile;
protected SystemWebUIHtmlControlsHtmlInputButton Submit1;
protected SystemWebUIWebControlsDataGrid DG_Persons;

private void Page_Load(object sender, SystemEventArgs e)
{
// 在此处放置用户代码以初始化页面
BindGrid();
}
private void BindGrid()
{
string strCnn = "Provider=MicrosoftJetOLEDB40;Data Source=" +ServerMapPath("Image2Accessmdb");
OleDbConnection myConnection = new OleDbConnection(strCnn);
OleDbCommand myCommand = new OleDbCommand("SELECT FROM Person", myConnection);
myCommandCommandType = CommandTypeText;
try
{
myConnectionOpen();
DG_PersonsDataSource = myCommandExecuteReader(CommandBehaviorCloseConnection);
DG_PersonsDataBind();
}
catch(OleDbException SQLexc)
{
ResponseWrite("提取数据时出现错误:" + SQLexcToString());
}
}
protected string FormatURL(object strArgument)
{
return "ReadImageaspxid=" + strArgumentToString();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASPNET Web 窗体设计器所必需的。
//
InitializeComponent();
baseOnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
thisSubmit1ServerClick += new SystemEventHandler(thisSubmit1_ServerClick);
thisLoad += new SystemEventHandler(thisPage_Load);

}
#endregion

private void Submit1_ServerClick(object sender, SystemEventArgs e)
{
//得到提交的文件
Stream fileDataStream = MyFilePostedFileInputStream;

//得到文件大小
int fileLength = MyFilePostedFileContentLength;

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

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

//得到文件名字
string fileTitle = MyFileNameValue;

//得到文件类型
string fileType = MyFilePostedFileContentType;

//构建数据库连接,SQL语句,创建参数
string strCnn = "Provider=MicrosoftJetOLEDB40;Data Source=" + ServerMapPath("Image2Accessmdb");
OleDbConnection myConnection = new OleDbConnection(strCnn);
OleDbCommand command = new OleDbCommand ("INSERT INTO Person (PersonName,PersonEmail,PersonSex,PersonImageType,PersonImage)" +
"VALUES (@PersonName,@PersonEmail,@PersonSex,@PersonImageType,@PersonImage)", myConnection);

SystemDataOleDbOleDbParameter paramPersonName = new OleDbParameter("@PersonName", SystemDataOleDbOleDbTypeVarChar,50);
paramPersonNameValue = fileTitle;
commandParametersAdd(paramPersonName);

SystemDataOleDbOleDbParameter paramPersonEmail = new OleDbParameter("@PersonEmail", SystemDataOleDbOleDbTypeVarChar,50);
paramPersonEmailValue = "mengxianhui@dotnetaspxcc";
commandParametersAdd(paramPersonEmail);

SystemDataOleDbOleDbParameter paramPersonSex = new OleDbParameter("@paramPersonSex", SystemDataOleDbOleDbTypeVarChar,50);
paramPersonSexValue = "男";
commandParametersAdd(paramPersonSex);

SystemDataOleDbOleDbParameter paramPersonImageType = new OleDbParameter("@PersonImageType", SystemDataOleDbOleDbTypeVarChar,50);
paramPersonImageTypeValue = fileType;
commandParametersAdd(paramPersonImageType);

SystemDataOleDbOleDbParameter paramPersonImage = new OleDbParameter("@PersonImage", SystemDataOleDbOleDbTypeBinary);
paramPersonImageValue = fileData;
commandParametersAdd(paramPersonImage);

//打开连接,执行查询
myConnectionOpen();
commandExecuteNonQuery();
myConnectionClose();
}
}
}

以上就是关于数据库可以存图片吗全部的内容,包括:数据库可以存图片吗、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9479873.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存