Save and Retrieve Files from SQL Server Database using ASP. Net. Files can be easily saved in the SQL Server Database Saving it in database makes it easily manageable. Here I will explain how to save and retrieve files from database. Database Design. Here I have created a Database called db. Files and it has a table called tbl. Files. It has 4 Fields. The complete description is available in the Figure below. As you can see above for the id field I have set Identity Specification true, so that it automatically increments itself. UploadFile/009464/how-to-export-gridview-to-pdf-using-itextsharp-library/Images/namspaces.jpg' alt='Itextsharp Fill Pdf Form Fields Vb.Net' title='Itextsharp Fill Pdf Form Fields Vb.Net' />Field. Relevanceid. Identification Number Name. File Name. Content Type. Content Type for the file. Im using iTextSharp. Some rows contain multiple lines and it may not fit within the current page, so iTextSharp. Read and Write CSV File in VB. NET We are using the FileIo namespace for writing CSV file and OLEDB Ado. CSV file. Search the worlds information, including webpages, images, videos and more. Google has many special features to help you find exactly what youre looking for. Here Mudassar Ahmed Khan has explained how to save file as binary data inside SQL Server database and also how to retreive them and reconvert to their respective. Remove Delete Duplicate Rows Records from DataTable using C and VB. Net Here Mudassar Ahmed Khan has explained, how to remove delete duplicate rows records. Data. File stored as Binary Data. Content Type. Depending on the type of the file below are the content types. File Type. Content Type. Word Documentapplicationvnd. Excel Documentapplicationvnd. JPEG Imageimagejpeg. Portable Document Formatapplicationpdf. In the source code attached I have added the database files. You will need to attach it to your SQL Server. Connection String. Below is the connection string to the database. You can modify it to suit yourslt connection. Strings lt addnamecon. Stringconnection. StringData Source. SQLEXPRESS databasedb. Files Integrated Securitytrue lt connection. Strings Reading the File. The files will be read into a File Stream and then the File Stream will be converted into byte array using Binary. Reader in order to save into the database table. C Read the file and convert it to Byte Arraystring file. Path Server. Map. PathAPPDATATest. Doc. Path. Get. File. Namefile. Path File. Stream fs new. File. Streamfile. Path, File. Mode. Open, File. Access. Read Binary. Reader br new. Binary. Readerfs Byte bytes br. Read. BytesInt. Length br. Close fs. Close VB. Net Read the file and convert it to Byte Array. Dim file. Path As. String Server. Map. PathAPPDATATestxls. Dim filename As. String Path. Get. File. Namefile. PathDim fs As File. Stream New File. Streamfile. Path, File. Mode. Open, File. Access. ReadDim br As Binary. Reader New Binary. ReaderfsDim bytes As. Byte br. Read. BytesConvert. To. Int. 32fs. Lengthbr. Closefs. CloseSaving the File to Database. Once the File is converted into Byte Array it will be inserted into the database. The File Name, File Content Type and the Binary data which resembles the file are stored in the database. The figure below shows the data being stored in the table. Cinsert the file into databasestring str. Query insert into tbl. FilesName, Content. Type, Data values Name, Content. Type, Data Sql. Command cmd new. Sql. Commandstr. Query cmd. Parameters. AddName, Sql. Db. Type. Var. Char. Value filename cmd. Parameters. AddContent. Type, Sql. Db. Type. Var. Char. Value applicationvnd. Parameters. AddData, Sql. Db. Type. Binary. Frozen Il Regno Di Ghiaccio Film Completo'>Frozen Il Regno Di Ghiaccio Film Completo. Value bytes Insert. Update. Datacmd VB. Netinsert the file into database. Dim str. Query As. String insert into tbl. FilesName, Content. Type, Data values Name, Content. Type, DataDim cmd As Sql. Command New Sql. Commandstr. Querycmd. Parameters. AddName, Sql. Db. Type. Var. Char. Value filenamecmd. Parameters. AddContent. Type, Sql. Db. Type. Var. Char. Value applicationvnd. Parameters. AddData, Sql. Db. Type. Binary. Value bytes. Insert. Update. DatacmdAnd the function Insert. Update. Data accepts the Sql. Command object, executes the query andinserts the data into the database. Cprivate. Boolean Insert. Update. DataSql. Command cmd String str. Conn. String System. Configuration. Configuration. Manager . Connection. Stringscon. String. Connection. String Sql. Connection con new. Sql. Connectionstr. Conn. String cmd. Command. Type Command. Type. Text cmd. Connection con try con. Open cmd. Execute. Non. Query returntrue catch Exception ex Response. Writeex. Message returnfalse finally con. Close con. Dispose VB. Net. Public. Function Insert. Update. DataBy. Val cmd As Sql. Command As. Boolean Dim str. Conn. String As. String System. Configuration. Configuration. Manager. Connection. Stringscon. String. Connection. String Dim con As. New Sql. Connectionstr. Conn. String cmd. Command. Type Command. Type. Text cmd. Connection con Try con. Open cmd. Execute. Non. Query Return. True Catch ex As Exception Response. Writeex. Message Return. False Finally con. Close con. Dispose End. Try. End. Function. For more details on how to Execute Parameterized queries refer Using Parameterized queries to prevent SQL Injection Attacks in SQL Server. Retrieving the File from Database. To retrieve the file from the database, a select query is executed and the ID of the file is passed as the parameter. The command object is prepared and is passed to the Get. Data which returns the Data. Table which contains the desired file data. Then the Data. Table is passed to the download function which starts the download of the file. Cstring str. Query select Name, Content. Type, Data from tbl. Files where idid Sql. Command cmd new. Sql. Commandstr. Query cmd. Parameters. Addid, Sql. Db. Type. Int. Value 1 Data. Table dt Get. Datacmd if dt null downloaddt VB. Net Dim str. Query As. String select Name, Content. Type, Data from tbl. Files where idid Dim cmd As Sql. Command New Sql. Commandstr. Query cmd. Parameters. Addid, Sql. Db. Type. Int. Value 1 Dim dt As Data. Table Get. Datacmd If dt Is. Not. Nothing. Then downloaddt End. If. Below is the code for the Get. Data function. It is a simple function that executes the Select query. More details on the function you can refer here. Cprivate. Data. Table Get. DataSql. Command cmd Data. Table dt new. Data. Table String str. Stick Games Sift Heads World Act 6 on this page. Conn. String System. Configuration. Configuration. Manager . Connection. Stringscon. String. Connection. String Sql. Connection con new. Sql. Connectionstr. Conn. String Sql. Data. Adapter sda new. Sql. Data. Adapter cmd. Command. Type Command. Type. Text cmd. Connection con try con. Open sda. Select. Command cmd sda. Filldt return dt catch returnnull finally con. Close sda. Dispose con. Dispose VB. Net. Public. Function Get. DataBy. Val cmd As Sql. Command As Data. Table Dim dt As. New Data. Table Dim str. Conn. String As. String System. Configuration . Configuration. Manager. Connection. Stringscon. String. Connection. String Dim con As. New Sql. Connectionstr. Conn. String Dim sda As. New Sql. Data. Adapter cmd. Command. Type Command. Type. Text cmd. Connection con Try con. Open sda. Select. Command cmd sda. Filldt Return dt Catch ex As Exception Response. Writeex. Message Return. Nothing Finally con. Close sda. Dispose con. Dispose End. Try. End. Function. Download the File. Here is the function which initiates the download of file. It basically reads the file contents into a Byte array and also gets the file name and the Content Type. Then it writes the bytes to the response using Response. Binary. Write. Cprivatevoid download Data. Table dt Byte bytes Bytedt. Rows0Data Response. Buffer true Response. Mikrotik Full License Level 6 here. Charset Response.