当前位置:编程学习 > C#/ASP.NET >>

如何保持datarow是DataRowState.Added

答案:     大多数情况下,我们希望从数据库读出的DataSet中的每个DataRow为DataRowState.UnChanged状态。
  但是在表的同步情况下,我们希望DataRow的状态为DataRowState.Added.
  这个很容易被实现:
  你可以设置SqlDataAdapter对象的AcceptChangesDuringFill属性为False.
  这样,读出来的表的DataRow的状态就是DataRowState.Added.
  下面是个例子:
   Public Function GetLogTables(sHostIP As String, _
   sTableName As String, _
   dLastUpdate As Date) As DataSet
  
   Dim oConn As SqlConnection
   Try
   ' create new DataSet and create array of table names
   Dim oDS As New DataSet("IISLogResult")
  
   ' create Connection and DataAdapter with empty
   ' SelectCommand text
   oConn = New SqlConnection( _
   ConfigurationSettings.AppSettings("IISLogs"))
   Dim oDA As New SqlDataAdapter("", oConn)
   'rows must be marked as "added"
   oDA.AcceptChangesDuringFill = False
   oConn.Open()
  
   ' change the SQL statement by setting the
   ' CommandText property of the Command object
   ' already attached to the DataAdapter
   oDA.SelectCommand.CommandText = “SELECT * FROM ” sTableName.Trim()
   ' fill this table in the DataSet
   oDA.Fill(oDS, sTableName.Trim())
  
   Return oDS ' and return the DataSet to the client
   Finally
   oConn.Close()
   End Try
   End Function
  

上一个:C#2.0新特性探究之模拟泛型和内置算法
下一个:优化TryCatch性能

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,