Hi Tevin,
Using the below Article i have created the example.
Please refer below Sample.
HTML
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="Save" />
Namespace
C#
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Code
C#
protected void Save(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT Name, Data FROM tblFiles";
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
byte[] bytes = (byte[])sdr["Data"];
string filePath = "~/Files/" + Path.GetFileName(sdr["Name"].ToString());
File.WriteAllBytes(Server.MapPath(filePath), bytes);
}
}
con.Close();
}
}
}
VB.Net
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT Name, Data FROM tblFiles"
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Dim bytes As Byte() = CType(sdr("Data"), Byte())
Dim filePath As String = "~/Files/" & Path.GetFileName(sdr("Name").ToString())
File.WriteAllBytes(Server.MapPath(filePath), bytes)
End While
End Using
con.Close()
End Using
End Using
End Sub