我用类来保存,然后用二进投影保存类。但是每一次保存都会覆盖上一次的数据,有没有办法不用SQL 能不能保存每天的记得。然后用 ListBox 显示。
解决方案 »
- PHP通过套接字向C#发送中文字符,C#接收到的却是数字字符
- C#中关于GridView的问题
- treeview在拖拽节点前能否实现不选中拖拽的节点?
- 有没有直接得到一个字符串在另一个字符串中匹配多少次的函数?
- 100分求一个C#的问卷源码?谁有,简单的就行?
- 如何将数据库的记录内容写入到WORD文件?
- 怎么在winForm的dataGrid中加入dropdownlist
- 救命呀
- 请问如何在winform上划出一条直线
- 正在装 visual studio.net 2003 , 奇怪的是才1.多g的容量, 竟然不是2002 的plus。。。。
- datagridview 中的插入列问题
- listview光标定位问题以及颜色设置。。在线等待!!!急!!!
保存数据xml,sql server都可以
double price;
昨天 price=0.035;
今天 price=0.037;
后天........
我怎样把每天的 price 保存下来,然后用控件 listBox 显示出来。
创建新窗体,拖ListBox一个,Button控件2个,分别命名为 Load,Save。然后分别双击2个按钮得到事件,在代码中粘贴如下内容: DataTable table = new DataTable();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
table.Columns.Add("c1",typeof(string));
table.TableName = "test";
listBox1.DataSource = table;
listBox1.DisplayMember = "c1";
} private void listBox1_Click(object sender, EventArgs e)
{
//listBox1.Items.Add(listBox1.Items.Count.ToString());
DataRow row = table.NewRow();
row[0] = listBox1.Items.Count.ToString();
table.Rows.Add(row);
} private void buttonSave_Click(object sender, EventArgs e)
{
table.WriteXml("data.xml");
} private void buttonLoad_Click(object sender, EventArgs e)
{
table.Clear();
table.ReadXml("data.xml");
}
你就能看到效果了。
你的不例题我转不过来,我发我的基本代码来你帮我检查看吧,谢谢了。主程序代码:
public partial class 超市 : Form
{
public 超市()
{
InitializeComponent();
} SaveInfo saveInfo = new SaveInfo();
public double Price1;
public double Price2; private void btnAveeragePrice_Click(object sender, EventArgs e)
{
价格 price = new 价格(this, ref saveInfo);
price.Show();
} private void 添加小米价_Click(object sender, EventArgs e)
{
Price1 = double.Parse(textBox1.Text);
} private void 添加鱼价格_Click(object sender, EventArgs e)
{
Price2 = double.Parse(textBox2.Text);
} private void buttonSave_Click(object sender, EventArgs e)
{
SaveInfo Info = new SaveInfo(Price1, Price2);
FileStream fs = new FileStream("Info.set", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, Info);
fs.Close();
} private void buttonLoad_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("Info.set", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
saveInfo = (SaveInfo)bf.Deserialize(fs);
fs.Close();
}
}
}主程序窗口:l显示窗口代码: public partial class 价格 : Form
{
public 价格()
{
InitializeComponent();
}
超市 super;
SaveInfo saveInfo;
public 价格(超市 super, ref SaveInfo saveInfo)
: this()
{
this.saveInfo = saveInfo;
this.super = super; this.listBox1.Items.Add(saveInfo.Price1);
this.listBox2.Items.Add(saveInfo.Price2);
}
}显示窗口:
类代码: [Serializable]
public class SaveInfo
{
public SaveInfo() { }
public SaveInfo(double Price1, double Price2)
{ this.Price1 = Price1;
this.Price2 = Price2;
}
//成交的平均价格
private double price1;
public double Price1
{
get { return price1; }
set { price1 = value; }
} private double price2;
public double Price2
{
get { return price2; }
set { price2 = value; }
}
}基本上就是这样了,每添加一个新的价格,旧的价格又被覆盖了。不知道用怎么加法解决为好。
{
public 超市()
{
InitializeComponent();
} SaveInfo saveInfo = new SaveInfo();
public double Price1;
public double Price2; private void btnAveeragePrice_Click(object sender, EventArgs e)
{
价格 price = new 价格(this, ref saveInfo);
price.Show();
} private void 添加小米价_Click(object sender, EventArgs e)
{
Price1 = double.Parse(textBox1.Text);
} private void 添加鱼价格_Click(object sender, EventArgs e)
{
Price2 = double.Parse(textBox2.Text);
} private void buttonSave_Click(object sender, EventArgs e)
{
SaveInfo Info = new SaveInfo(Price1, Price2);
FileStream fs = new FileStream("Info.set", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, Info);
fs.Close();
} private void buttonLoad_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("Info.set", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
saveInfo = (SaveInfo)bf.Deserialize(fs);
fs.Close();
}
}
}主程序窗口:l显示窗口代码: public partial class 价格 : Form
{
public 价格()
{
InitializeComponent();
}
超市 super;
SaveInfo saveInfo;
public 价格(超市 super, ref SaveInfo saveInfo)
: this()
{
this.saveInfo = saveInfo;
this.super = super; this.listBox1.Items.Add(saveInfo.Price1);
this.listBox2.Items.Add(saveInfo.Price2);
}
}
显示窗口:类代码: [Serializable]
public class SaveInfo
{
public SaveInfo() { }
public SaveInfo(double Price1, double Price2)
{ this.Price1 = Price1;
this.Price2 = Price2;
}
//成交的平均价格
private double price1;
public double Price1
{
get { return price1; }
set { price1 = value; }
} private double price2;
public double Price2
{
get { return price2; }
set { price2 = value; }
}
}
基本上就是这样了,每添加一个新的价格,旧的价格又被覆盖了。不知道用怎么加法解决为好。
首先你这样保存
FileStream fs;
if(!File.exits(info.set))
fs = new FileStream("Info.set", FileMode.Create);
else
fs = new FileStream("Info.set",FileMode.Append);写进去之后要加个自定义协议来分隔以便后面读取的时候反序列化
fs = new FileSteam ("Info.set",FileMode.Append);
fs.write("|||",2机制);
fs.close();读取字符串 然后split("123");然后得到string[] srets;
foreach(var s in srets)
{
byte[] buff = Encoding.Ascii.getbytes(s);
try
{
saveinfo info = binaryformatter.deserializer(new memorystream(buff));
}
catch(){}
}我写的伪代码 只是提供一种方案 你自己测试下 如果需要我帮你继续的话 再说哈