In this article I will explain with an example, how to save (insert) file in SQL Server Database in Windows Forms (WinForms) Application using C# and VB.Net.
The file will be uploaded using OpenFileDialog Box to database table in Binary format in Windows Application using C# and VB.Net.
Database
This article makes use of a table named tblFiles whose schema is defined as follows.
Note: You can download the database table SQL by clicking the download link below.
Form Design
The following Form consists of a Button control.
Namespaces
You need to import the following namespaces.
C#
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.IO
Imports System.Configuration
Imports System.Data.SqlClient
Inserting file in Database
The following event handler is executed, when the Select File Button is clicked.
Inside this event handler, an instance of OpenFileDialog class in created.
Then, after selecting the file using the OpenFileDialog Box, the file name is read from the FileName property of the OpenFileDialog Box.
Next, the Byte Array is determined using the ReadAllBytes method of File class and Content Type (MIME type) is determined based on the file extension.
Finally, then file name, the Byte Array and the Content Type are inserted into the Database table.
C#
private void btn_SelectClick(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = Path.GetFileName(openFileDialog1.FileName);
byte[] bytes = File.ReadAllBytes(openFileDialog1.FileName);
string contentType = "";
switch (Path.GetExtension(fileName))
{
case ".jpg":
contentType = "image/jpeg";
break;
case ".png":
contentType = "image/png";
break;
case ".gif":
contentType = "image/gif";
break;
case ".bmp":
contentType = "image/bmp";
break;
case ".pdf":
contentType = "application/pdf";
break;
case ".doc":
contentType = "application/vnd.ms-word";
break;
case ".docx":
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
}
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO tblFiles (Name, ContentType, Data) VALUES (@Name, @ContentType, @Data)", con))
{
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
VB.Net
Private Sub btn_SelectClick(ByVal sender As Object, ByVal e As EventArgs)
Using openFileDialog1 As OpenFileDialog = New OpenFileDialog()
If openFileDialog1.ShowDialog() = DialogResult.OK Then
Dim fileName As String = Path.GetFileName(openFileDialog1.FileName)
Dim bytes As Byte() = File.ReadAllBytes(openFileDialog1.FileName)
Dim contentType As String = ""
Select Case Path.GetFileName(fileName)
Case ".jpg"
contentType = "image/jpeg"
Case ".png"
contentType = "image/png"
Case ".gif"
contentType = "image/gif"
Case ".bmp"
contentType = "image/bmp"
Case ".pdf"
contentType = "application/pdf"
Case ".doc"
contentType = "application/vnd.ms-word"
Case ".docx"
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
End Select
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO tblFiles (Name, ContentType, Data) VALUES (@Name, @ContentType, @Data)", con)
cmd.Parameters.AddWithValue("@Name", fileName)
cmd.Parameters.AddWithValue("@ContentType", contentType)
cmd.Parameters.AddWithValue("@Data", bytes)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
End Using
End Sub
Screenshot
The inserted files in Database table
Demo
Downloads