帮帮我解决一下那几个问题吧!!!
img2access.aspx
<%@ Page language="c#" Codebehind="img2access.aspx.cs" AutoEventWireup="false" Inherits="test.img2access" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>img2access</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体"><INPUT type="file" runat="server" id="myFile"></FONT>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>
img2access.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
namespace test
{
/// <summary>
/// img2access 的摘要说明。
/// </summary>
public class img2access : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Web.HttpContext.Current.Server.MapPath("img.mdb"));
Stream FileStream=this.myFile.PostedFile.InputStream; //建立一个Stream对象
int FileLen=this.myFile.PostedFile.ContentLength; //图片数据的字节大小
string ContentType = this.myFile.PostedFile.ContentType;
string clientFile = this.myFile.PostedFile.FileName;
string[] tmp = clientFile.Split('.');
string imgType = tmp[tmp.Length-1].ToString();
byte[] FileData=new byte[FileLen];
int n = FileStream.Read(FileData,0,FileLen);
string strSql = "INSERT INTO tbImg (imgdata,imgtype,ContentType) VALUES (@imgdata,@imgtype,@ContentType)";
myConn.Open();
OleDbCommand myCommand = new OleDbCommand(strSql,myConn);
myCommand.Parameters.Add("@imgdata",OleDbType.Binary);
myCommand.Parameters["@imgdata"].Value = FileData;
myCommand.Parameters.Add("@imgtype",OleDbType.VarChar,50);
myCommand.Parameters["@imgtype"].Value = imgType;
myCommand.Parameters.Add("@ContentType",OleDbType.VarChar,50);
myCommand.Parameters["@ContentType"].Value = ContentType;
int iout = myCommand.ExecuteNonQuery();
myConn.Close();
if (iout==1)
{
Response.Write("添加成功");
}
}
}
}
img.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace test
{
/// <summary>
/// img 的摘要说明。
/// </summary>
public class img : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Web.HttpContext.Current.Server.MapPath("img.mdb"));
string strSql = "select * from tbImg where id=4";
myConn.Open();
OleDbCommand myCommand = new OleDbCommand(strSql,myConn);
OleDbDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
Response.ContentType = myReader["ContentType"].ToString();
Response.BinaryWrite((byte[])myReader["imgdata"]);
}
myReader.Close();
myConn.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}