Advertisement
Guest User

fsdfsdf

a guest
Mar 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.99 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.OleDb;
  4. using System.Windows.Forms;
  5.  
  6. namespace ExLab
  7. {
  8.  
  9.  
  10.     partial class FMSSDB_NewDataSet
  11.     {
  12.         partial class FILESDataTable
  13.         {
  14.             public override void EndInit()
  15.             {
  16.                 base.EndInit();  //super class
  17.                 FILESRowChanged += MyRowChanged;
  18.             }
  19.             //  static int i=0;
  20.             OleDbDataReader reader;
  21.             public int GetNextCluster(OleDbCommand cmd,int currentCluster)
  22.             {
  23.                
  24.                 cmd.CommandText = "SELECT NextCluster, ClusterStatus FROM CLUSTERS WHERE [#C]=" + currentCluster;
  25.                 reader = cmd.ExecuteReader();
  26.                 reader.Read();
  27.  
  28.                 if ("E".Equals(reader[1]))
  29.                 {
  30.                     reader.Close();
  31.                     return -1;
  32.                 }
  33.                 Console.WriteLine(reader[0]);
  34.                 int nextCluster = (int)reader[0];
  35.                 reader.Close();
  36.                 return nextCluster;
  37.             }
  38.  
  39.             public Tuple<int,int> GetClusterChain(OleDbCommand cmd,int currentCluster)
  40.             {
  41.                 int noClusters=1;
  42.  
  43.                 while (true)
  44.                 {
  45.                     int next = GetNextCluster(cmd, currentCluster);
  46.                     if (next == -1)
  47.                         break;
  48.                     ++noClusters;
  49.                     currentCluster = next;
  50.                 }
  51.                 return new Tuple<int, int>(currentCluster, noClusters);
  52.             }
  53.  
  54.  
  55.             public void LinkClusterAAfterB(OleDbCommand cmd,int clusterA, int clusterB)
  56.             {
  57.                 cmd.CommandText = "UPDATE CLUSTERS SET NextCluster=" + clusterA + ",CLUSTER STATUS='C' WHERE [#C]=" + clusterB;
  58.                 cmd.ExecuteNonQuery();
  59.             }
  60.  
  61.  
  62.             public void MyRowChanged(object sender, FILESRowChangeEvent ev){
  63.  
  64.                 //Console.WriteLine("bla"+(i++));
  65.                 //Console.WriteLine(ev.Row.File);
  66.                 //Console.WriteLine(ev.Row[0]);
  67.                 String conString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source =| DataDirectory |\\FMSSDB_New.accdb";
  68.                 OleDbConnection con = new OleDbConnection(conString);
  69.                 OleDbCommand cmd = new OleDbCommand();
  70.                 OleDbDataReader reader;
  71.                 cmd.CommandType =CommandType.Text;
  72.                 cmd.Connection = con;
  73.  
  74.                 con.Open();
  75.                 cmd.CommandText = "SELECT FileSize FROM FILES WHERE [#F]=" + ev.Row[0];
  76.                 reader = cmd.ExecuteReader();
  77.                 reader.Read();
  78.                 int oldFileSize = (int)((float)reader[0]);
  79.                 reader.Close();
  80.                 int newFileSize = (int)ev.Row.FileSize;
  81.                 if(newFileSize==oldFileSize)
  82.                 {
  83.                     con.Close();
  84.                     return;
  85.                 }
  86.  
  87.                 cmd.CommandText = "SELECT FirstFreeCluster FROM DRIVES WHERE [#LD]=" + ev.Row.LogicDrive;
  88.                 reader = cmd.ExecuteReader();
  89.                 reader.Read();
  90.                 int firstFreeCluster = (int)reader[0];
  91.                 reader.Close();
  92.  
  93.                 Tuple<int, int> r = GetClusterChain(cmd, firstFreeCluster);
  94.                 int noFreeClusters = r.Item2;
  95.                 int lastFreeCluster = r.Item1;
  96.  
  97.                 r = GetClusterChain(cmd, ev.Row.FileStart);
  98.                 int noFileClusters = r.Item2;
  99.                 int lastFileCluster = r.Item1;
  100.  
  101.                 //    Console.WriteLine(GetNextCluster(cmd, 169432));
  102.  
  103.                 int clusterSize = 10240;
  104.                 int oldClusterNr = (oldFileSize / clusterSize)+1;
  105.                 int newClusterNr = (newFileSize / clusterSize) + 1;
  106.  
  107.                 if (newClusterNr > noFileClusters + noFreeClusters)
  108.                 {
  109.                     MessageBox.Show("Not enough free clusters for the new file size");
  110.                     ev.Row.RejectChanges();
  111.                     return;
  112.                 }
  113.  
  114.                 LinkClusterAAfterB(cmd, firstFreeCluster, lastFileCluster);
  115.  
  116.                 int currentCluster = lastFileCluster;
  117.  
  118.                 for(int i = 0; i < newClusterNr - noFileClusters; i++)
  119.                 {
  120.                     lastFileCluster = firstFreeCluster;
  121.                     firstFreeCluster = GetNextCluster(cmd, firstFreeCluster);
  122.                 }
  123.  
  124.  
  125.                 cmd.CommandText = "UPADATE CLUSTERS SET NextCluster= NULL, ClusterStatus= 'E' WHERE [#C]=" + lastFileCluster;
  126.                 cmd.ExecuteNonQuery();
  127.  
  128.                 cmd.CommandText = "UPADATE DRIVES SET FirstFreeCluster="+firstFreeCluster+" WHERE [#LD]=" + ev.Row.LogicDrive;
  129.                 cmd.ExecuteNonQuery();
  130.  
  131.                 cmd.CommandText = "UPADATE FILES SET FileSize= " + newFileSize + " WHERE [#F]=" + ev.Row[0];
  132.                 cmd.ExecuteNonQuery();
  133.  
  134.                 con.Close();
  135.             }
  136.             }
  137.         }
  138.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement