对于有些程序用txt文本做数据库,对文本进行增删改查

 

//自动生成文本文件
FileStream fs = new FileStream("Login.txt", FileMode.Create);
fs.Close();

如果数据量太大会影响程序的读取速度所以可以根据日期每天创建一个存储信息的文本

 

public static string data = DateTime.Now.ToString("yyyy/MM/dd");
string path = data + ".txt";

FileStream fs = new FileStream(path, FileMode.Create);
fs.Close();

 

#region 根据编码循环读取文本查看信息是否已经存在

StreamReader Reader = new StreamReader(class1.pandian, System.Text.Encoding.GetEncoding("UTF-8"));
string Line = "";
while (Line != null)
{
Line = Reader.ReadLine();
if (Line != null)
{
string[] ypan = Line.Split(',');
string z = ypan[1].ToString();
if (txtTiaoma.Text == ypan[0].ToString())
{
Reader.Close();
MessageBox.Show("此信息已存在!");
return;
}
}

//如果信息不存在执行写入功能

else
{
Reader.Close();
string strWrt = bianhao.Text + "," + name.Text + "," + time.Text  + "," + address.Text  + "\r\n";
StreamWriter sw2 = new StreamWriter(class1.pandian, true, Encoding.GetEncoding("UTF-8"));
sw2.Write(strWrt);
sw2.Close();
}

}

#endregion

 

//根据编号提取个人信息并显示信息

StreamReader objReader = new StreamReader(class1.Fscan, System.Text.Encoding.GetEncoding("UTF-8"));
string sLine = "";
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
{
string[] ca = sLine.Split(',');
if (tiaoma == ca[0].ToString())
{
name.Text.Text += ca[1].ToString();
name.Text += ca[2].ToString();
time.Text += ca[3].ToString();
address.Text+= ca[4].ToString();
objReader.Close();
return;
}
}

//循环查找完毕没有这个信息就会提示
else
{
MessageBox.Show("数据不存在!!!");
objReader.Close();
return;
}
}

posted on 2013-04-09 17:17  TreeStar  阅读(322)  评论(1)    收藏  举报