How to save .EXE File DATabase in Binary Format

private void SetFileFromDB(string tableName, string binFieldName, string idFieldName, string idFieldValue, string filepath)
        {
            try
            {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append(" SELECT ");
                strBuilder.Append(binFieldName);
                strBuilder.Append(" FROM ");
                strBuilder.Append(tableName);
                strBuilder.Append(" WHERE ");
                strBuilder.Append(idFieldName);
                strBuilder.Append(" = ");
                strBuilder.Append(idFieldValue);

                DataTable dt = new DataTable();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = new SqlConnection(Connection.ConnectionString);
                cmd.CommandTimeout = 0;
                cmd.CommandText = strBuilder.ToString();
                cmd.CommandType = CommandType.Text;
                if (cmd.Connection.State != ConnectionState.Open)
                {
                    cmd.Connection.Open();
                }
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
                cmd.Dispose();



                if (!string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
                {
                    byte[] fileData = (byte[])dt.Rows[0][0];

                  //  lbl_Download.Content = "0  Of  " + (fileData.Length / 1024).ToString() + " Kbs.";

                    FileStream fStream = new FileStream(filepath, FileMode.Create);
                    for (int i = 0; i < fileData.Length; i = i + 50)
                    {
                       // lbl_Download.Content = (i >= 1024 ? Convert.ToString((i + 1) / 1024) + " Kbs. " : (i + 1).ToString() + " Bytes ") + " Of " + (fileData.Length / 1024).ToString() + " Kbs.";
                        if ((fileData.Length - i) >= 50)
                        {
                            fStream.Write(fileData, i, 50);
                        }
                        else
                        {
                            fStream.Write(fileData, i, (fileData.Length - i));
                        }
                    }                 
                   
                    fStream.Close();

                    //Unzip code
                    using (ZipFile zip = ZipFile.Read(filepath))
                    {
                        ZipEntry entry = zip["VISION.exe"];
                        string baseDir = filepath.Substring(0, filepath.LastIndexOf("\\"));
                        FileInfo ExeFile = new FileInfo(baseDir + "\\VISION.exe");
                        if (ExeFile.Exists)
                        {
                            ExeFile.Delete();
                        }                      
                        entry.Extract(baseDir);   
                    }

                    if (fileData.Length > 1024)
                    {
                        lbl_Mesg.Visibility = Visibility.Visible;

                        StringBuilder sbMesg = new StringBuilder();
                        sbMesg.Append(fileData.Length / 1024);
                        sbMesg.Append(" KBs. of ");
                        sbMesg.Append(fileData.Length / 1024);
                        sbMesg.Append("KBs. download \ncompleted successfully.");

                        //lbl_Mesg.Content = "Updates download completed \nsuccessfully. Press Continue to \nrun application or Exit to quit.";
                        lbl_Mesg.Text = sbMesg.ToString();
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Online Server Not Found. Please Try Again!", "VISION ERP");
                 //Application.Current.Shutdown();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sorry for your inconvenience. Please Try Again!" + filepath, "VISION ERP");
            }
        }

Comments

Popular posts from this blog

ASP.NET Session States in SQL Server Mode

How to find client's MAC Address in Asp.Net and C#.net

Use XML Data For Save in Database