OleDbConnection conn = new OleDbConnection();            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Data2.mdb;Persist Security Info=True"; conn.Open();
            OleDbCommand cmd = new OleDbCommand("select top 25 * from way where (LEN(aa)> 14)", conn);
                        
            OleDbDataReader dr = cmd.ExecuteReader(); 
            int count = 0;
            
            while (dr.Read())
            {
                string str = dr.GetString(3).ToString();
                int b = str.LastIndexOf('-');
                if (b > 2 && str.Length > b)
                {
                    str = str.Substring(b, str.Length - b);
                    string sqlstr = "update way set aa='" + str + "' where bb=" + dr.GetValue(5).ToString();
                    System.Diagnostics.Debug.Print(sqlstr);
                    OleDbConnection conn1 = new OleDbConnection();
                    conn1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Data2.mdb;Persist Security Info=True"; 
                    conn1.Open();
                    OleDbCommand cmd1 = new OleDbCommand(sqlstr, conn1);
                    System.Diagnostics.Debug.Print(cmd1.ExecuteNonQuery().ToString());
                    //conn1.Close();
                }
                count++;
            }
            MessageBox.Show(count.ToString());
            conn.Close();运行结果是更新不到数据库的,程序运行以后,执行多次后,后台输出可以看出是更新了,但实际上是没有更新的,好像是没有提交上去!怎么办?

解决方案 »

  1.   

    string sqlstr = "update way set aa='" + str + "' where bb=" + dr.GetValue(5).ToString();
    --------------
    string sqlstr = "update way set aa='" + str + "' where bb='" + dr.GetValue(5).ToString()"';如果bb是int类型,可以不用加单引号。你没有关后门。你执行后的结果是update way set aa=某个值 where bb=
      

  2.   

    调试看看sqlstr ,在查询分析器里看看sqlstr是否正确
      

  3.   

    把语句输出来然后改为select * from way where bb=" + dr.GetValue(5).ToString(); 看看有没有找到内容,如果没有,说明where后面的条件不成立
      

  4.   

    //conn1.Close();
    这句一定要让它执行, 
    改成 conn1.Close();OleDbconnection数据库连接与其它数据库不同, 它的更新只是写入了内存, 没有写到文件上, 只有连接关闭时, 才写入文件
    所以 conn1.Close(); 这句一定要执行
      

  5.   

    conn1.Close(); 这句一定要执行 
    这句是执行了,我帖上来得时候忘记吧注释去掉!还有就食bb的不是字符型,是数字来的,所以不用单引号的!
      

  6.   

    1、你的更新语句不要放在while (dr.Read()) 里面,放在外面。
    2、你的dr要关闭,dr.Close(),再更新,不然可能会影响下面的命令。
      

  7.   

    string sqlstr = "update way set aa='" + str + "' where bb=" + dr.GetValue(5).ToString();
    这条语句,由哪个command对象执行的?我没看到…
      

  8.   

    string sqlstr = "update way set aa='" + str + "' where bb='" + dr.GetValue(5).ToString(); 
    bb那里也应该加个单引号
      

  9.   

    写成string sqlstr = "update way set aa='" + str + "' where bb='" + dr.GetValue(5).ToString()+"';试试,肯定是条件没有传过来所以出错了,根本就没有执行那个循环