我做的是datagirdview允许客户在线编译,并点击更新按钮后,可以更新数据库中的数据表,但是每次我点击更新都更新不了,我怀疑是自己的连接字符串的问题,
string source = "pcdb.mdb";
            string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + source;
这样写的连接字符串默认是在跟目录下面吗?
DEBUG下的数据库文件跟跟目录下面的哪个是起作用的?更新代码如下OleDbConnection olecon = new OleDbConnection(conn);
OleDbDataAdapter da = new OleDbDataAdapter("select * from jsgx", olecon);
            OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];            //保存 
            da.Update(ds);
请各位看看哪里错了,我每次点更新按钮后,跟没更新一样!

解决方案 »

  1.   

    给你一些更新数据库,代码做参考
    public partial class Form1 : Form
        {
    Form1数据成员#region Form1数据成员
            private DataTable DT = new DataTable();
            private SqlDataAdapter SDA = new SqlDataAdapter();
    #endregionForm1构造函数#region  Form1构造函数 
            public Form1()
            {
                InitializeComponent();
            }
    #endregion连接数据库显示数据#region  连接数据库显示数据
            private void Form1_Load(object sender, EventArgs e)
            {            SqlConnection conn = new SqlConnection("server=127.0.0.1;database=pubs;uid=sa");
                SqlCommand SCD = new SqlCommand("select * from tables", conn);
                SDA.SelectCommand = SCD;
                SDA.Fill(DT);
                dataGridView1.DataSource = DT;
            }
    #endregion使用Update更新数据库#region  使用Update更新数据库
            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                try
                {
                    SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);                
                    SDA.Update(DT);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
                MessageBox.Show("更新成功!");
            }
    #endregion
      

  2.   

    string source = "pcdb.mdb"; 
    加上绝对路径~
      

  3.   

    string source = "pcdb.mdb"; 
    加上绝对路径~
      

  4.   

      OleDbDataAdapter da = new OleDbDataAdapter("select * from jsgx", olecon);
                
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
                try
                {
                    OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
                    //保存 
                    da.Update(ds);            }
                catch(System.Exception ex)
                {
                   MessageBox.Show(ex.ToString()); 
                    return; 
                }
                MessageBox.Show("更新成功!"); 这是我的部分代码,提示更新成功,并没有抓到错误,看来代码应该没问题!
      

  5.   

    dataGridView1.DataSource = Over.Tables["ds"].DefaultView;你重新绑定的时候,try下,看看有没有什么异常。
      

  6.   

    try
                {
                    OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
                    //保存 
                    da.Update(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
    也没抓到异常,郁闷!!
      

  7.   

    跟目录下的数据库我已经删除了,光DEBUG下面有数据库,不存在覆盖的问题吧!
      

  8.   


    public partial class Form1 : Form
        {
    Form1数据成员#region Form1数据成员
            private DataTable DT = new DataTable();
            private SqlDataAdapter SDA = new SqlDataAdapter();
    #endregionForm1构造函数#region  Form1构造函数 
            public Form1()
            {
                InitializeComponent();
            }
    #endregion连接数据库显示数据#region  连接数据库显示数据
            private void Form1_Load(object sender, EventArgs e)
            {            SqlConnection conn = new SqlConnection("server=127.0.0.1;database=pubs;uid=sa");
                SqlCommand SCD = new SqlCommand("select * from tables", conn);
                SDA.SelectCommand = SCD;
                SDA.Fill(DT);
                dataGridView1.DataSource = DT;
            }
    #endregion使用Update更新数据库#region  使用Update更新数据库
            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                try
                {
                    SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);                
                    SDA.Update(DT);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
                MessageBox.Show("更新成功!");
            }
    #endregion