日期:2014-05-19  浏览次数:20843 次

数据存储、提取问题,100分~~
程序里有一些编辑好的方案,每个方里包含一些数据,我想把这些数据存储在文本文档里,一个方案占一个文本文档。这样,退出程序时不怕这些数据丢失。还有,在下次启动程序时,想直接调用这些文本文档中的数据。

请教:存储、提取的方法

能简单给写点儿程序么?感谢!

------解决方案--------------------
退出的时候,保存在txt文件中......
------解决方案--------------------
你可以用ini文件类型进行读写
有必要的话,对这个ini进行加密
ini文件读写都很方便的

当然还有xml文件,不过我不会用xml文件,所以不敢多说
------解决方案--------------------
建议使用xml来存取都比较方便 还可以配合数据库来使用

例如把最终方案存数据库中!
------解决方案--------------------

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);
}
}
}

------解决方案--------------------
把文档存到数据库
------解决方案--------------------
知有一个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 ");