表结构:-- Table: demo-- DROP TABLE demo;CREATE TABLE demo
(
  "ID" smallint NOT NULL,
  "Name" character(12),
  "NO" integer,
  CONSTRAINT pk_table_1 PRIMARY KEY ("ID")
)
WITH (OIDS=FALSE);
ALTER TABLE demo OWNER TO demo;Form里放有一个dgv和一个button相关代码如下:        NpgsqlConnection conn = new NpgsqlConnection("Server=.;Port=5432;User Id=demo;Password=demo;Database=demo;");
        NpgsqlCommand comm = new NpgsqlCommand("select * from demo");
        DataSet ds = new DataSet();
        NpgsqlDataAdapter da = new NpgsqlDataAdapter();
        NpgsqlCommandBuilder cb;        private void Form1_Load(object sender, EventArgs e)
        {
            comm.Connection = conn;            da.SelectCommand = comm;            cb = new NpgsqlCommandBuilder(da);            da.Fill(ds, "demo");            dataGridView1.DataSource = ds.Tables["demo"];
        }        private void button1_Click(object sender, EventArgs e)
        {
            if (ds.HasChanges())
            {
                MessageBox.Show("Yes");
                MessageBox.Show(ds.Tables["demo"].Rows[0].RowState.ToString());
                da.Update(ds, "demo");
            }
            else
            {
                MessageBox.Show("NO");
            }
        }
当我在dgv里修改一些数据后,ds.HsaChanges()为true,rowstate为modified,但da.Update()之后,数据库里的数据并没有改变,我怎么看也没有找出问题所在。

解决方案 »

  1.   

    好好看哈自己的代码:哪一句涉及到sql表的修改了?根本就没有
      

  2.   

    你没有捕获异常当然看不到了。
    把可能有异常的地方用try catch ,然后看是不是捕获到了异常。
      

  3.   

    你说的dgv中的数据变了,那dataset中的datatable中的数据也变了吗?
    你后台用代码改变datatable中的数据,然后update,看看能不能改变。
      

  4.   

    参考
    http://topic.csdn.net/u/20081208/11/4C712B0A-CB2B-4B42-8B22-102F2B0D48FD.html
    26楼的回答
      

  5.   


    1楼说看异常的时候我就加了如下代码:                try
                    {                    da.Update(ds, "demo");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
    确实没有异常。
      

  6.   


    fill()的时候会自动打开连接,完成之后又会自己关闭。
      

  7.   

      ds.AcceptChanges();
    http://www.cnblogs.com/ekinyang/archive/2005/12/30/308338.html
      

  8.   


    我在发帖之前已经看了这个帖子了,现在的问题不是rowstate没有改变,通过
    MessageBox.Show(ds.Tables["demo"].Rows[0].RowState.ToString());可以看到修改后的rowstate已经是modified了。仍然不行。
    我使用如下代码跟踪ds的值,发现ds的值已是修改后的值:            foreach (DataRow row in ds.Tables["demo"].Rows)
                {
                    MessageBox.Show(row["NO"].ToString());
                }
      

  9.   

    查看postgresql日志,如下:%t 日志:  在客户端联接上的意外 EOF
    %t 日志:  无法从客户端获得数据: No connection could be made because the target machine actively refused it.
    从postgresql读取数据的时候日志不会出现这个错误,而当da.update()的时候日志才会出现这个错误。
      

  10.   

    ds设置update相关的command了么……貌似没看到你设哎~
      

  11.   

    updata的时候,看看你的CommandBuilder生成的sql语句是什么?
    我用你的代码试了下,可以。
      

  12.   

    如果不行就直接写 updatecommanddataAdapter.UpdateCommand = new OleDbCommand("update PurchasePlan set PurchaseNum=? where SMemberID=? and CommodityID=? and plantype=0", connection);
                   dataAdapter.UpdateCommand.Parameters.Add("PurchaseNum", OleDbType.Integer, 4, "PurchaseNum");
                   dataAdapter.UpdateCommand.Parameters.Add("SMemberID", OleDbType.Integer, 4, "SMemberID");
                   dataAdapter.UpdateCommand.Parameters.Add("CommodityID", OleDbType.Integer, 4, "CommodityID");
    不过也不知道你的是什么Connection?   NpgsqlConnection? conn ?是否是SqlConnection?是否少了一下东西。
      

  13.   


    我现在怀疑是不是postgresql的原因了,你看一下我在16楼的回复。
      

  14.   

    表里有两条记录:
    ID    NAME     NO
    =============
    5     ked      9
    6     wtu      3当我在dgv里修改第一行的NO从9变为8后,再使用update()更新时产生的语句如下:UPDATE "demo"."public"."demo" SET "NO" = 8::int4 WHERE (("ID" = 5::int2) AND ((0::int4 =
     1 AND "Name" IS NULL) OR ("Name" = 'ked         '::text)) AND ((0::int4 = 1 AND "NO" IS NULL) OR ("NO" = 9::int4)))
      

  15.   

    楼主想通过ds的commandbuilder对象自动产生commandtext时务必不要调用dataRow的AcceptChanges()和RejectChanges()方法,因为commandbuilder对象是根据
    ddataRow的rowState来产生commandtext的。
      

  16.   

    我没有调用AcceptChanges()和RejectChanges()