小弟我自己做了一个winform程序,想把textbox的数据存储到 自定义的数据文件而不是数据库,
比如说:
          ID     语文  数学  英语
          1       80    60    85
          2       70    55    90
把这些数据写入到自定义的  数据文件,txt之类的
 
表结构如下:
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
| YM    | int(11) | YES  |     | NULL    |                |
| SX    | int(11) | YES  |     | NULL    |                |
| YU    | int(11) | YES  |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
该怎么实现?用结构体?
哪位高手给个代码参考下?学习中...........

解决方案 »

  1.   

    每条记录就是一个struct,整个数据集就是struct的数组,然后再附加一些信息,比如数量,分类等
    比如
    struct A
    {
    public string content;
    public int num;
    }
    struct B
    {
    public int count;
    A[] items
    }
    当然记得用MarshalAs进行说明
      

  2.   

    第一种方法,用结构体,这个比较麻烦
    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Runtime.InteropServices;namespace RWFile{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        //从文件中读结构体        private void button1_Click(object sender, EventArgs e)        {            string strFile = Application.StartupPath + """test.dat";            if (!File.Exists(strFile))            {                MessageBox.Show("文件不存在");                return;            }            FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.ReadWrite);            TestStruct ts = new TestStruct();            byte[] bytData = new byte[Marshal.SizeOf(ts)];            fs.Read(bytData, 0, bytData.Length);            fs.Close();            ts = rawDeserialize(bytData);            textBox1.Text = ts.dTest.ToString();            textBox2.Text = ts.uTest.ToString();            textBox3.Text = Encoding.Default.GetString(ts.bTest);         }        //向文件中写结构体        private void button2_Click(object sender, EventArgs e)        {            string strFile = Application.StartupPath + """test.dat";            FileStream fs = new FileStream(strFile, FileMode.Create , FileAccess.Write);            TestStruct ts = new TestStruct();            ts.dTest = double.Parse(textBox1.Text);            ts.uTest = UInt16.Parse(textBox2.Text);            ts.bTest = Encoding.Default.GetBytes(textBox3.Text);             byte[] bytData = rawSerialize(ts);            fs.Write(bytData, 0, bytData.Length);            fs.Close();        }        [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)] //,Size=16        public struct TestStruct        {            [MarshalAs(UnmanagedType.R8)] //,FieldOffset(0)]             public double dTest;            [MarshalAs(UnmanagedType.U2)] //, FieldOffset(8)]            public UInt16 uTest;            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] //, FieldOffset(10)]            public byte[] bTest;        }        //序列化        public static byte[] rawSerialize(object obj)        {            int rawsize = Marshal.SizeOf(obj);            IntPtr buffer = Marshal.AllocHGlobal(rawsize);            Marshal.StructureToPtr(obj, buffer, false);            byte[] rawdatas = new byte[rawsize];            Marshal.Copy(buffer, rawdatas, 0, rawsize);            Marshal.FreeHGlobal(buffer);            return rawdatas;        }        //反序列化        public static TestStruct rawDeserialize(byte[] rawdatas)        {            Type anytype = typeof(TestStruct);           int rawsize = Marshal.SizeOf(anytype);            if (rawsize > rawdatas.Length) return new TestStruct();            IntPtr buffer = Marshal.AllocHGlobal(rawsize);            Marshal.Copy(rawdatas, 0, buffer, rawsize);            object retobj = Marshal.PtrToStructure(buffer, anytype);            Marshal.FreeHGlobal(buffer);            return (TestStruct)retobj;        }              }}
    第二种方法,你可以使用XML,特别是DataTable的本地xml方法, 简单方便啊。
      

  3.   

    现在不建议这样去做了,这么做太难维护了,不存数据库的话用xml多好,格式规整,维护起来多容易