ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Retrieve binary file path from Database and Save in Directory in Windows Application using C# and VB.Net<p>Hey <a class="username" rel="PRA">PRA</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">private void button1_Click(object sender, EventArgs e) { string saveDirectory = @&#34;D:\File\&#34;; using (OpenFileDialog openFileDialog1 = new OpenFileDialog()) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { if (!Directory.Exists(saveDirectory)) { Directory.CreateDirectory(saveDirectory); } filepath.filename = Path.GetFileName(openFileDialog1.FileName); filepath.fileSavePath = Path.Combine(saveDirectory, filepath.filename); string fileName = openFileDialog1.FileName; byte[] bytes = File.ReadAllBytes(fileName); string contentType = &#34;&#34;; switch (Path.GetExtension(fileName)) { case &#34;.docx&#34;: contentType = &#34;application/vnd.ms-word&#34;; break; case &#34;.pdf&#34;: contentType = &#34;application/pdf&#34;; break; case &#34;.gif&#34;: contentType = &#34;image/gif&#34;; break; case &#34;.bmp&#34;: contentType = &#34;image/bmp&#34;; break; } File.Copy(openFileDialog1.FileName, filepath.fileSavePath, true); //insert the file into database string constr = @&#34;Data Source=.\SQL2005;Database=dbFiles; uid=sa; pwd=pass&#34;; SqlConnection conn = new SqlConnection(constr); string strQuery = &#34;insert into tblfiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)&#34;; SqlCommand cmd = new SqlCommand(strQuery, conn); cmd.Parameters.Add(&#34;@Name&#34;, SqlDbType.VarChar).Value = filepath.filename; cmd.Parameters.Add(&#34;@ContentType&#34;, SqlDbType.NVarChar).Value = contentType; cmd.Parameters.AddWithValue(&#34;@Data&#34;, bytes); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } } private void button2_Click(object sender, EventArgs e) { byte[] bytes; string fileName; string constr = @&#34;Data Source=.\SQL2005;Database=dbFiles; uid=sa; pwd=pass&#34;; SqlConnection conn = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;SELECT TOP 1 Id, Name, ContentType, Data FROM tblfiles ORDER BY Id DESC&#34;, conn); conn.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); foreach (DataRow dr in dt.Rows) { fileName = dr[&#34;Name&#34;].ToString(); bytes = (byte[])dr[&#34;Data&#34;]; File.WriteAllBytes(@&#34;C:\Users\anand\Desktop\&#34; + fileName, bytes); conn.Close(); } } public static class filepath { public static string fileSavePath; public static string filename; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim saveDirectory As String = &#34;D:\File\&#34; Using openFileDialog1 As OpenFileDialog = New OpenFileDialog() If openFileDialog1.ShowDialog() = DialogResult.OK Then If Not Directory.Exists(saveDirectory) Then Directory.CreateDirectory(saveDirectory) End If filepath.filename = Path.GetFileName(openFileDialog1.FileName) filepath.fileSavePath = Path.Combine(saveDirectory, filepath.filename) Dim fileName As String = openFileDialog1.FileName Dim bytes As Byte() = File.ReadAllBytes(fileName) Dim contentType As String = &#34;&#34; Select Case Path.GetExtension(fileName) Case &#34;.docx&#34; contentType = &#34;application/vnd.ms-word&#34; Case &#34;.pdf&#34; contentType = &#34;application/pdf&#34; Case &#34;.gif&#34; contentType = &#34;image/gif&#34; Case &#34;.bmp&#34; contentType = &#34;image/bmp&#34; End Select File.Copy(openFileDialog1.FileName, filepath.fileSavePath, True) Dim constr As String = &#34;Data Source=.\SQL2005;Database=dbFiles; uid=sa; pwd=pass&#34; Dim conn As SqlConnection = New SqlConnection(constr) Dim strQuery As String = &#34;insert into tblfiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)&#34; Dim cmd As SqlCommand = New SqlCommand(strQuery, conn) cmd.Parameters.Add(&#34;@Name&#34;, SqlDbType.VarChar).Value = filepath.filename cmd.Parameters.Add(&#34;@ContentType&#34;, SqlDbType.NVarChar).Value = contentType cmd.Parameters.AddWithValue(&#34;@Data&#34;, bytes) conn.Open() cmd.ExecuteNonQuery() conn.Close() End If End Using End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Dim bytes As Byte() Dim fileName As String Dim constr As String = &#34;Data Source=.\SQL2005;Database=dbFiles; uid=sa; pwd=pass&#34; Dim conn As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(&#34;SELECT TOP 1 Id, Name, ContentType, Data FROM tblfiles ORDER BY Id DESC&#34;, conn) conn.Open() Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) For Each dr As DataRow In dt.Rows fileName = dr(&#34;Name&#34;).ToString() bytes = CType(dr(&#34;Data&#34;), Byte()) File.WriteAllBytes(&#34;C:\Users\anand\Desktop\&#34; &amp; fileName, bytes) conn.Close() Next End Sub Class filepath Public Shared fileSavePath As String Public Shared filename As String End Class</pre>https://www.aspforums.net:443/Threads/264724/Retrieve-binary-file-path-from-Database-and-Save-in-Directory-in-Windows-Application-using-C-and-VBNet/https://www.aspforums.net:443/Threads/264724/Retrieve-binary-file-path-from-Database-and-Save-in-Directory-in-Windows-Application-using-C-and-VBNet/Thu, 22 Nov 2018 04:01:26 GMT