Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
please help me.I want this code very badly.
Posted
Updated 18-Apr-19 1:30am

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..

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

Add content as parameter to your stored procedure..

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


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
 
Share this answer
 
 //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();
            }
            
        }

    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900