简体   繁体   中英

Strategy for selecting records for DataSet

In Most common cases, we have two tables (& more) in DB termed as master (eg SalesOrderHeader ) & chirld (eg SalesOrderDetail ). We can read records from DB by one Select with INNER JOIN and additional constaints WHERE for lessen volume data for loading from DB (using "Addater.Fill(DataSet)" )

@"SELECT d.SalesOrderID, d.SalesOrderDetailID, d.OrderQty,
d.ProductID, d.UnitPrice
FROM Sales.SalesOrderDetail d
INNER JOIN Sales.SalesOrderHeader h
ON d.SalesOrderID = h.SalesOrderID
WHERE DATEPART(YEAR, OrderDate) = @year;"

Did I understand right, in this case we receive one table in DataSet, w/o primary and foreign keys, and w/o possibility to set constraint between master and child tables.

This Dataset can be useful only for different queries regarding columns and record which exist in DataSet? We can't using DbCommandBuilder for creating SQLCommands for Insert, Update, Delete based on the SelectCommand which was used for filling DataSet? And simply to Update data in these table in DB?

If we want to organize the local data moddification in tables by using the disconnect layer of ADO.NET, we must populate DataSet by two Select

"SELECT *
FROM Sales.SalesOrderHeader;"
"SELECT *
FROM Sales.SalesOrderDetail;"

After that we must create the primary keys for both table, and set constraint between master and child table. Create by DbCommandBuilder SQLCommands for Insert, Update, Delete.

In that case we will have possibility to any modification data in these tables remotely and after Update records in DB (using "Addater.Update(DataSet)" ).

If we will use one SelectCommand to load data in two tables in DataSet, can we use that SelectCommand for DbCommandBuilder for creating other SQLCommands for "Update" and Update all tables in DataSet by one "Addater.Update(DataSet)" or we must create separate Addapter for Update every table?

If I for economy resources will load only part of records (see below) from table (eg SalesOrderDetail). Do I right understand, that in this case, I can have a possible problems, when I will send new records to DB (by Update), because news records can conflict with existen in DB by primary key (some records which have other value in OrderDate field)?

"SELECT *
FROM Sales.SalesOrderDetail
WHERE DATEPART(YEAR, OrderDate) = @year;"

There is nothing preventing you from writing your own Insert, Update and Delete commands for your first select statement with the join. Of course you will have to determine a way to assure that the foreign keys exist.

Insert Into SalesOrderDetail (SalesOrderID, OrderQty, ProductID, UnitPrice) Values ( @SalesOrderID, @OrderQty, @ProductID, @UnitPrice);

Update SalesOrderDetail Set OrderQty = @OrderQty Where SalesOrderDetailID = @ID;

Delete From SalesOrderDetail Where SalesOrderDetailID = @ID;

You would execute these with ADO.net commands instead of using the adapter. I wrote the sample code in vb.net but I am sure it is easy to change to C# if you prefer.

Private Sub UpdateQuantity(Quant As Integer, DetailID As Integer)
    Using cn As New SqlConnection("Your connection string"),
            cmd As New SqlCommand("Update SalesOrderDetail Set OrderQty = @OrderQty Where SalesOrderDetailID = @ID;")
        cmd.Parameters.Add("@OrderQty", SqlDbType.Int).Value = Quant
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = DetailID
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM