日期:2014-05-19  浏览次数:21033 次

如何把图片保存到数据库中?
以及如何再WinForm中把他显示出来?


谢谢

------解决方案--------------------
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.IO;
using System.Data.OleDb;
namespace MikeCat
{
/// <summary>
/// MikeCat_Upload_DataBase 的摘要说明。
/// ************************************************
/// 作者:迈克老猫(张琳)
/// 功能:上传文件至数据库并显示
/// ************************************************
/// </summary>
public class MikeCat_Upload_DataBase : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label strStatus;
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + HttpContext.Current.Server.MapPath( "northwind.mdb ");

private void Page_Load(object sender, System.EventArgs e)
{
mfbind();
}
private void mfbind()
{
string sql= "select * from uploadfile ";
OleDbConnection conn=new OleDbConnection(connstr);
OleDbCommand cmd=new OleDbCommand(sql,conn);
conn.Open();
DataGrid1.DataSource=cmd.ExecuteReader();
DataGrid1.DataBind();
conn.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.UploadButton.Click += new System.EventHandler(this.UploadButton_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void UploadButton_Click(object sender, System.EventArgs e)
{
try
{
Stream mffileStream;
int mffileSize;
string mffileName;
string mffileContentType;
string mffileUploadedName;
mffileStream = this.File1.PostedFile.InputStream; //二进制流
mffileSize = this.File1.PostedFile.ContentLength; //文件大小
mffileUploadedName = this.File1.PostedFile.FileName; //文件路径
byte[] mffileBody=new byte[mffileSize];
mffileContentType = this.File1.PostedFile.ContentType; //文件类型
mffileName =mffileUploadedName.Substring(mffileUploadedName.LastIndexOf( '\\ ')+1);
int n = mffileStream.Read(mffileBody, 0, mffileSize);

//写库法一:
OleDbConnection conn = new OleDbConnection(connstr);
String sql = "Select * from uploadfile ";
OleDbDataAdapter da = new OleDbDataAdapter(sql,conn);
DataSet ds = new DataSet();
da.Fill(ds);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
DataRow row = ds.Tables[0].NewRow();
row[ "filebody "] = mffileBody; //保存数据到数据库为二进制
row[ "filename "] = mffileName; //保存文件名称
row[ "file