asp.net保存文件到access数据库并且读取文件示例

  本示例示范了如何使用asp.net保存文件到access数据库并且读取保存的的文件。

1)首先,在Access中建立一个表mg,结构如
 

字段名             字段类型              说明
ID                    自动编号              系统分配ID值
Imgtype           文本                     上传文件类型
imgdata          OLE对象                上传文件数据

 

 

read.aspx 页面主要代码
<form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" Width="345px" />
        <asp:Button ID="Button1" runat="server"
            Text="上传到数据库" onclick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="读取数据库到文件" 
            onclick="Button2_Click" /><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>

 

read.aspx.cs文件代码
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=admin;Data Source=D:\\WEB\\MyVs2008\\MyVs2008\\App_Data\\supervod.mdb");
protected void Button1_Click(object sender, EventArgs e)
        {
            if (!IsValid) return;
            if (FileUpload1.PostedFile.FileName != "")
            {
                int datalength = this.FileUpload1.PostedFile.ContentLength; //获取上传文件大小
                byte[] databuffer = new byte[datalength]; //建立文件字节数组,缓存空间
                System.IO.Stream objstream = this.FileUpload1.PostedFile.InputStream;
                objstream.Read(databuffer, 0, datalength);
                string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
                System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
                cmd.Connection = conn;
                cmd.CommandText = "insert into img(imgtype,imgdata) values(@imgtype,@imgdata)";
                cmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("@imgtype", System.Data.OleDb.OleDbType.VarChar)).Value = ext;
                cmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("@imgdata", System.Data.OleDb.OleDbType.Binary)).Value = databuffer;
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbDataReader dr = null;
            System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
            byte[] FileData=null;
            string type="";
            cmd.CommandText = "select imgdata,imgtype from img where id=@ID";
            cmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("@ID", System.Data.OleDb.OleDbType.Integer)).Value = TextBox1.Text.Trim();
            cmd.Connection = conn;
            conn.Open();
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                FileData = (byte[])dr["imgdata"];
                type=dr["imgtype"].ToString();
            }
            dr.Close();
            conn.Close();
            System.IO.FileStream fs = new System.IO.FileStream("d:\\aaa" + type, System.IO.FileMode.Create);
            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
            bw.Write(FileData, 0, FileData.Length);
            bw.Close();
        }

  代码很简单,这里只做了一个演示,保存文件名为固定名,其实可以在数据库里加入一个文件名字段,这样就可以解决

来源:http://www.cnblogs.com/lujin49/archive/2011/08/06/2129516.html

加支付宝好友偷能量挖...


评论(0)网络
阅读(145)喜欢(0)Asp.Net/C#/WCF