• Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable


      1  /// <summary>
      2         /// 添加
      3         /// </summary>
      4         /// <param name="sender"></param>
      5         /// <param name="e"></param>
      6         private void button1_Click(object sender, EventArgs e)
      7         {
      8             BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile), Encoding.Unicode);//StreamWriter           
      9             binWriter.Seek(0, SeekOrigin.End);
     10             num =int.Parse(this.textBox1.Text.Trim());
     11             strsex = this.textBox2.Text.Trim();
     12             strname = this.textBox3.Text.Trim();
     13             creatdate =this.dateTimePicker1.Value;
     14             binWriter.Write(num);
     15             binWriter.Write(strsex);
     16             binWriter.Write(strname);
     17             binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine);
     18             binWriter.Flush();
     19             binWriter.Close();
     20 
     21 
     22         }
     23 
     24 
     25 
     26         /// <summary>
     27         /// 顯示全部
     28         /// </summary>
     29         /// <param name="sender"></param>
     30         /// <param name="e"></param>
     31         private void button2_Click(object sender, EventArgs e)
     32         {
     33 
     34             //創建文件
     35             //Write out to binary file
     36             //if (!File.Exists(datfile))
     37             //{
     38             //    FileStream myFile = File.Create(datfile);
     39             //    myFile.Close();
     40             //}
     41             //strsex = "男";
     42             //num = 124;
     43             //creatdate = DateTime.Now;
     44             //strname = "塗聚文geovindu";
     45             //BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile), Encoding.Unicode);//StreamWriter 
     46             //binWriter.Write(num);
     47             //binWriter.Write(strsex);
     48             //binWriter.Write(strname);
     49             //binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine);
     50             ////binWriter.Seek(1, SeekOrigin.Begin);
     51             //strsex = "男";
     52             //num = 125;
     53             //creatdate = DateTime.Now;
     54             //strname = "劉志軍";
     55             ////BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile));
     56             //binWriter.Write(num);
     57             //binWriter.Write(strsex);
     58             //binWriter.Write(strname);
     59             //binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine);
     60             ////binWriter.Seek(2, SeekOrigin.Current);
     61             //strsex = "女";
     62             //num = 126;
     63             //creatdate = DateTime.Now;
     64             //strname = "孩子";
     65             ////BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile));
     66             //binWriter.Write(num);
     67             //binWriter.Write(strsex);
     68             //binWriter.Write(strname);
     69             //binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine);
     70             ////binWriter.Seek(3, SeekOrigin.Current);
     71             //binWriter.Flush();
     72             //binWriter.Close();
     73 
     74             //Read from binary file
     75             //最一條
     76             //byte[] bs;
     77             //using (FileStream fs = new FileStream(datfile, FileMode.Open, FileAccess.Read))
     78             //{
     79             //    using (BinaryReader br = new BinaryReader(fs))
     80             //    {
     81             //        bs = br.ReadBytes((int)fs.Length);
     82             //    }
     83             //}
     84             //foreach (byte detail in bs)
     85             //{
     86             //    Console.WriteLine(detail);
     87             //}
     88             dt = null;
     89             dt = new DataTable();
     90             dt.Columns.Add("ID", typeof(int));
     91             dt.Columns.Add("Sex", typeof(string));
     92             dt.Columns.Add("ReName", typeof(string));
     93             dt.Columns.Add("CreateTime", typeof(DateTime));
     94 
     95             using (BinaryReader binaryReader = new BinaryReader(File.Open(datfile, FileMode.Open), Encoding.Unicode))
     96             {
     97                 
     98                 //binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);
     99                 //1
    100                 //while (true)
    101                 //{
    102                 //    num  = binaryReader.ReadInt32();
    103                 //    strsex = binaryReader.ReadString();
    104                 //    strname = binaryReader.ReadString();
    105                 //    creatdate = DateTime.Parse(binaryReader.ReadString());
    106                 //    //data.Add(k, v);
    107 
    108                 //    if (binaryReader.BaseStream.Position == binaryReader.BaseStream.Length)
    109                 //        break;
    110                 //}
    111                 //
    112                 //2
    113                 while (binaryReader.PeekChar() != -1)
    114                 {
    115                     try
    116                     {
    117                         num = binaryReader.ReadInt32();
    118                         strsex = binaryReader.ReadString();
    119                         strname = binaryReader.ReadString();
    120                         creatdate = DateTime.Parse(binaryReader.ReadString());
    121                         dt.Rows.Add(num, strsex, strname, creatdate);
    122                     }
    123                     catch (Exception ex)
    124                     {
    125                         ex.Message.ToString();
    126                         break;
    127                     }
    128 
    129 
    130                 }
    131                 binaryReader.Close();
    132             }
    133 
    134             //BinaryReader binReader = new BinaryReader(File.OpenRead(datfile), Encoding.Unicode);
    135 
    136             //num = binReader.ReadInt32();
    137             //strsex = binReader.ReadString();
    138             //strname = binReader.ReadString();
    139             //creatdate = DateTime.Parse(binReader.ReadString());
    140 
    141             this.textBox1.Text = num.ToString();
    142             this.textBox2.Text = strsex;
    143             this.textBox3.Text = strname;
    144             this.dateTimePicker1.Value = creatdate;
    145             //binReader.PeekChar();
    146 
    147             //num = binReader.ReadInt32();
    148             //this.textBox4.Text = num.ToString();
    149             //binReader.Close();
    150 
    151             this.dataGridView1.DataSource = dt;
    152 
    153         }
    154         /// <summary>
    155         /// 
    156         /// </summary>
    157         /// <param name="FilePath"></param>
    158         /// <param name="buff"></param>
    159         public static void WriteBuffer(string FilePath, byte[] buff)
    160         {
    161             StreamWriter Writer = File.AppendText(FilePath);
    162             foreach (byte i in buff)
    163                 Writer.BaseStream.WriteByte(i);
    164             Writer.Close();
    165         }
    166         /// <summary>
    167         /// 查找
    168         /// </summary>
    169         /// <param name="sender"></param>
    170         /// <param name="e"></param>
    171         private void button3_Click(object sender, EventArgs e)
    172         {
    173             string s = this.textBox4.Text.Trim();
    174             using (BinaryReader binaryReader = new BinaryReader(File.Open(datfile, FileMode.Open), Encoding.Unicode))
    175             {
    176                 //binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);
    177                 //1
    178                 //while (true)
    179                 //{
    180                 //    num  = binaryReader.ReadInt32();
    181                 //    strsex = binaryReader.ReadString();
    182                 //    strname = binaryReader.ReadString();
    183                 //    creatdate = DateTime.Parse(binaryReader.ReadString());
    184                 //    //data.Add(k, v);
    185 
    186                 //    if (binaryReader.BaseStream.Position == binaryReader.BaseStream.Length)
    187                 //        break;
    188                 //}
    189                 //
    190                 //2
    191                 while (binaryReader.PeekChar() != -1)
    192                 {
    193                     try
    194                     {
    195                         num = binaryReader.ReadInt32();
    196                         strsex = binaryReader.ReadString();
    197                         strname = binaryReader.ReadString();
    198                         creatdate = DateTime.Parse(binaryReader.ReadString());
    199                         if (strname.Contains(s))
    200                         {
    201                             this.label2.Text=binaryReader.BaseStream.Position.ToString();
    202                             this.label3.Text =(binaryReader.BaseStream.Length-binaryReader.BaseStream.Seek(0, SeekOrigin.Current)).ToString();
    203                             setCurrent = binaryReader.BaseStream.Seek(0, SeekOrigin.Current);
    204                             //binaryReader.BaseStream.Seek((binaryReader.BaseStream.Position / 50) * 50, 0);                              
    205                             this.textBox1.Text = num.ToString();
    206                             this.textBox2.Text = strsex;
    207                             this.textBox3.Text = strname;
    208                             this.dateTimePicker1.Value = creatdate;
    209                         }
    210 
    211                     }
    212                     catch (Exception ex)
    213                     {
    214                         ex.Message.ToString();
    215                         break;
    216                     }
    217 
    218 
    219                 }
    220 
    221 
    222 
    223                 binaryReader.Close();
    224             }
    225         }
    226         /// <summary>
    227         /// 修改,創建
    228         /// </summary>
    229         /// <param name="sender"></param>
    230         /// <param name="e"></param>
    231         private void button4_Click(object sender, EventArgs e)
    232         {
    233             #region 創建 塗聚文 20130329
    234 
    235             //if (File.Exists(datfile))
    236             //{
    237             //    File.Delete(datfile);
    238             //}
    239 
    240             //dt = null;
    241             //dt = new DataTable();
    242             //dt.Columns.Add("ID", typeof(int));
    243             //dt.Columns.Add("Sex", typeof(string));
    244             //dt.Columns.Add("ReName", typeof(string));
    245             //dt.Columns.Add("CreateTime", typeof(DateTime));
    246             //dt.Rows.Add(1, "男", "塗聚文", "2013-03-1");
    247             //dt.Rows.Add(2, "女", "王熙鳳", "2013-03-11");
    248             //dt.Rows.Add(3, "女", "劉麗", "2013-03-21");
    249             //dt.Rows.Add(4, "男", "劉海粟", "2013-03-3");
    250             //dt.Rows.Add(5, "男", "高春輝", "2013-03-5");
    251             //BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile), Encoding.Unicode);//StreamWriter  
    252             ////binWriter.BaseStream.Seek(0, SeekOrigin.Current);
    253             //////binWriter.Seek((int)setCurrent, SeekOrigin.Current);//定位有問題
    254             //foreach (DataRow dr in dt.Rows)
    255             //{
    256             //    num=(int)dr["ID"];
    257             //    strsex =(string) dr["Sex"];
    258             //    strname = (string)dr["ReName"];
    259             //    creatdate =(DateTime)dr["CreateTime"];
    260             //    binWriter.Write(num);
    261             //    binWriter.Write(strsex);
    262             //    binWriter.Write(strname);
    263             //    binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine); //+ Environment.NewLine //加一行,成了現在的行
    264             //}
    265 
    266             //binWriter.Flush();
    267             //binWriter.Close();
    268             #endregion 
    269 
    270 
    271 
    272             //num = int.Parse(this.textBox1.Text.Trim());
    273             //strsex = this.textBox2.Text.Trim();
    274             //strname = this.textBox3.Text.Trim();
    275             //creatdate = this.dateTimePicker1.Value;
    276             //binWriter.Write(num);
    277             //binWriter.Write(strsex);
    278             //binWriter.Write(strname);
    279             //binWriter.Write(creatdate.ToShortDateString()); //+ Environment.NewLine //加一行,成了現在的行
    280 
    281             //binWriter.Flush();
    282             //binWriter.Close();
    283 
    284             //using (BinaryReader binaryReader = new BinaryReader(File.Open(datfile, FileMode.Open), Encoding.Unicode))
    285             //{
    286             //    while (binaryReader.PeekChar() != -1)
    287             //    {
    288             //        //binaryReader.BaseStream.Seek(setCurrent, SeekOrigin.Begin);
    289             //        num = binaryReader.ReadInt32();
    290             //        strsex = binaryReader.ReadString();
    291             //        strname = binaryReader.ReadString();
    292             //        creatdate = DateTime.Parse(binaryReader.ReadString());
    293             //        this.textBox1.Text = num.ToString();
    294             //        this.textBox2.Text = strsex;
    295             //        this.textBox3.Text = strname;
    296             //        this.dateTimePicker1.Value = creatdate;
    297             //    }
    298             //}
    299             
    300 
    301             #region 修改
    302 
    303             BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile), Encoding.Unicode);//StreamWriter 
    304             binWriter.BaseStream.Position = 0;
    305             num = 8;// int.Parse(this.textBox1.Text.Trim());
    306             strsex = "";// this.textBox2.Text.Trim();
    307             //strname = this.textBox3.Text.Trim();
    308             //creatdate = this.dateTimePicker1.Value;
    309             binWriter.Write(num);
    310             binWriter.BaseStream.Position = 4;
    311             binWriter.Write(strsex);
    312             //binWriter.Write(strname);
    313             //binWriter.Write(creatdate.ToShortDateString()); //+ Environment.NewLine //加一行,成了現在的行
    314             binWriter.Flush();
    315             binWriter.Close();
    316 
    317             #endregion 
    318 
    319             #region 查找數據位置
    320             cudt = null;
    321             cudt = new DataTable();
    322             cudt.Columns.Add("ID", typeof(int));
    323             cudt.Columns.Add("Position", typeof(int));
    324             cudt.Columns.Add("Legnth", typeof(int));
    325             cudt.Columns.Add("Sart", typeof(int));
    326             cudt.Columns.Add("End", typeof(int));
    327             cudt.Columns.Add("Name", typeof(string));
    328             cudt.Columns.Add("Value", typeof(string));
    329 
    330                 int pos = 0;
    331                 int row = 0;
    332                 int p = 0;
    333                 int br = 0;
    334                 string s = "";
    335                 int starpos = 0;
    336                 int endpos = 0;
    337             // 1.
    338             using (BinaryReader b = new BinaryReader(File.Open(datfile, FileMode.Open, FileAccess.Read),Encoding.Unicode))
    339             {
    340                 // 2.
    341                 // Position and length variables.
    342 
    343                 // 2A.
    344                 // Use BaseStream.
    345                 int length = (int)b.BaseStream.Length;
    346                 int k = 0;
    347                 while (b.PeekChar() != -1)//pos < length
    348                 {
    349 
    350                     try
    351                     {
    352                         starpos = (int)b.BaseStream.Position;
    353                         //
    354                         num = b.ReadInt32();  
    355                         p = (int)b.BaseStream.Position;
    356                         endpos = (int)b.BaseStream.Position;
    357                         pos = p;
    358                        // b.BaseStream.Seek(pos, SeekOrigin.Current);
    359                         br = (int)b.BaseStream.Length-p;
    360                         s = b.GetType().ToString();
    361                        cudt.Rows.Add(row, pos, br, starpos, endpos, s,num.ToString());//添加第一記錄  
    362                         //第二條記錄
    363                        starpos = (int)b.BaseStream.Position;
    364                         strsex = b.ReadString();
    365                         p = (int)b.BaseStream.Position;
    366                         endpos = p; 
    367                         pos = p;             
    368                         br = (int)b.BaseStream.Length-p;
    369                         s = b.GetType().ToString();  
    370                         cudt.Rows.Add(row, pos, br, starpos, endpos, s,strsex);//添加第二條記錄  
    371                         //第三第記錄
    372                         starpos=(int)b.BaseStream.Position;
    373                         strname = b.ReadString();
    374                         p = (int)b.BaseStream.Position;
    375                         endpos = p;                                              
    376                         pos = p;                        
    377                        // b.BaseStream.Seek(pos, SeekOrigin.Current);
    378                         br = (int)b.BaseStream.Length - p;
    379                         s = b.GetType().ToString();
    380                         cudt.Rows.Add(row, p, br, starpos, endpos, s,strname);//添加第三條記錄 
    381 
    382                         //第四條記錄
    383                         starpos = (int)b.BaseStream.Position;  
    384                         creatdate = DateTime.Parse(b.ReadString());
    385                         p = (int)b.BaseStream.Position;
    386                         endpos = p;  
    387                         pos = p;
    388                         br = (int)b.BaseStream.Length - p;  
    389                         s = b.GetType().ToString();
    390                         cudt.Rows.Add(row, p, br, starpos, endpos, s, creatdate);//第四記錄                                
    391 
    392                         k++;
    393                     }
    394 
    395 
    396                     catch (Exception ex)
    397                     {
    398                         ex.Message.ToString();
    399                         break;
    400 
    401                     }
    402                    
    403 
    404                     // 4.
    405                     // Advance our position variable.
    406                     pos += sizeof(int);
    407                     row++;
    408                 }
    409 
    410                 //b.BaseStream.Seek(7, SeekOrigin.Current);
    411                 //string f=b.ReadString();
    412                 //MessageBox.Show(f);
    413                 //指定位顯示
    414                 b.BaseStream.Position = 0;
    415                 MessageBox.Show(b.ReadInt32().ToString());
    416                 b.BaseStream.Position = 4;
    417                 MessageBox.Show(b.ReadString());
    418                 b.BaseStream.Position = 7;
    419                 MessageBox.Show(b.ReadString());
    420                 b.Close();
    421 
    422             }
    423            this.dataGridView2.DataSource = cudt;
    424             #endregion 
    425 
    426 
    427 
    428        }
    429 
    430 
    431         /// <summary>
    432         /// 
    433         /// </summary>
    434         /// <param name="sectorid"></param>
    435         /// <returns></returns>
    436         //public string getclientid(string sectorid)
    437         //{
    438         //    try
    439         //    {
    440         //        DataSet dsobj = new DataSet();
    441         //        bllobj = new ConverterClass();
    442         //        string retclientval = "0";
    443 
    444         //        dsobj = bllobj.BinaryDeSerialize(strDataPath + "\\" + "BinaryClientlocation.dat");
    445         //        DataRow[] dr = dsobj.Tables[0].Select("client_location_id in(" + locationid + ")");
    446         //        if (dr.Length > 0)
    447         //        {
    448         //            foreach (DataRow drow in dr)
    449         //            {
    450         //                retclientval = retclientval + "," + drow["client_id"].ToString();
    451         //            }
    452         //        }
    453         //        return retclientval;
    454         //    }
    455         //    catch (Exception ex)
    456         //    {
    457         //        ex.Message.ToString();
    458         //    }
    459         //}
    460     }
    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    数据库事务的四大特性
    Mysql数据库的慢sql优化步骤
    数据库事务的4大隔离级别
    mysql的命令练习
    通过jedis对redis客户端的一些基本操作
    通过jedisPool写一个redis连接池
    jedis
    maven将本地jar包添加到本地仓库
    [Django学习] Django基础(7)_分类统计
    [Django学习] Django基础(6)_Field lookups
  • 原文地址:https://www.cnblogs.com/geovindu/p/2988918.html
Copyright © 2020-2023  润新知