数据存储、提取问题,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 ");
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);//显示子节点点文本
}
}
[解决办法]
在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 ")]
[解决办法]
定一个格式,直接写盘即可
[解决办法]
方法有许多种
[解决办法]
mark
[解决办法]
建议在数据库中建一个日志表来存储,简单方便
[解决办法]
当然是XML了
[解决办法]
mark
[解决办法]
支持 xml
[解决办法]
用DataSet吧,然后保存成XML文件,读写都方便,只需要一行代码,要展现什么的更是没得说,绑定到控件上面就OK了