'How to upload into and download from SQLite database doc file in C# .NET?

I have decided to develop desktop program with usage of SQLite datatbase and with help of C# programming language. One of the necessary functions that must be present is uploding the doc file into SQLite database and downloding the same type of files from it. But I cannot download the word document from db programically. It does not appear in the needed directory. So I have a doubt about the correctness of the code.

Accordingly the plan, first and foremost I should create doc file and perform some manipulations on it. It's another topic and it's related to DocX. The file could be create in some particular directory, but after full editing the file should be deleted and uploaded previously to SQLite db in the field marked with blob data type. In any moment user must have ability to download the document into some chosen directory.

byte[] file;
            using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read))
            {
                using (var reader = new BinaryReader(stream))
                {
                    file = reader.ReadBytes((int)stream.Length);
                }
            }

            SQLiteConnection m_dbConn = new SQLiteConnection();
            try
            {
                GetDB cl = new GetDB();
                m_dbConn = new SQLiteConnection("Data Source=" + cl.dbFileName + ";Version=3;");
                m_dbConn.Open();

                if (m_dbConn.State != ConnectionState.Open)
                {
                    MessageBox.Show("Open connection with database");
                    return;
                }
                else
                {
                    string Q = "INSERT INTO file_" + Convert.ToString(IdOrganization) +
                        "(id, file_contract) VALUES(" + last_rowid + ", @File);";
                    SQLiteCommand m_sqlCmd = new SQLiteCommand();
                    m_sqlCmd.Connection = m_dbConn;
                    m_sqlCmd.CommandText = Q;
                    m_sqlCmd.Parameters.Add("@File", (DbType)SqlDbType.VarBinary, file.Length).Value = file;
                    m_sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("1" + ex.Message);
            }
            m_dbConn.Close();

            File.Delete("Договір_підряду.doc");

            m_dbConn = new SQLiteConnection();
            try
            {
                GetDB cl = new GetDB();
                m_dbConn = new SQLiteConnection("Data Source=" + cl.dbFileName + ";Version=3;");
                m_dbConn.Open();

                if (m_dbConn.State != ConnectionState.Open)
                {
                    MessageBox.Show("Open connection with database");
                    return;
                }
                else
                {
                    using (var sqlQuery = new SQLiteCommand(@"SELECT file_contract FROM file_" +
                    Convert.ToString(IdOrganization) + " WHERE " +
                    "id = @varID", m_dbConn))
                    {
                        sqlQuery.Parameters.AddWithValue("@varID", last_rowid);
                        using (var sqlQueryResult = sqlQuery.ExecuteReader())
                            if (sqlQueryResult != null)
                            {
                                sqlQueryResult.Read();
                                var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
                                sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
                                using (var fs = new FileStream(@"C:\Users\Default\Desktop\1.doc",
                                    FileMode.Create, FileAccess.Write))
                                    fs.Write(blob, 0, blob.Length);
                            }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("2" + ex.Message);
            }

I have no opportunity to download db file because of the exception "Specified cast is not valid". This error occurs on the part where I'm trying to download the file from db. (Where the digit 2 is present in the text of MessageBox) Probably the value of the field is empty, but according to the Database Management System the column is not null. Also there's no complaints about the uploading document into database.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source