コード例 #1
0
            public void ProcessDB(string TableName)
            {
                Constants        constants  = new Constants();
                SubRoutine       sub        = new SubRoutine();
                DataSet          dsCount    = new DataSet();
                DataSet          dsProcess  = new DataSet();
                OleDbCommand     dbCmd      = new OleDbCommand();
                OleDbDataAdapter dbAd       = new OleDbDataAdapter();
                string           base64Text = "";

                OleDbConnection conn2 = new OleDbConnection
                {
                    //ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                    //                   "Data source= D:\\This PC\\Pars Info\\Rick PC\\rick laptop\\pars_prod_2013_pic1.accdb"
                    ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                                       "Data source=D:\\Users\\dmand\\projects\\AcccesImages\\AcccesImages\\bin\\Debug\\pars_prod_2013.accdb"
                };

                OleDbCommandBuilder builder = new OleDbCommandBuilder(dbAd);

                try
                {
                    conn2.Open();
                    dbCmd = sub.DBCommand(TableName, conn2);
                    dbAd  = new OleDbDataAdapter(dbCmd);
                    dbAd.Fill(dsCount, TableName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to process dataset.\n" + ex.Message, "ProcessDB");
                }

                //  set file path
                string fullpath = @constants.SystemPath + @"\";

                fullpath = fullpath.Substring(6);

                try
                {
                    double FirstHalf     = 0;
                    double SecondHalf    = 0;
                    double ReadCount     = 0;
                    double StartCount    = 0;
                    int    StartCountInt = 0;
                    int    OddNumber     = 0;

                    //  get record count of table
                    double RowCount = dsCount.Tables[0].Rows.Count;

                    // delete existing
                    for (int xInt = 1; xInt < 3; xInt++)
                    {
                        if (File.Exists(fullpath + @constants.PicTable + xInt + ".json"))
                        {
                            File.Delete(fullpath + @constants.PicTable + xInt + ".json");
                        }
                    }

                    //  set variables for even or odd record count
                    if (RowCount % 2 == 0)
                    {
                        FirstHalf  = RowCount / 2;
                        SecondHalf = RowCount / 2;
                        ReadCount  = FirstHalf;
                    }
                    else
                    {
                        FirstHalf  = Math.Truncate(RowCount / 2);
                        SecondHalf = Math.Round(RowCount / 2);

                        if (FirstHalf + SecondHalf != RowCount)
                        {
                            SecondHalf = SecondHalf + 1;
                        }

                        ReadCount = FirstHalf;
                        OddNumber = -1;
                    }

                    DataSet ds = new DataSet();
                    //dbAd.Fill(ds, (int)StartCount, (int)ReadCount, @constants.PicTable + xInt);
                    dbAd.Fill(ds, (int)StartCount, (int)ReadCount, TableName);

                    //  loop through tables
                    //for (int xInt = 1; xInt < 3; xInt++)
                    //for (int xInt = 1; xInt < dsCount.Tables[0].Rows.Count; xInt++)
                    for (int xInt = 1; xInt < 3; xInt++)
                    {
                        //DataSet ds = new DataSet();
                        //dbAd.Fill(ds, (int)StartCount, (int)ReadCount, @constants.PicTable + xInt);

                        //for (int yInt = 0; yInt < ds.Tables[0].Rows.Count; yInt++)
                        for (StartCount = 0; StartCount < ReadCount; StartCount++)
                        {
                            StartCountInt = (int)StartCount;
                        }
                        {
                            //  delete image
                            if (File.Exists(fullpath + "image_temp.jpg"))
                            {
                                File.Delete(fullpath + "image_temp.jpg");
                            }

                            if (File.Exists(fullpath + "image_temp_compressed.jpg"))
                            {
                                File.Delete(fullpath + "image_temp_compressed.jpg");
                            }

                            //  save image to file
                            byte[] blobpic = (byte[])ds.Tables[0].Rows[StartCountInt]["PicBlob"];
                            File.WriteAllBytes(fullpath + "image_temp.jpg", blobpic);

                            //  compress image
                            base64Text = Convert.ToBase64String(blobpic);
                            Image img = Base64ToImage(base64Text);

                            //  get image
                            Image imgFromFile = Image.FromFile(fullpath + "image_temp.jpg");
                            DefaultCompresion(imgFromFile, fullpath, "image_temp_compressed.jpg");
                            imgFromFile.Dispose();      //  release file resources

                            //  read compressed image in as base64
                            string ImageAsBase64 = ImageToBase64(fullpath, "image_temp_compressed.jpg");


                            //  update ds image filed with new compressed base64
                            DataTable    picTable  = ds.Tables[0];
                            OleDbCommand cmdUpdate = new OleDbCommand
                            {
                                Connection = conn2,
                                //CommandText = "UPDATE tblPicture1 SET PicName = 'me' WHERE IDPictures = " + ds.Tables[0].Rows[yInt]["IDPictures"]
                                CommandText = "UPDATE " + TableName + " SET PicBlob = '" + ImageAsBase64 + "' WHERE IDPictures = " + ds.Tables[0].Rows[StartCountInt]["IDPictures"]
                            };
                            cmdUpdate.ExecuteNonQuery();

                            ////  serialize json
                            //string output2 = JsonConvert.SerializeObject(ds, Formatting.Indented);

                            ////  write to file
                            //File.AppendAllText(fullpath + TableName + "_" + xInt + ".json", output2);

                            ////  set second half
                            //ReadCount = SecondHalf;
                            //StartCount = SecondHalf + OddNumber;

                            //  images to base64
                            //sub.ProcessPictures(ds, TableName, constants.SystemPath, constants.PicFile);

                            ds.Dispose();
                        }

                        //  serialize json
                        string output2 = JsonConvert.SerializeObject(ds, Formatting.Indented);

                        //  write to file
                        File.AppendAllText(fullpath + TableName + "_" + xInt + ".json", output2);


                        ////  set second half
                        ReadCount  = SecondHalf;
                        StartCount = SecondHalf + OddNumber;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed to process dataset.\n" + ex.Message);
                }
            }