我用类来保存,然后用二进投影保存类。但是每一次保存都会覆盖上一次的数据,有没有办法不用SQL 能不能保存每天的记得。然后用 ListBox 显示。

解决方案 »

  1.   

    每一次保存都会覆盖上一次的数据??
    保存数据xml,sql server都可以
      

  2.   

    比如有一个价格变量   
    double price;
    昨天 price=0.035;
    今天 price=0.037;
    后天........
    我怎样把每天的 price 保存下来,然后用控件 listBox 显示出来。
      

  3.   

    不用数据库,可定要有一个持久化对象用于保存价格吧,那就用XML,文本文件啊,什么不行啊,要加载的时候listbox在从这些资源里面读出来就行了。
      

  4.   

    我只针对问题给例子。
    创建新窗体,拖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");
            }
    你就能看到效果了。
      

  5.   

    点击listbox增加行。点save保存到xml,点load读取xml.
      

  6.   


    你的不例题我转不过来,我发我的基本代码来你帮我检查看吧,谢谢了。主程序代码:
        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; }
            }
        }基本上就是这样了,每添加一个新的价格,旧的价格又被覆盖了。不知道用怎么加法解决为好。
      

  7.   

    你的不例题我转不过来,我发我的基本代码来你帮我检查看吧,谢谢了。主程序代码:  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; }
      }
      }
    基本上就是这样了,每添加一个新的价格,旧的价格又被覆盖了。不知道用怎么加法解决为好。
      

  8.   

    我没看完你的代码 ,大概意思就是你采用序列化的手段来保存实例到一个文件,然后反序列化实例对象,那个肯定当然 你序列化什么就保存什么
    首先你这样保存
    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(){}
    }我写的伪代码 只是提供一种方案 你自己测试下 如果需要我帮你继续的话 再说哈