public int UpdateByDataSet(DataSet ds,string strTblName,string strConnection)
  {
   try
  {
   SqlConnection  conn = new SqlConnection(strConnection));
         
   SqlDataAdapter myAdapter = new SqlDataAdapter();
   SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection)this.conn);    
   myAdapter.SelectCommand = myCommand;
   SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);     
   myAdapter.Update(ds,strTblName);  
  
   return 0;
 }
 catch(BusinessException errBU)
 {  
   throw errBU;
 }   
 catch(Exception err)
 {  
   throw new BusinessException(err);
 }
}
请问这个函数是不是都将DS里的数据插入strTblName  中?  意思就是ds.tables[0].Rows[].RowState 都默认为Added?    对这个不熟。

解决方案 »

  1.   

    不是, 有五种状态. New Row,AddRow,AcceptChanges,Modified,Deleted 
      

  2.   

    public static void DataToXml()
            {
                SqlConnection sqlcon = new SqlConnection("....");
                SqlDataAdapter da = new SqlDataAdapter("select * from mytable", sqlcon);
                DataSet ds = new DataSet();
                da.Fill(ds, "mytable");
                ds.WriteXml("C:\\1.xml");
            }        public static void XmlToSqlServer()
            {
                SqlConnection sqlcon = new SqlConnection("....");
                SqlDataAdapter da = new SqlDataAdapter("select * from mytable", sqlcon);
                DataSet ds = new DataSet();
                ds.ReadXml("C:\\1.xml");
                SqlCommandBuilder comb = new SqlCommandBuilder(da);
                da.Update(ds, "mytable");
            }
    我这两个函数先DataToXml()再XmlToSqlServer()应该不出错啊(应该是完全update mytable,只是没变化才对)异常信息,违反了主键约束,他肯定是望里面插数据了才出错的。XmlToSqlServer里的ds默认rowstate都是added吗?是不是我新建的dataset第行状态都是added?
      

  3.   

    是不是我新建的dataset第行状态都是added? 是的
    Modified和Deleted 是相对于上次凋用AcceptChanges()方法之后的操作.
      

  4.   

    那对这里的da.Update(ds, "mytable");做一个事务应该怎么做呢?
     da.insertcommand.transaction=tr这样?
     da.updatecommand.transaction=tr这样?
     da.deletecommand.transaction=tr这样?tr 是一个 SqlTransaction 对象。
      

  5.   

    如果只是更新数据,需要UpdateCommand,如果有插入,就得有InsertCommand,有删除行,就得有DeleteCommand。你这只有SelectCommand,这个在查询数据的时候,会自动产生的,特不要你特意new出来。