Data files are various types and it's extension is also deferent such as ms word file extension is .Doc, Excel file extension Xls and etc. so we design the data base with three columns. first is file_name, second is content_type and last data.
There are various steps to store data file in to data base. that are given bellow:
Design the window form, it contain one TextBox and two Buttons:
design Database, it contain three fields.
OpenDialog1 coding, that open the new dialog and select the file do want to store: Public Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click Dim ofd As OpenFileDialog = New OpenFileDialog() ofd.ShowDialog() TextBox1.Text = ofd.FileName Dim path As String = TextBox1.Text Dim file_nameAs String = ofd.FileName End Sub
SaveDialog1 coding. to use to store file in binary format in to database: Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click Try Dim strAs String ="Data Source=MCNDESKTOP14;Initial Catalog=studentinfo; uid=sa;pwd=wintellect" Dim conAs SqlConnection con = New SqlConnection(str) con.Open() Dim pathAs String = TextBox1.Text Dim fs As System.IO.FileStream = New System.IO.FileStream(path, FileMode.Open, FileAccess.Read) Dim br As System.IO.BinaryReader = New System.IO.BinaryReader(fs) Dim bytesAs Byte() = br.ReadBytes(Convert.ToInt32(fs.Length)) br.Close() fs.Close() Dim strQueryAs String ="insert into info09(name, contentType, data) values (@name, @contentType, @data)" Dim cmd As SqlCommand = New SqlCommand(strQuery) cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = "application/vnd.ms-excel" cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes InsertUpdateData(cmd) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
How to store Excel file in Database
Transaction scheduling in ADO.NET