如何将word文件上传和下载到SQL Server数据库? [英] How to upload and download a word file into SQL Server database?

查看:111
本文介绍了如何将word文件上传和下载到SQL Server数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮帮我。我非常想要这段代码。

please help me.I want this code very badly.

推荐答案

在数据库中创建一个名为File的表,其中包含以下字段:

列名数据类型



ID uniqueidentifier

FileName nvarchar(150)

FileType nvarchar(100)

FileSize int

FileContent varbinary(MAX)



使用varbinary(Max),你可以保存任何数据库中的内容类型



将文件从流转换为字节数组。

如果使用FileUpload控件则使用fallowing ..



Create a table with name File in your database with following fields:
Column Name Data Type

ID uniqueidentifier
FileName nvarchar(150)
FileType nvarchar(100)
FileSize int
FileContent varbinary(MAX)

By using varbinary(Max) , you can save any type of content in the database

Convert file from stream to byte array.
if you are using FileUpload control use fallowing..

byte[] fileContent = new byte[FileUpload1.PostedFile.ContentLength];
FileUpload1.PostedFile.InputStream.Read(fileContent, 0, FileUpload1.PostedFile.ContentLength);



将内容作为参数添加到存储过程中..




Add content as parameter to your stored procedure..

command.Parameters.Add("@FileContent", SqlDbType.VarBinary, -1).Value = fileContent;





更多细节请点击链接

http://csharpings.blogspot.com/2009/08/store-pdf-images-with-jpeg-bmp_19 .html



我希望它对你有帮助。





问候,

Jayant



for more detail go through link
http://csharpings.blogspot.com/2009/08/store-pdf-images-with-jpeg-bmp_19.html

I hope it helped you.


Regards,
Jayant


 //first create your table and connection then apply this code i hope it will work 
using System.Data.SqlClient;
using System.io;
public class FrmFiles {
    
    private string fname;
    
    private void BunifuImageButton3_Click(object sender, EventArgs e) {
        this.Close();
    }
    
    void Pics(void Dim, string[] fullfilename, void =, void OpenFileDialog1.FileName.Split, void \) {
        Fname = fullfilename.Last.ToString();
        FileStream Fsteream = new FileStream(OpenFileDialog1.FileName, FileMode.Open);
        BinaryReader BReader = new BinaryReader(Fsteream);
        MyFile = BReader.ReadBytes(Fsteream.Length);
        Fsteream.Close();
        BReader.Close();
        TxtName.Text = fname;
    }
    
    private void BtnBrowse_Click(object sender, EventArgs e) {
        OpenFileDialog1.ShowDialog();
        LbPicture.Text = OpenFileDialog1.FileName;
        Pics;
    }
    
        
    private void BtnSave_Click(object sender, EventArgs e) {
        pics();
        connection();
        sql = (" insert Into Tb_File values (\'" 
                    + (TxtNumber.Text + ("\',\'" 
                    + (fname + "\',@pic)"))));
        sqlcmd = new SqlCommand(sql, sqlcon);
        SqlParameter pic = new SqlParameter("@pic", SqlDbType.Image);
        pic.Value = MyFile;
        sqlcmd.Parameters.Add(pic);
        Sqlcmd.ExecuteNonQuery();
        MsgBox("Successful.............", MsgBoxStyle.Information, "Message");
    }
    
    private void BunifuThinButton21_Click(object sender, EventArgs e) {
        Connection();
        sqlcmd.CommandText = "Select Name,Files from Tb_File";
        sqldr = sqlcmd.ExecuteReader;
        sqldr.Read();

        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.FileName = sqldr.Item[0];
        FileStream FStream = new FileStream(sqldr(0).ToString, FileMode.Create, FileAccess.Write);
        saveFileDialog1.Filter = ("txt files (*.txt)|*.txt|Word 97-2003(*.doc)|*.doc|Word Document(*.doc)|*.doc|Excel 97-Excel 2003 Work" +
        "book(*.xls)|*.xls|Excel Document(*.xlsx)|*.xlsx|PDF Document(*.Pdf)|*.Pdf|All files (*.*)|*.*|Bitmap" +
        " Files|*.bmp" + "|Gif Files|*.gif|JPEG Files|*.jpg");
        saveFileDialog1.FilterIndex = 7;
        saveFileDialog1.RestoreDirectory = true;
        if ((saveFileDialog1.ShowDialog() == DialogResult.OK)) {
            FStream = saveFileDialog1.OpenFile();
            if (FStream) {
                IsNot;
                null;
                byte[] filebytes = ((byte[])(sqldr(1)));
                FStream.Write(filebytes, 0, filebytes.Length);
                FStream.Close();
            }
            
        }

    }
}


这篇关于如何将word文件上传和下载到SQL Server数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆