您的位置:首页 > 编程语言 > C#

C#成神之路<18> C#使用磁盘数据文件(2)

2016-02-12 16:12 531 查看
1、连续文件与随机访问文件

(1)连续文件

定义:不断的将新数据添加到文件末尾的过程创建了连续文件。使用连续文件,新数据会不断的添加到文件末尾。数据之间没有间隙。每个记录大小不同

优点:数据是密集的。不浪费文件空间。

缺点:必须读一遍不想要的数据才能得到实际要使用的数据。在编辑连续文件的一部分需要将旧文件读到内存中。(常用于不经常需要编辑或更新的数据类型很适合连续文件)

特点:具备BOF(文件开头)和EOF(文件结尾)。连续文件连续的从BOF读到EOF。为了读连续文件,打开文件时文件指向BOF。

(2)随机访问文件

定义:基于固定记录大小的概念。每个记录大小相同

优点:可以选择文件指针并且跳过不想读取的数据。随机访问文件中对记录位置使用散列算法使得单个记录的定位非常快。(用一块信息派生另一块信息的过程称之为散列。)记录大小相同,可以方便更新特定记录,并且重写该记录。

缺点:必须设计最坏情况下的记录大小。意味着随机访问文件不像连续文件那样密集,文件之间具有未存储信息的信息间隙。

C#提供函数seek进行文件指针快速移动到文件的任何位置。

Seek((desiredRecord-1)*RECRDSIZE,SeekOrigin.Begin);


第一个参数:移动指针的字节偏移量,long数据类型。负偏移量意味着希望将文件指针向逆方向移动。

第二个参数:从哪里移动文件指针的相对位置。提供了移动文件指针的三个相对点。SeekOrigin.Begin用相对于BOF的字节偏移量定位文件指针。SeekOrigin.Current将文件指针移动到文件指针的当前位置。SeekOrigin.End将指针放在文件的末尾EOF。

2、串行化与反串行化

串行化:将对象的状态保存或者永久保存到磁盘中的动作。

反串行化:通过即将存储在磁盘上的信息读回对象的类中来推向对象状态的动作。

默认情况下基本数据类型(byte,char,int,long,double,string)是不能串行化的。所以必须用要串行化的对象的类源文件顶部的【Serializable】属性,来显示指出该对象可以被串行化。

示例程序:

实现功能:编写一个电子电话薄来维护 客户的记录。

随机访问类代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
class clsRandomAccess
{
//设置类中的常量,设置用来存储数据的各种字符串的最大字符个数。

const int NAMESIZE = 20;
const int ADDRESSSIZES = 30;
const int PHONESIZES = 12;
const int EMAILSIZE = 100;
const int DATESIZES = 10;
//每个记录中存储了14个字符串变量。

const int STRINGSINFILE = 14;
//所有常量加在一起的目的是记录每个记录所使用的字节数。
const int RECORDSIZE = NAMESIZE + 1 + NAMESIZE + ADDRESSSIZES * 2
+ NAMESIZE + 2 + 5 + PHONESIZES * 3 + EMAILSIZE + DATESIZES * 2 + sizeof(int) * STRINGSINFILE;
//sizeof运算符确定一个类型值类型所需的字节数。
//设置类中的变量
private string firstName;
private string middleInitial;
private string lastName;
private string address1;
private string address2;
private string city;
private string state;
private string zip;
private string homePhone;
private string cellPhone;
private string workPhone;
private string email;
private string birthday;
private string anniversary;

private int status;

private string errorMessage;
private string fileName;

private FileStream myFile;
private BinaryReader br;
private BinaryWriter bw;
//当字符串存储在二进制文件当中时
//低级文件的I/O代码需要知道有多少字节
//与每块字符串数据相关联。
//C#中 字符串描述符块简称长度字节。
public clsRandomAccess()
{
myFile = null;
errorMessage = " ";
fileName = "Friend.bin";
status = 1;
}

public clsRandomAccess(string fn)
: this()
{
fileName = fn;
}

public string FirstName
{
get {
return firstName;
}
set
{
if (value.Length > 0)
{
firstName = value;
if (firstName.Length > NAMESIZE)
{
firstName=firstName.Substring(0,NAMESIZE);
}
}
}

}
public string MiddleInitial
{
get
{
return middleInitial;
}
set
{
if (value.Length > 0)
{
middleInitial = value;
if (middleInitial.Length != 1)
{
middleInitial = "n/a";
}
}
}
}

public string LastName
{
get
{

20000
return lastName;
}
set
{
if (value.Length > NAMESIZE)
{
lastName = value;
if (lastName.Length > NAMESIZE)
{
lastName = lastName.Substring(0,NAMESIZE);
}
}
}
}

public string Address1
{
get
{
return address1;
}
set
{
if (value.Length > 0)
{
address1 = value;
if (address1.Length > ADDRESSSIZES)
{
address1 = address1.Substring(0, ADDRESSSIZES);
}
}
else
{
address1 = "n/a";
}

}
}

public string Address2
{
get {
return address2;
}
set
{
if (value.Length > 0)
{
address2 = value;
if (address2.Length > ADDRESSSIZES)
{
address2 = address2.Substring(0,ADDRESSSIZES);
}
}
if (value == null)
{
address2 = "n/a";
}
}
}

public string City
{
get
{
return city;
}
set
{
if (value.Length > 0)
{
city = value;
if (city.Length > NAMESIZE)
{
city = city.Substring(0,NAMESIZE);
}
}
}
}

public string State
{
get
{
return state;
}
set
{
if (value.Length >0)
{
state = value;
if (state.Length != 2)
{
state = " ";
}
}
}
}

public string Zip
{
get
{
return zip;
}
set
{
if (value.Length > 0)
{
zip = value;
if (zip.Length !=5)
{
zip = " ";
}
}
}
}

public string HomePhone
{
get
{
return homePhone;
}
set
{
if (value.Length > 0)
{
homePhone = value;
if (homePhone.Length > PHONESIZES)
{
homePhone = homePhone.Substring(0,PHONESIZES);
}
}
if (value == null)
{
homePhone = " ";
}
}
}

public string CellPhone
{
get
{
return cellPhone;
}
set
{
if (value.Length > 0)
{
cellPhone = value;
if (cellPhone.Length > PHONESIZES)
{
cellPhone.Substring(0,PHONESIZES);
}
}
if (value == null)
{
cellPhone = "n/a";
}
}
}

public string WorkPhone
{
get
{
return workPhone;
}
set
{
if (value.Length > 0)
{
workPhone = value;
if (workPhone.Length > PHONESIZES)
{
workPhone = workPhone.Substring(0,PHONESIZES);
}
}
if (workPhone == null)
{
workPhone = "n/a";
}
}
}

public string Email
{
get
{
return email;
}
set
{
if (value.Length > 0)
{
email = value;
if (email.Length > EMAILSIZE)
{
email = email.Substring(0,EMAILSIZE);
}
}
if (email == null)
{
email = "n/a";
}
}
}

public string BirthDay
{
get
{
return birthday;
}
set
{
if (value.Length > 0)
{
birthday = value;
if (birthday.Length > DATESIZES)
{
birthday = birthday.Substring(0,DATESIZES);
}
}
if (birthday == null)
{
birthday = "n/a";
}
}
}

public string Anniversary
{
get
{
return anniversary;
}
set
{
if (value.Length > 0)
{
anniversary = value;
if (anniversary.Length > DATESIZES)
{
anniversary = anniversary.Substring(0,DATESIZES);
}
}
if (anniversary == null)
{
anniversary = "n/a";
}
}
}

public int Status
{
get
{
return status;
}
set
{
if (value == 1)
{
status = value;
}
else
{
status = 0;
}
}
}

public string Filename
{
get
{
return fileName;
}
set
{
if (value.Length > 0)
{
fileName = value;
}
}
}

public FileStream MyFile
{
get
{
return myFile;
}
set
{
myFile = value;
}
}

public BinaryReader BinReader
{
get
{
return br;
}
set
{
br = value;
}
}

public BinaryWriter BinWriter
{
get
{
return bw;
}
set
{
bw = value;
}
}

public String ErrorText
{
get
{
return errorMessage;
}
}

//接口制作完成
//下面对于相关函数的创建
//文件的创建
public int Create(String fn)
{
try
{
myFile = new FileStream(fn, FileMode.OpenOrCreate);
bw = new BinaryWriter(myFile);
}
catch
{
return 0;
}
return 1;
}

//打开文件进行读写的函数
public int Open(String fn)
{
if (bw == null)
{
return Create(fn);
}
else
{
myFile = new FileStream(fn,FileMode.OpenOrCreate);
}
return 1;
}

//Close function

public void Close()
{
if (myFile != null)
myFile.Close();
if (br != null)
br.Close();
if (bw != null)
bw.Close();
}
//该代码确保有一个有效的FileStream对象
//然后将文件指针定位在要将新纪录写入文件中的位置。
public int WriteOneRecord(long num)
{
int errorFlag = 1;
try
{
if (myFile != null)
{
myFile.Seek(num * RECORDSIZE, SeekOrigin.Begin);
//将num * RECORDSIZE个字节都写到文件中
//实例化创建一个binarywriter对象。
bw = new BinaryWriter(myFile);
//继续向文件中写入数据。
bw.Write(firstName);
bw.Write(middleInitial);
bw.Write(lastName);
bw.Write(address1);
bw.Write(address2);
bw.Write(city);
bw.Write(state);
bw.Write(zip);
bw.Write(homePhone);
bw.Write(cellPhone);
bw.Write(workPhone);
bw.Write(email);
bw.Write(birthday);
bw.Write(anniversary);
bw.Write(status);
bw.Close();
//bw写的每个字符串都会自动写出各个字符串
//的长度。
}
}
catch (IOException ex)
{
errorMessage = ex.Message;
errorFlag = 0;
}
return errorFlag;
}

public int ReadOnRecord(long num)
{
try
{
if (myFile != null)
{
myFile.Close();
}
myFile = new FileStream(fileName, FileMode.Open);
br = new BinaryReader(myFile);
//读取方法中的顺序和写入方法中顺序必须一致
if (myFile != null && br != null)
{
myFile.Seek(num * RECORDSIZE, SeekOrigin.Begin);
//读取字符串的方法
firstName = br.ReadString();
middleInitial = br.ReadString();
lastName = br.ReadString();
address1 = br.ReadString();
address2 = br.ReadString();
city = br.ReadString();
state = br.ReadString();
zip = br.ReadString();
homePhone = br.ReadString();
cellPhone = br.ReadString();
workPhone = br.ReadString();
email = br.ReadString();
workPhone = br.ReadString();
email = br.ReadString();
birthday = br.ReadString();
anniversary = br.ReadString();
//读取整形的方法
status = br.ReadInt32();
br.Close();
}
}
catch(IOException ex)
{
errorMessage = ex.Message;
return 0;
}
return 1;
}

//得到当前记录的数量。
public long getRecordCount()
{
long record = 0;
long remainder;

try
{
if (myFile != null)
{
//用指针把文件指针放到文件尾
//当文件指针到达该位置的时候,返回文件中
//离当前位置的字节数(long类型)
record = myFile.Seek(0, SeekOrigin.End);
Close();
}
}
catch (IOException ex)
{
return -1;
}
remainder = record % RECORDSIZE;//细化计算,考虑间隙+1
record = record / RECORDSIZE;//得到记录数量
if (remainder > 0)
{
record++;
}
return record;
}

}


frmMain代码:

using System;
using System.Windows.Forms;
public class frmMain : Form
{
const string TESTDATAFILE = "Friends.bin";
//新文件的记录位置。
long recs;
long currentRecord = 1;
clsRandomAccess myData = new clsRandomAccess(TESTDATAFILE);

private GroupBox groupBox1;
private TextBox txtState;
private Label label15;
private Label label14;
private Label label13;
private Label label12;
private Label label11;
private Label label10;
private TextBox txtLastName;
private TextBox txtMI;
private Label label9;
private TextBox txtBirthday;
private TextBox txtEmail;
private TextBox txtHome;
private TextBox txtCity;
private TextBox txtAddress2;
private TextBox txtAddress1;
private TextBox txtFirstName;
private Label label8;
private Label label7;
private Label label6;
private Label label5;
private Label label4;
private Label label3;
private Label label2;
private GroupBox groupBox2;
private Button btnNext;
private Button btnPrevious;
private Button btnLast;
private Button btnFirst;
private Button btnAdd;
private Button btnSave;
private Button btnDelete;
private Button btnClose;
private Label label1;
private CheckBox chkStatus;
private TextBox txtAnniversary;
private TextBox txtWork;
private TextBox txtCell;
private TextBox txtZip;
private TextBox txtRecord;
#region Windows code
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.btnAdd = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.btnFirst = new System.Windows.Forms.Button();
this.btnLast = new System.Windows.Forms.Button();
this.btnPrevious = new System.Windows.Forms.Button();
this.btnNext = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.txtRecord = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.txtFirstName = new System.Windows.Forms.TextBox();
this.txtAddress1 = new System.Windows.Forms.TextBox();
this.txtAddress2 = new System.Windows.Forms.TextBox();
this.txtCity = new System.Windows.Forms.TextBox();
this.txtHome = new System.Windows.Forms.TextBox();
this.txtEmail = new System.Windows.Forms.TextBox();
this.txtBirthday = new System.Windows.Forms.TextBox();
this.label9 = new System.Windows.Forms.Label();
this.txtMI = new System.Windows.Forms.TextBox();
this.txtLastName = new System.Windows.Forms.TextBox();
this.label10 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.txtState = new System.Windows.Forms.TextBox();
this.txtZip = new System.Windows.Forms.TextBox();
this.txtCell = new System.Windows.Forms.TextBox();
this.txtWork = new System.Windows.Forms.TextBox();
this.txtAnniversary = new System.Windows.Forms.TextBox();
this.chkStatus = new System.Windows.Forms.CheckBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.chkStatus);
this.groupBox1.Controls.Add(this.txtAnniversary);
this.groupBox1.Controls.Add(this.txtWork);
this.groupBox1.Controls.Add(this.txtCell);
this.groupBox1.Controls.Add(this.txtZip);
this.groupBox1.Controls.Add(this.txtState);
this.groupBox1.Controls.Add(this.label15);
this.groupBox1.Controls.Add(this.label14);
this.groupBox1.Controls.Add(this.label13);
this.groupBox1.Controls.Add(this.label12);
this.groupBox1.Controls.Add(this.label11);
this.groupBox1.Controls.Add(this.label10);
this.groupBox1.Controls.Add(this.txtLastName);
this.groupBox1.Controls.Add(this.txtMI);
this.groupBox1.Controls.Add(this.label9);
this.groupBox1.Controls.Add(this.txtBirthday);
this.groupBox1.Controls.Add(this.txtEmail);
this.groupBox1.Controls.Add(this.txtHome);
this.groupBox1.Controls.Add(this.txtCity);
this.groupBox1.Controls.Add(this.txtAddress2);
this.groupBox1.Controls.Add(this.txtAddress1);
this.groupBox1.Controls.Add(this.txtFirstName);
this.groupBox1.Controls.Add(this.label8);
this.groupBox1.Controls.Add(this.label7);
this.groupBox1.Controls.Add(this.label6);
this.groupBox1.Controls.Add(this.label5);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Location = new System.Drawing.Point(28, 27);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(760, 340);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Personal Information";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.btnNext);
this.groupBox2.Controls.Add(this.btnPrevious);
this.groupBox2.Controls.Add(this.btnLast);
this.groupBox2.Controls.Add(this.btnFirst);
this.groupBox2.Location = new System.Drawing.Point(28, 408);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(760, 132);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Navigate Record";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(825, 55);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 2;
this.btnAdd.Text = "Add New";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(825, 99);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(75, 23);
this.btnSave.TabIndex = 3;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnDelete
//
this.btnDelete.Location = new System.Drawing.Point(825, 222);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(75, 23);
this.btnDelete.TabIndex = 4;
this.btnDelete.Text = "Delete";
this.btnDelete.UseVisualStyleBackColor = true;
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(825, 267);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 5;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnFirst
//
this.btnFirst.Location = new System.Drawing.Point(34, 66);
this.btnFirst.Name = "btnFirst";
this.btnFirst.Size = new System.Drawing.Size(75, 23);
this.btnFirst.TabIndex = 0;
this.btnFirst.Text = "First";
this.btnFirst.UseVisualStyleBackColor = true;
this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
//
// btnLast
//
this.btnLast.Location = new System.Drawing.Point(590, 66);
this.btnLast.Name = "btnLast";
this.btnLast.Size = new System.Drawing.Size(75, 23);
this.btnLast.TabIndex = 1;
this.btnLast.Text = "Last";
this.btnLast.UseVisualStyleBackColor = true;
this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
//
// btnPrevious
//
this.btnPrevious.Location = new System.Drawing.Point(414, 66);
this.btnPrevious.Name = "btnPrevious";
this.btnPrevious.Size = new System.Drawing.Size(75, 23);
this.btnPrevious.TabIndex = 2;
this.btnPrevious.Text = "Previous";
this.btnPrevious.UseVisualStyleBackColor = true;
this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
//
// btnNext
//
this.btnNext.Location = new System.Drawing.Point(222, 66);
this.btnNext.Name = "btnNext";
this.btnNext.Size = new System.Drawing.Size(75, 23);
this.btnNext.TabIndex = 3;
this.btnNext.Text = "Next";
this.btnNext.UseVisualStyleBackColor = true;
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(825, 432);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 6;
this.label1.Text = "Record";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// txtRecord
//
this.txtRecord.Location = new System.Drawing.Point(826, 473);
this.txtRecord.Name = "txtRecord";
this.txtRecord.Size = new System.Drawing.Size(100, 21);
this.txtRecord.TabIndex = 7;
//
// label2
//
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(24, 33);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(85, 23);
this.label2.TabIndex = 0;
this.label2.Text = "First Name";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label3
//
this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label3.Location = new System.Drawing.Point(24, 71);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(85, 23);
this.label3.TabIndex = 1;
this.label3.Text = "Address1";
this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label4
//
this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label4.Location = new System.Drawing.Point(24, 109);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(85, 23);
this.label4.TabIndex = 2;
this.label4.Text = "Address2";
this.label4.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label5
//
this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label5.Location = new System.Drawing.Point(24, 149);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(85, 23);
this.label5.TabIndex = 3;
this.label5.Text = "City";
this.label5.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label6
//
this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label6.Location = new System.Drawing.Point(24, 194);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(85, 23);
this.label6.TabIndex = 4;
this.label6.Text = "Home Phone";
this.label6.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label7
//
this.label7.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label7.Location = new System.Drawing.Point(24, 239);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(85, 23);
this.label7.TabIndex = 5;
this.label7.Text = "Email";
this.label7.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label8
//
this.label8.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label8.Location = new System.Drawing.Point(24, 278);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(85, 23);
this.label8.TabIndex = 6;
this.label8.Text = "BirthDay";
this.label8.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// txtFirstName
//
this.txtFirstName.Location = new System.Drawing.Point(116, 34);
this.txtFirstName.Name = "txtFirstName";
this.txtFirstName.Size = new System.Drawing.Size(149, 21);
this.txtFirstName.TabIndex = 7;
//
// txtAddress1
//
this.txtAddress1.Location = new System.Drawing.Point(116, 72);
this.txtAddress1.Name = "txtAddress1";
this.txtAddress1.Size = new System.Drawing.Size(609, 21);
this.txtAddress1.TabIndex = 8;
//
// txtAddress2
//
this.txtAddress2.Location = new System.Drawing.Point(116, 110);
this.txtAddress2.Name = "txtAddress2";
this.txtAddress2.Size = new System.Drawing.Size(609, 21);
this.txtAddress2.TabIndex = 9;
//
// txtCity
//
this.txtCity.Location = new System.Drawing.Point(116, 150);
this.txtCity.Name = "txtCity";
this.txtCity.Size = new System.Drawing.Size(220, 21);
this.txtCity.TabIndex = 10;
//
// txtHome
//
this.txtHome.Location = new System.Drawing.Point(116, 195);
this.txtHome.Name = "txtHome";
this.txtHome.Size = new System.Drawing.Size(149, 21);
this.txtHome.TabIndex = 11;
//
// txtEmail
//
this.txtEmail.Location = new System.Drawing.Point(116, 240);
this.txtEmail.Name = "txtEmail";
this.txtEmail.Size = new System.Drawing.Size(609, 21);
this.txtEmail.TabIndex = 12;
//
// txtBirthday
//
this.txtBirthday.Location = new System.Drawing.Point(116, 279);
this.txtBirthday.Name = "txtBirthday";
this.txtBirthday.Size = new System.Drawing.Size(100, 21);
this.txtBirthday.TabIndex = 13;
//
// label9
//
this.label9.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label9.Location = new System.Drawing.Point(283, 32);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(76, 23);
this.label9.TabIndex = 14;
this.label9.Text = "Initial";
this.label9.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// txtMI
//
this.txtMI.Location = new System.Drawing.Point(375, 34);
this.txtMI.Name = "txtMI";
this.txtMI.Size = new System.Drawing.Size(100, 21);
this.txtMI.TabIndex = 15;
//
// txtLastName
//
this.txtLastName.Location = new System.Drawing.Point(590, 34);
this.txtLastName.Name = "txtLastName";
this.txtLastName.Size = new System.Drawing.Size(135, 21);
this.txtLastName.TabIndex = 16;
//
// label10
//
this.label10.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label10.Location = new System.Drawing.Point(490, 32);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(81, 23);
this.label10.TabIndex = 17;
this.label10.Text = "LastName";
this.label10.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label11
//
this.label11.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label11.Location = new System.Drawing.Point(352, 150);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(56, 23);
this.label11.TabIndex = 18;
this.label11.Text = "State";
this.label11.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label12
//
this.label12.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label12.Location = new System.Drawing.Point(500, 149);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(71, 23);
this.label12.TabIndex = 19;
this.label12.Text = "ZipCode";
this.label12.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label13
//
this.label13.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label13.Location = new System.Drawing.Point(271, 193);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(65, 23);
this.label13.TabIndex = 20;
this.label13.Text = "Cell";
this.label13.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label14
//
this.label14.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label14.Location = new System.Drawing.Point(500, 194);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(71, 23);
this.label14.TabIndex = 21;
this.label14.Text = "Work";
this.label14.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label15
//
this.label15.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label15.Location = new System.Drawing.Point(222, 278);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(100, 23);
this.label15.TabIndex = 22;
this.label15.Text = "Anniversary";
this.label15.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// txtState
//
this.txtState.Location = new System.Drawing.Point(414, 149);
this.txtState.Name = "txtState";
this.txtState.Size = new System.Drawing.Size(79, 21);
this.txtState.TabIndex = 23;
this.txtState.TextChanged += new System.EventHandler(this.textBox11_TextChanged);
//
// txtZip
//
this.txtZip.Location = new System.Drawing.Point(578, 149);
this.txtZip.Name = "txtZip";
this.txtZip.Size = new System.Drawing.Size(147, 21);
this.txtZip.TabIndex = 24;
//
// txtCell
//
this.txtCell.Location = new System.Drawing.Point(343, 195);
this.txtCell.Name = "txtCell";
this.txtCell.Size = new System.Drawing.Size(150, 21);
this.txtCell.TabIndex = 25;
//
// txtWork
//
this.txtWork.Location = new System.Drawing.Point(578, 195);
this.txtWork.Name = "txtWork";
this.txtWork.Size = new System.Drawing.Size(147, 21);
this.txtWork.TabIndex = 26;
this.txtWork.TextChanged += new System.EventHandler(this.textBox14_TextChanged);
//
// txtAnniversary
//
this.txtAnniversary.Location = new System.Drawing.Point(329, 278);
this.txtAnniversary.Name = "txtAnniversary";
this.txtAnniversary.Size = new System.Drawing.Size(146, 21);
this.txtAnniversary.TabIndex = 27;
//
// chkStatus
//
this.chkStatus.AutoSize = true;
this.chkStatus.Location = new System.Drawing.Point(633, 285);
this.chkStatus.Name = "chkStatus";
this.chkStatus.Size = new System.Drawing.Size(60, 16);
this.chkStatus.TabIndex = 28;
this.chkStatus.Text = "Active";
this.chkStatus.UseVisualStyleBackColor = true;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(938, 567);
this.Controls.Add(this.txtRecord);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "frmMain";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion
//#region是C# 预处理器指令。
//#region 使您可以在使用 Visual Studio
//代码编辑器的大纲显示功能时指定可展开或折叠的代码块。
public frmMain()
{
InitializeComponent();
}

public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}

//显示相关信息和输入相关信息

private void ShowOneRecord()
{
txtFirstName.Text = myData.FirstName;
txtLastName.Text = myData.LastName;
txtAddress1.Text = myData.Address1;
txtAddress2.Text = myData.Address2;
txtCity.Text = myData.City;
txtState.Text = myData.State;
txtZip.Text = myData.Zip;
txtHome.Text = myData.HomePhone;
txtCell.Text = myData.CellPhone;
txtWork.Text = myData.WorkPhone;
txtEmail.Text = myData.Email;
txtBirthday.Text = myData.BirthDay;
txtAnniversary.Text = myData.Anniversary;
if (myData.Status == 1)
{
chkStatus.Checked = true;
}
else
{
chkStatus.Checked = false;
}
}

private void CopyDate()
{
myData.FirstName = txtFirstName.Text;
myData.LastName = txtLastName.Text  ;
myData.Address1= txtAddress1.Text ;
myData.Address2 = txtAddress2.Text;
myData.City = txtCity.Text;
myData.State= txtState.Text ;
myData.Zip=txtZip.Text ;
myData.HomePhone=txtHome.Text ;
myData.CellPhone=txtCell.Text ;
myData.WorkPhone=txtWork.Text ;
myData.Email=  txtEmail.Text;
myData.BirthDay= txtBirthday.Text;
myData.Anniversary=txtAnniversary.Text  ;
if (chkStatus.Checked == true)
myData.Status = 1;
else
myData.Status = 0;
}

private int ReadAndShowRecord()
{
int flag = 1;
try
{
myData.Open(myData.Filename);
flag = myData.ReadOnRecord(currentRecord - 1);
if (flag == 1)
{
ShowOneRecord();
txtRecord.Text = currentRecord.ToString();
}
else
{
MessageBox.Show("Record not available.", "Error Read");
flag = 0;
}
}
catch
{
flag = 0;
}
myData.Close();
return flag;
}

private void textBox11_TextChanged(object sender, EventArgs e)
{

}

private void textBox14_TextChanged(object sender, EventArgs e)
{

}

private void btnFirst_Click(object sender, EventArgs e)
{
int flag;

currentRecord=1;
flag = ReadAndShowRecord();
}

private void btnPrevious_Click(object sender, EventArgs e)
{
int flag;

currentRecord--;
flag = ReadAndShowRecord();
if (flag == 0)
{
currentRecord++;
}
}

private void btnLast_Click(object sender, EventArgs e)
{
int flag;
myData.Open(myData.Filename);
currentRecord = myData.getRecordCount();
if (currentRecord > -1)
{
flag = ReadAndShowRecord();
}
}

private void btnSave_Click(object sender, EventArgs e)
{
CopyDate();
if (myData.Open(TESTDATAFILE) == 1)
{
recs = myData.getRecordCount();
myData.Open(TESTDATAFILE);
myData.WriteOneRecord(recs);
myData.Close();
MessageBox.Show("Data written successfully");
}
else
{
MessageBox.Show("Couldnt open the file"+TESTDATAFILE,"File Error");
return;
}
}

private void btnAdd_Click(object sender, EventArgs e)
{
ClearTextboxs();
if (myData.Status == 1)
{
chkStatus.Checked = true;
}
else
{
chkStatus.Checked = false;
}
txtFirstName.Focus();
}

private void ClearTextboxs()
{
txtFirstName.Text = " ";
txtLastName.Text = " ";
txtAddress1.Text = " ";
txtAddress2.Text = " ";
txtCity.Text = " ";
txtState.Text = " ";
txtZip.Text = " ";
txtHome.Text = " ";
txtCell.Text = " ";
txtWork.Text = " ";
txtEmail.Text = " ";
txtBirthday.Text = " ";
txtAnniversary.Text = " ";

}

private void btnDelete_Click(object sender, EventArgs e)
{
DialogResult ask;

ask = MessageBox.Show("Are you sure you want to delete this record?","Delete record",MessageBoxButtons.YesNo);
if (ask == DialogResult.Yes)
{
myData.Status = 0;
myData.Open(myData.Filename);
myData.WriteOneRecord(currentRecord-1);
MessageBox.Show("Record delete","delete record");
}
}

private void btnClose_Click(object sender, EventArgs e)
{
Close();
}

private void btnNext_Click(object sender, EventArgs e)
{
int flag;

currentRecord++;
flag = ReadAndShowRecord();
if (flag == 0)
{
currentRecord--;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 磁盘 数据