请大家帮忙看下代码 刚开始自己学NET 按照书上打的代码出了些问题,请大家帮忙指点。
程序运行起来后,可以添加数据 修改数据。 此时数据库中数据的确增加 和修改了。
当关闭程序后。 数据依然存在新增加的数据 和 修改的数据
但是再重新运行后, 新增加的数据 没有了 。修改的数据也还原了。
不知道哪里的原因 请大家帮忙
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ado
{
    public partial class Form1 : Form
    {        private BindingManagerBase Navigator;
        private OleDbConnection MyCon;
        private OleDbCommand MyCom;
        private OleDbDataAdapter MyDataAdapter;
        private DataSet MyDs;
        private int CurRowNo;
        private bool AppendRow;
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“studentDataSet.Student”中。您可以根据需要移动或移除它。
           // this.studentTableAdapter.Fill(this.studentDataSet.Student);
            string CString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Student.mdb";
            string SQLStr = "Select * From Student";
             MyCon = new OleDbConnection(CString);
            MyCon.Open();
            MyCom = new OleDbCommand();
            MyCom.Connection = MyCon;
            MyCom.CommandType = CommandType.Text;
            MyCom.CommandText = SQLStr;
            MyDataAdapter = new OleDbDataAdapter();
            MyDataAdapter.SelectCommand = MyCom;
            MyDs = new DataSet();
            MyDataAdapter.Fill(MyDs,"Student");
            Navigator = this.BindingContext[MyDs, "Student"];            BtnOkOrCancel();
            TextReadOnly(); 
           ;
            DispValue();
        }        private void button1_Click(object sender, EventArgs e)
        {
            Navigator.Position = 0;
            DispValue();
        }        private void button2_Click(object sender, EventArgs e)
        {
            if (Navigator.Position == 0)
                Navigator.Position = Navigator.Count - 1;
            else
                Navigator.Position -= 1;
            DispValue();
        }        private void button3_Click(object sender, EventArgs e)
        {
            if (Navigator.Position == Navigator.Count - 1)
                Navigator.Position = 0;
            else
                Navigator.Position += 1;
            DispValue();
        }        private void button4_Click(object sender, EventArgs e)
        {
            Navigator.Position = Navigator.Count - 1;
            DispValue();
        }        private void TextReadOnly()
        {
            textBox1.ReadOnly = false;
            textBox2.ReadOnly = false;
            textBox3.ReadOnly = false;
            textBox4.ReadOnly = false;
            textBox5.ReadOnly = false;
            textBox6.ReadOnly = true;
           
        }
        private void BtnOkOrCancel()
        
        {
            button1.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = true;
            button7.Enabled = true;
            button10.Enabled = true;
            button8.Enabled = false;
            button9.Enabled = false;
     
        }        private void DispValue()
        
        {
            textBox1.Text = MyDs.Tables["Student"].Rows[Navigator.Position]["StudentID"].ToString();
            textBox2.Text = MyDs.Tables["Student"].Rows[Navigator.Position]["Name"].ToString();
            textBox3.Text = MyDs.Tables["Student"].Rows[Navigator.Position]["Sex"].ToString();
            textBox4.Text = MyDs.Tables["Student"].Rows[Navigator.Position]["ClassID"].ToString();
            textBox5.Text = MyDs.Tables["Student"].Rows[Navigator.Position]["Birthday"].ToString();
            textBox6.Text = MyDs.Tables["Student"].Rows[Navigator.Position]["Native"].ToString();
        }        private void ClearText()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
        }        private void BtnAppOrModi()        {
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            button7.Enabled = false;
            button8.Enabled = true;
            button9.Enabled = true;
            button10.Enabled = false;
        
        }        private void button5_Click(object sender, EventArgs e)
        {
            CurRowNo = Navigator.Position;
            TextReadWrite();
            BtnAppOrModi();
            ClearText();
            AppendRow = true;
        }        private void button6_Click(object sender, EventArgs e)
        {
            TextReadWrite();
            BtnAppOrModi();
            AppendRow = false;
            CurRowNo = Navigator.Position;
        }        private void button7_Click(object sender, EventArgs e)
        {        }        private void button8_Click(object sender, EventArgs e)
        {
            string AppString, ModiString;
            OleDbCommand MyComB = new OleDbCommand();
            MyComB.Connection = MyCon;
            MyComB.CommandType = CommandType.Text;
            if (AppendRow == true)
            {                AppString = "Insert into Student(StudentID,Name,Sex,ClassID,Birthday,Native) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')";
             
                MyComB.CommandText = AppString;
                MyComB.ExecuteNonQuery();            }
            else
            {
                ModiString = "Update Student Set StudentId='" + textBox1.Text + "'";
                ModiString = ModiString + ",Name ='" + textBox2.Text + "'";
                ModiString = ModiString + ",Sex='" + textBox3.Text + "'";
                ModiString = ModiString + ",ClassID='" + textBox4.Text + "'";
                ModiString = ModiString + ",Birthday='" + textBox5.Text + "'";
                ModiString = ModiString + ",Native='" + textBox6.Text + "'";
                MyComB.CommandText = ModiString + "where StudentID='" + textBox1.Text + "'";
                MyComB.ExecuteNonQuery();
            
            }
            MyDataAdapter.Update(MyDs,"Student");
            MyDs.Clear();
            MyDataAdapter.Fill(MyDs,"student");
            TextReadOnly();
            BtnOkOrCancel();
            if (AppendRow == true)
                Navigator.Position = Navigator.Count - 1;
            else
                Navigator.Position = CurRowNo;
            DispValue();        }        private void button9_Click(object sender, EventArgs e)
        {
            TextReadOnly();
            BtnOkOrCancel();
            DispValue();    
        }        private void button10_Click(object sender, EventArgs e)
        {
            MyCon.Close();
            this.Close();
        }        private void TextReadWrite()
        {
            textBox1.ReadOnly = false;
            textBox2.ReadOnly = false;
            textBox3.ReadOnly = false;
            textBox4.ReadOnly = false;
            textBox5.ReadOnly = false;
            textBox6.ReadOnly = false;
        }    }
}数据问题 C#

解决方案 »

  1.   

    winform 程序,请注意你数据库文件放置的问题。每次按F5调试时,Debug目录的DB文件可能被覆盖哈.
      

  2.   

    你的意思是说,修改不了的意思是吧....
    在 winform中WinForm程序运行的时候连接的是bin/Debug下的mdf文件,而不是项目中的mdf文件,这是winform比较麻烦一点...
    在asp.net就不会出现这样...
    你看看这个链接:
    http://sharp.blog.163.com/blog/static/21765814820132865048343/
      

  3.   

    string dataDir = AppDomain.CurrentDomain.BaseDirectory;
                if (dataDir.EndsWith(@"\bin\Debug\")
                    || dataDir.EndsWith(@"\bin\Release\"))
                {
                    dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
                    AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
                }
    加了这个依然不好使  我从网上下了一个例子 WinForm 为什么没有Program.cs 
      

  4.   

    winform不一定非得有Program.cs吧,按说就是有程序入口就行吧。