如何保持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)
| 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。 |