我试着用SqlDataAdapter的update去做,发现,我新添的Row,在update后,系统没有添加记录操作得到的主键,和其他的一些带默认值的数据给取回来,比方: 
原先有3条,我添加一条,只填Ur_UserName和Ur_Password的值,DataTable如下: 
Ur_ID Ur_UserName Ur_Password Ur_OrganID 
1    张三          123        1 
2    李四          xxx        1 
3    王二        123        1 
      李逵        123 然后update,数据库sqlServer内容如下: 
Ur_ID Ur_UserName Ur_Password Ur_OrganID 
1    张三          123        1 
2    李四          xxx        1 
3    王二          123        1 
4    李逵          123        1 这个正确的,可是内存里面的DataTable没有像这个一样,而是和添加前的一致: 
Ur_ID Ur_UserName Ur_Password Ur_OrganID 
1    张三          123        1 
2    李四          xxx        1 
3    王二          123        1 
      李逵          123 
Ur_ID的4(Sqlserver自动编号),以及Ur_OrganID的1(字段默认值),都没有反应到内存里面的DataTable中!!! 我只是需要刷新添加的那一行记录,要是重新读取所有表,我知道怎么做

解决方案 »

  1.   

    在update后,系统没有添加记录操作得到的主键,和其他的一些带默认值的数据给取回来SqlDataAdapter的update是将表更新到指定的数据库或数据集,但是不会把更新的结果返回,所以原来你用来
    更新的表是什么样的,更新完后还是什么样。
    你可以选择:
    1.先在要更新的表里填好完整的数据,比如 “4 李逵 123  1” (索引可以通过查询得到)
    然后再更新数据库,那么这样就保持了内存表跟数据库表内容的一致性。
    2.先更新,后查询返回库中表。
      

  2.   

    这个好像要重新执行sqlcommand, 再载入datasource才得行
      

  3.   

    我看到有一本书上是这样写的:
    this.m_adapter.InsertCommand = new System.Data.SqlClient.SqlCommand();
    this.m_adapter.InsertCommand.Connection = this.Connection;
    this.m_adapter.InsertCommand.CommandText =
    "INSERT INTO [dbo].[Animals] ([AnimalID], [AnimalName]) " +
    " VALUES (@AnimalID, @Animal" +
    "Name);\r\nSELECT AnimalID, AnimalName FROM Animals WHERE " +
    " (AnimalID = @AnimalID)";
    this.m_adapter.InsertCommand.CommandType = System.Data.CommandType.Text;
    this.m_adapter.InsertCommand.Parameters.Add(
    new System.Data.SqlClient.SqlParameter("@AnimalID",
    System.Data.SqlDbType.Int, 0, System.Data.ParameterDirection.Input, 0, 0,
    "AnimalID", System.Data.DataRowVersion.Current, false, null, "", "", ""));
    this.m_adapter.InsertCommand.Parameters.Add(
    new System.Data.SqlClient.SqlParameter("@AnimalName",
    System.Data.SqlDbType.VarChar, 0, System.Data.ParameterDirection.Input, 0,
    0, "AnimalName", System.Data.DataRowVersion.Current,
    false, null, "", "", ""));
    他的InsertCommand.CommandText 里面先是Insert紧接着是Select,我用的是sqlcommandbuilder的方式自动生成InsertCommand,手工加上select部分,似乎不起效果
      

  4.   

    SqlDataAdapter的update是将表更新到指定的数据库或数据集,但是不会把更新的结果返回,所以原来你用来 
    更新的表是什么样的,更新完后还是什么样。 
    你可以选择: 
    1.先在要更新的表里填好完整的数据,比如 “4 李逵 123  1” (索引可以通过查询得到) 
    然后再更新数据库,那么这样就保持了内存表跟数据库表内容的一致性。 
    2.先更新,后查询返回库中表。
    3.或者设置DataTable中对应的列设为递增。
    如下:DataColumn dc1 = new DataColumn("ID", typeof(int)); 
            dc1.AllowDBNull = false;//不允许为空 
            dc1.AutoIncrement = true;//自动递增 
            dc1.AutoIncrementSeed = 1;//列起始值为1 
            dc1.AutoIncrementStep = 1;//步长为1 
      

  5.   

    myAdapter.SelectCommand/InsertCommand/UpdataCommand = myCommand;
    SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);     
    myAdapter.Update(ds,strTblName); 
    在需要缓存的时候,用上面的方法,可以在页面上多次进行编辑/删除/更新后,最后一并交给数据库