With the Google search one is overloaded and I do not know at the end properly what is good. That's why I ask the experts here.
As you may have noticed, you get a bit of overload here too, not the least when you add several tags. Myself I come from the SQL Server side, while Joe and Karen are .NET experts.
Here is a stored procedure that you can work from:
CREATE TYPE dbo.ProductsEntry_type AS TABLE
(ProductID int NOT NULL PRIMARY KEY,
Quantity int NOT NULL,
Price decimal(10,2) NOT NULL
)
go
CREATE PROCEDURE dbo.AddOrder @CustomerID int,
@EmployeeID int,
@Freight decimal(10,2),
@Products dbo.ProductsEntry_type READONLY,
@OrderID int OUTPUT AS
BEGIN TRANSACTION
SELECT @OrderID = isnull(MAX(OrderID), 0)
FROM dbo.Orders WITH (UPDLOCK)
INSERT dbo.Orders (OrderID, OrderDate, CustomerID,
EmployeeID, Freight)
VALUES(@OrderID, convert(date, sysdatetime()), @CustomerID,
@EmployeeID, @Freight)
INSERT dbo.OrderDetails(OrderID, ProductID, Quantity, Price)
SELECT @OrderID, ProductID, Quantity, Price
FROM @Products
go
Very important: here I'm assuming that the Orders table does not have the IDENTITY property. IDENTITY is a highly abused feature. It has its uses in systems with high-concurrency inserts, and of course you can argue that a true order system qualifies. However, I want you to first learn the basics and to roll your own ID, which I'm doing here. IDENTITY has some problems that can be a nuisance when you run into them, so why use it when you don't need to?
Note here that the data I send into the SP does not include an order number. As I said previously, the order ID is typically created in the database when the order is created. But you can add your own attributes to the pattern above.
Can you show me the best way to do this in C#?
As I said, I have different expertise from Joe and Karen. I can write simple console-mode .NET programs, and I even have an article where I show how to use table-valued parameters in .NET, and I dropped the link to it above. But beyond direct data-access code I am inexperienced in .NET myself. Maybe Karen and Joe can fill in here. Then again, they prefer to do things their way. After all, they know SQL a lot better than I know .NET.