ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Save and retrieve Word document from Database in Windows Application using C# and VB.Net<p>Hey <a class="username" rel="PRA"> PRA</a>,</p> <p>Please refer below smaple.</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.Data.SqlClient Imports System.IO</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">private void Choose_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); 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 tblFilesPath(Name, Path) values (@Name, @Path)&#34;; SqlCommand cmd = new SqlCommand(strQuery, conn); cmd.Parameters.Add(&#34;@Name&#34;, SqlDbType.VarChar).Value = filepath.filename; cmd.Parameters.Add(&#34;@Path&#34;, SqlDbType.NVarChar).Value = filepath.fileSavePath; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } } private void Retrieve_Click(object sender, EventArgs e) { string destFile = @&#34;D:\Folder\&#34;; if (!Directory.Exists(destFile)) { Directory.CreateDirectory(destFile); } string sourcepath = filepath.fileSavePath; File.Copy(sourcepath, destFile + filepath.filename, true); } 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 Choose_Click(ByVal sender As Object, ByVal e As EventArgs) 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) 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 tblFilesPath(Name, Path) values (@Name, @Path)&#34; Dim cmd As SqlCommand = New SqlCommand(strQuery, conn) cmd.Parameters.Add(&#34;@Name&#34;, SqlDbType.VarChar).Value = filepath.filename cmd.Parameters.Add(&#34;@Path&#34;, SqlDbType.NVarChar).Value = filepath.fileSavePath conn.Open() cmd.ExecuteNonQuery() conn.Close() End If End Using End Sub Private Sub Retieve_Click(ByVal sender As Object, ByVal e As EventArgs) Dim destFile As String = &#34;D:\Folder\&#34; If Not Directory.Exists(destFile) Then Directory.CreateDirectory(destFile) End If Dim sourcepath As String = filepath.fileSavePath File.Copy(sourcepath, destFile &amp; filepath.filename, True) End Sub Class filepath Public Shared fileSavePath As String Public Shared filename As String End Class</pre> <p>&nbsp;</p>, 19 Nov 2018 02:05:22 GMT