Save and retrieve Word document from Database in Windows Application using C# and VB.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

Hi! I want save ms word file into database using c# windows application. After save when I click retrieve button then save file into local disk c without open word.

output path: c:\filename from dadabase.

You are viewing reply posted by: pandeyism 4 months ago.
Posted 4 months ago

Hey PRA,

Please refer below smaple.

Namespaces

C#

using System.IO;
using System.Data.SqlClient;

VB.Net

Imports System.Data.SqlClient
Imports System.IO

Code

C#

private void Choose_Click(object sender, EventArgs e)
{
    string saveDirectory = @"D:\File\";
    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 = @"Data Source=.\SQL2005;Database=dbFiles; uid=sa; pwd=pass";
            SqlConnection conn = new SqlConnection(constr);
            string strQuery = "insert into tblFilesPath(Name, Path) values (@Name, @Path)";
            SqlCommand cmd = new SqlCommand(strQuery, conn);
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filepath.filename;
            cmd.Parameters.Add("@Path", SqlDbType.NVarChar).Value = filepath.fileSavePath;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
}
private void Retrieve_Click(object sender, EventArgs e)
{
    string destFile = @"D:\Folder\";
    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;
}

VB.Net

Private Sub Choose_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim saveDirectory As String = "D:\File\"
    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 = "Data Source=.\SQL2005;Database=dbFiles; uid=sa; pwd=pass"
            Dim conn As SqlConnection = New SqlConnection(constr)
            Dim strQuery As String = "insert into tblFilesPath(Name, Path) values (@Name, @Path)"
            Dim cmd As SqlCommand = New SqlCommand(strQuery, conn)
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filepath.filename
            cmd.Parameters.Add("@Path", 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 = "D:\Folder\"

    If Not Directory.Exists(destFile) Then
        Directory.CreateDirectory(destFile)
    End If
    Dim sourcepath As String = filepath.fileSavePath
    File.Copy(sourcepath, destFile & filepath.filename, True)
End Sub
Class filepath
    Public Shared fileSavePath As String
    Public Shared filename As String
End Class