数据存储、提取问题,100分~~

fengkoulangjian 2007-06-06 05:18:43
程序里有一些编辑好的方案,每个方里包含一些数据,我想把这些数据存储在文本文档里,一个方案占一个文本文档。这样,退出程序时不怕这些数据丢失。还有,在下次启动程序时,想直接调用这些文本文档中的数据。

请教:存储、提取的方法

能简单给写点儿程序么?感谢!
...全文
685 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengkoulangjian 2007-06-08
  • 打赏
  • 举报
回复
LS的大哥,DataSet控件怎么用啊?没接触过,给小段程序行么?
amandag 2007-06-07
  • 打赏
  • 举报
回复
mark
fengkoulangjian 2007-06-07
  • 打赏
  • 举报
回复
提取:

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
//我加的
this.textbox1.text=line;
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}


为什么提取出来的是.txt中的最后一行?
guyehanxinlei 2007-06-07
  • 打赏
  • 举报
回复
方法有许多种
calen 2007-06-07
  • 打赏
  • 举报
回复
定一个格式,直接写盘即可
fengkoulangjian 2007-06-07
  • 打赏
  • 举报
回复
TO:yumanqing(笨鸟)

我如何能把提取的数据显示在比如Textbox2中?

"Textbox2"加在什么地方呢?
fengkoulangjian 2007-06-07
  • 打赏
  • 举报
回复
哦,上面用过EventArgs e

我把Exception e 改成Exception f 了
fengkoulangjian 2007-06-07
  • 打赏
  • 举报
回复
TO:yumanqing(笨鸟)

在提取时:
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
出现问题:
1.错误 1 不能在此范围内声明名为“e”的局部变量,因为这样会使“e”具有不同的含义,而它已在“父级或当前”范围中表示其他内容了 D:\Documents and Settings\wz\My Documents\Visual Studio 2005\Projects\参数设置\参数设置\Form1.cs 268 13 参数设置

2.错误 2 “System.EventArgs”并不包含“Message”的定义 D:\Documents and Settings\wz\My Documents\Visual Studio 2005\Projects\参数设置\参数设置\Form1.cs 272 37 参数设置

谢谢,能指导下么?
Tassadar1979 2007-06-07
  • 打赏
  • 举报
回复
在C#中读取和写入ini文件的一段代码,其实本文只是指出一个方向,希望大家能够触类旁通。

  以下为代码全文:


  //写INI文件
  [ DllImport ( "kernel32" ) ]
  private static extern bool WritePrivateProfileString ( string section ,string key , string val , string filePath ) ;
  //读ini文件(字符
  [ DllImport ( "kernel32" ) ]
  private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal ,int size , string filePath ) ;

  //读ini文件(数字
  [ DllImport ( "kernel32" ) ]
  private static extern int GetPrivateProfileInt 的( string section ,string key , int def , string filePath ) ;

  //////////////////////////////////////////////////////////////
  using System;
  using System.IO;
  using System.Runtime.InteropServices;
  using System.Text;

  namespace EchonComponentLibrary
  {
  /// <summary>
  /// IniFile 的摘要说明。
  /// </summary>
  public class IniFile
  {
  private string FFileName;

  [DllImport("kernel32")]
Tassadar1979 2007-06-07
  • 打赏
  • 举报
回复
知有一个XML文件(bookstore.xml)如下:


<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>

1、往<bookstore>节点中插入一个<book>节点:

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性

XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<book>节点中
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);

root.AppendChild(xe1);//添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml");


//================
结果为:




<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>





2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。


XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
{
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”

XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
}

xmlDoc.Save("bookstore.xml");//保存。




//=================

最后结果为:


<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>




3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。


XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;

foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;


if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
else if(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");

//====================

最后结果为:


<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>


4、显示所有数据。


XmlNode xn=xmlDoc.SelectSingleNode("bookstore");

XmlNodeList xnl=xn.ChildNodes;

foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));

XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
}

fengkoulangjian 2007-06-07
  • 打赏
  • 举报
回复
谢谢各位,我试试~
jinta2001 2007-06-07
  • 打赏
  • 举报
回复
用DataSet吧,然后保存成XML文件,读写都方便,只需要一行代码,要展现什么的更是没得说,绑定到控件上面就OK了
hanshibo 2007-06-07
  • 打赏
  • 举报
回复
支持 xml
BiBuBiBu 2007-06-07
  • 打赏
  • 举报
回复
mark
zxkid 2007-06-07
  • 打赏
  • 举报
回复
当然是XML了
yang20052008 2007-06-07
  • 打赏
  • 举报
回复
建议在数据库中建一个日志表来存储,简单方便
CathySun118 2007-06-06
  • 打赏
  • 举报
回复
把文档存到数据库
yumanqing 2007-06-06
  • 打赏
  • 举报
回复

using System;
using System.IO;

class Test
{
public static void Main()
{
// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter sw = new StreamWriter("TestFile.txt"))
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
}
}



using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
jiatong1981 2007-06-06
  • 打赏
  • 举报
回复
建议使用xml来存取都比较方便 还可以配合数据库来使用

例如把最终方案存数据库中!
神奇的章鱼哥 2007-06-06
  • 打赏
  • 举报
回复
你可以用ini文件类型进行读写
有必要的话,对这个ini进行加密
ini文件读写都很方便的

当然还有xml文件,不过我不会用xml文件,所以不敢多说
加载更多回复(1)

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧