none
vs中在Access数据库中保存Ole对象 RRS feed

  • 问题

  •  

      请问在vs 中可不可以把表格当作一个Ole对象保存到Access数据库中?有哪位高手知道的话,请指点一下,谢谢!!!

     另外,vs中怎么实现把图片存到数据库,怎么把图片从数据库中提取出来?

    2008年4月11日 7:10

答案

  • Code Snippet

    <%@ Page language="c#" Debug="true" Codebehind="Image2Access.aspx.cs"
    AutoEventWireup="false" Inherits="eMeng.Exam.Image2Access" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>上传文件到 Access 数据库</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <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 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='<%# DataBinder.Eval(Container.DataItem, "PersonName") %>' ID="Label1"/>
      </ItemTemplate>
     </asp:TemplateColumn>
     <asp:TemplateColumn HeaderText="电子邮件">
      <ItemTemplate>
       <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonEmail") %>' ID="Label2"/>
      </ItemTemplate>
     </asp:TemplateColumn>
     <asp:TemplateColumn HeaderText="性别">
      <ItemTemplate>
       <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonSex") %>' ID="Label3"/>
      </ItemTemplate>
     </asp:TemplateColumn>
     <asp:TemplateColumn HeaderText="照片">
      <ItemTemplate>
       <asp:Image Runat=server ID="Image1"
       ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "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>
    Image2Access.aspx.cs

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.IO;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace eMeng.Exam
    {
    /// <summary>
    /// Image2Access 的摘要说明。
    /// </summary>
    public class Image2Access : System.Web.UI.Page
    {
    protected System.Web.UI.HtmlControls.HtmlInputText MyFileName;
    protected System.Web.UI.HtmlControls.HtmlInputFile MyFile;
    protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
    protected System.Web.UI.WebControls.DataGrid DG_Persons;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    BindGrid();
    }
    private void BindGrid()
    {
    string strCnn  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    + Server.MapPath("Image2Access.mdb");
    OleDbConnection myConnection = new OleDbConnection(strCnn);
    OleDbCommand myCommand = new OleDbCommand("SELECT * FROM Person", myConnection);
    myCommand.CommandType = CommandType.Text;
    try
    {
    myConnection.Open();
    DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    DG_Persons.DataBind();
    }
    catch(OleDbException SQLexc)
    {
    Response.Write("提取数据时出现错误:" + SQLexc.ToString());
    }
    }
    protected string FormatURL(object strArgument)
    {
    return "ReadImage.aspx?id=" + strArgument.ToString();
    }

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

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {   
    this.Submit1.ServerClick += new System.EventHandler(this.Submit1_ServerClick);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void Submit1_ServerClick(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语句,创建参数
    string strCnn  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Image2Access.mdb");
    OleDbConnection myConnection = new OleDbConnection(strCnn);
    OleDbCommand command = new OleDbCommand ("INSERT INTO Person (PersonName,PersonEmail,PersonSex,PersonImageType,PersonImage)" +
    "VALUES (@PersonName,@PersonEmail,@PersonSex,@PersonImageType,@PersonImage)", myConnection);

    System.Data.OleDb.OleDbParameter paramPersonName = new OleDbParameter("@PersonName", System.Data.OleDb.OleDbType.VarChar,50);
    paramPersonName.Value = fileTitle;
    command.Parameters.Add(paramPersonName);

    System.Data.OleDb.OleDbParameter paramPersonEmail = new OleDbParameter("@PersonEmail", System.Data.OleDb.OleDbType.VarChar,50);
    paramPersonEmail.Value = "mengxianhui@dotnet.aspx.cc";
    command.Parameters.Add(paramPersonEmail);

    System.Data.OleDb.OleDbParameter paramPersonSex = new OleDbParameter("@paramPersonSex", System.Data.OleDb.OleDbType.VarChar,50);
    paramPersonSex.Value = "男";
    command.Parameters.Add(paramPersonSex);

    System.Data.OleDb.OleDbParameter paramPersonImageType = new OleDbParameter("@PersonImageType", System.Data.OleDb.OleDbType.VarChar,50);
    paramPersonImageType.Value = fileType;
    command.Parameters.Add(paramPersonImageType);

    System.Data.OleDb.OleDbParameter paramPersonImage = new OleDbParameter("@PersonImage", System.Data.OleDb.OleDbType.Binary);
    paramPersonImage.Value = fileData;
    command.Parameters.Add(paramPersonImage);

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

     

     

    2008年4月27日 12:01
    版主

全部回复

  •  

    呵呵,谢谢,我想图片的录入和提出应该没问题了

    还有一个问题请教,我用的VB语言,你说可不可以把一张表格当作Ole对象保存到数据库中啊?

    2008年4月12日 7:25
  •  

    一张表格?html代码的table还是什么?
    2008年4月12日 13:34
    版主
  •  

    不是html的表格,是word里面的表格,

    举个例子:有一个试题,除了有文本,还有表格,那可不可把表格当作Ole对象来保存,或者我应该怎样保存,高手,有什么高见?期待中........

    2008年4月13日 11:55
  • Code Snippet

    <%@ Page language="c#" Debug="true" Codebehind="Image2Access.aspx.cs"
    AutoEventWireup="false" Inherits="eMeng.Exam.Image2Access" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>上传文件到 Access 数据库</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <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 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='<%# DataBinder.Eval(Container.DataItem, "PersonName") %>' ID="Label1"/>
      </ItemTemplate>
     </asp:TemplateColumn>
     <asp:TemplateColumn HeaderText="电子邮件">
      <ItemTemplate>
       <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonEmail") %>' ID="Label2"/>
      </ItemTemplate>
     </asp:TemplateColumn>
     <asp:TemplateColumn HeaderText="性别">
      <ItemTemplate>
       <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonSex") %>' ID="Label3"/>
      </ItemTemplate>
     </asp:TemplateColumn>
     <asp:TemplateColumn HeaderText="照片">
      <ItemTemplate>
       <asp:Image Runat=server ID="Image1"
       ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "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>
    Image2Access.aspx.cs

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.IO;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace eMeng.Exam
    {
    /// <summary>
    /// Image2Access 的摘要说明。
    /// </summary>
    public class Image2Access : System.Web.UI.Page
    {
    protected System.Web.UI.HtmlControls.HtmlInputText MyFileName;
    protected System.Web.UI.HtmlControls.HtmlInputFile MyFile;
    protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
    protected System.Web.UI.WebControls.DataGrid DG_Persons;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    BindGrid();
    }
    private void BindGrid()
    {
    string strCnn  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    + Server.MapPath("Image2Access.mdb");
    OleDbConnection myConnection = new OleDbConnection(strCnn);
    OleDbCommand myCommand = new OleDbCommand("SELECT * FROM Person", myConnection);
    myCommand.CommandType = CommandType.Text;
    try
    {
    myConnection.Open();
    DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    DG_Persons.DataBind();
    }
    catch(OleDbException SQLexc)
    {
    Response.Write("提取数据时出现错误:" + SQLexc.ToString());
    }
    }
    protected string FormatURL(object strArgument)
    {
    return "ReadImage.aspx?id=" + strArgument.ToString();
    }

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

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {   
    this.Submit1.ServerClick += new System.EventHandler(this.Submit1_ServerClick);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void Submit1_ServerClick(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语句,创建参数
    string strCnn  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Image2Access.mdb");
    OleDbConnection myConnection = new OleDbConnection(strCnn);
    OleDbCommand command = new OleDbCommand ("INSERT INTO Person (PersonName,PersonEmail,PersonSex,PersonImageType,PersonImage)" +
    "VALUES (@PersonName,@PersonEmail,@PersonSex,@PersonImageType,@PersonImage)", myConnection);

    System.Data.OleDb.OleDbParameter paramPersonName = new OleDbParameter("@PersonName", System.Data.OleDb.OleDbType.VarChar,50);
    paramPersonName.Value = fileTitle;
    command.Parameters.Add(paramPersonName);

    System.Data.OleDb.OleDbParameter paramPersonEmail = new OleDbParameter("@PersonEmail", System.Data.OleDb.OleDbType.VarChar,50);
    paramPersonEmail.Value = "mengxianhui@dotnet.aspx.cc";
    command.Parameters.Add(paramPersonEmail);

    System.Data.OleDb.OleDbParameter paramPersonSex = new OleDbParameter("@paramPersonSex", System.Data.OleDb.OleDbType.VarChar,50);
    paramPersonSex.Value = "男";
    command.Parameters.Add(paramPersonSex);

    System.Data.OleDb.OleDbParameter paramPersonImageType = new OleDbParameter("@PersonImageType", System.Data.OleDb.OleDbType.VarChar,50);
    paramPersonImageType.Value = fileType;
    command.Parameters.Add(paramPersonImageType);

    System.Data.OleDb.OleDbParameter paramPersonImage = new OleDbParameter("@PersonImage", System.Data.OleDb.OleDbType.Binary);
    paramPersonImage.Value = fileData;
    command.Parameters.Add(paramPersonImage);

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

     

     

    2008年4月27日 12:01
    版主