如何保持datarow是DataRowState.Added

如何保持datarow是DataRowState.Added


Author: 杨宁  &;nbspFrom:Internet


                大多数情况下,我们希望从数据库读出的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
    (2005-5-18:02:42)

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [C#] 的这些文章也感兴趣:

用C#制作字幕显示屏幕保护
C#实现的列出目录下所有子目录和文件的程序(附思路)
HttpClient, 使用C#操作Web
C#网络编程初探(3)
C#处理数码相片之马赛克的实现
C#下实现动态系统托盘图标
.NET的目标(三)
c#委托与事件心得
MySQL 5 C# sample code using ObjectDataSources
.NETRemoting编程简介