A DataSet
/DataTable
是数据库的离线/内存表示。如果要更新数据库,则需要使用DataAdapter
.
例如(假设您使用的是 MS-Sql-Server):
Public Function UpdateDataSet(dataSet As DataSet) As Int32
Using con = New SqlConnection(My.Settings.SqlConnection)
Dim sql = "INSERT INTO TUser(Name,Address)VALUES(@Name,@Address)"
Using cmd = New SqlCommand(sql, con)
cmd.Parameters.Add(New SqlParameter("@Name", SqlDbType.VarChar))
cmd.Parameters.Add(New SqlParameter("@Address", SqlDbType.VarChar))
Using da = New SqlDataAdapter()
da.InsertCommand = cmd
con.Open()
Dim rowCount = da.Update(dataSet)
Return rowCount
End Using
End Using
End Using
End Function