mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Added new samples
BCP, string_agg sample, and comparisng with EF.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using ProductCatalog.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace ProductCatalog.Controllers
|
||||
@@ -46,5 +47,22 @@ namespace ProductCatalog.Controllers
|
||||
ViewData["page"] = "report2";
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpGet(Name = "GetAll")]
|
||||
public IEnumerable<Product> GetAll()
|
||||
{
|
||||
return _context.Products.AsEnumerable<Product>();
|
||||
}
|
||||
|
||||
[HttpGet("GetById")]
|
||||
public IActionResult GetById(int id)
|
||||
{
|
||||
var product = _context.Products.FirstOrDefault(p=>p.ProductId == id);
|
||||
if (product == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return new ObjectResult(product);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Belgrade.SqlClient.SqlDb;
|
||||
using Belgrade.SqlClient;
|
||||
using Belgrade.SqlClient.SqlDb;
|
||||
using Belgrade.SqlClient.SqlDb.Rls;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@@ -58,14 +59,14 @@ namespace ProductCatalog
|
||||
services.AddDbContext<ProductCatalogContext>(options => options.UseSqlServer(new SqlConnection(ConnString)));
|
||||
|
||||
// Adding data access services/components.
|
||||
services.AddTransient(
|
||||
services.AddTransient<IQueryPipe>(
|
||||
sp => new QueryPipe(new SqlConnection(ConnString))
|
||||
.AddRls("CompanyID",() => GetCompanyIdFromSession(sp))
|
||||
//.AddRls("CompanyID",() => GetCompanyIdFromSession(sp))
|
||||
);
|
||||
|
||||
services.AddTransient(
|
||||
services.AddTransient<ICommand>(
|
||||
sp => new Command(new SqlConnection(ConnString))
|
||||
.AddRls("CompanyID", () => GetCompanyIdFromSession(sp))
|
||||
//.AddRls("CompanyID", () => GetCompanyIdFromSession(sp))
|
||||
);
|
||||
|
||||
//// Add framework services.
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"Belgrade.Sql.Client": "0.6.2",
|
||||
"Microsoft.AspNetCore.Mvc": "1.0.0",
|
||||
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
|
||||
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
CREATE TABLE Orders (
|
||||
OrderID int,
|
||||
OrderDate date,
|
||||
Status varchar(16),
|
||||
DeliveryDate date,
|
||||
Data nvarchar(4000)
|
||||
)
|
||||
|
||||
CREATE TABLE OrderLines (
|
||||
OrderLineID int,
|
||||
OrderID int,
|
||||
ProductID int,
|
||||
Quantity int,
|
||||
UnitPrice decimal,
|
||||
TaxRate decimal
|
||||
)
|
||||
|
||||
CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
|
||||
WITH ( TYPE = BLOB_STORAGE, LOCATION = 'https://myazureblobstorage.blob.core.windows.net/data');
|
||||
|
||||
|
||||
BULK INSERT Orders
|
||||
FROM 'orders.bcp'
|
||||
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
|
||||
FORMATFILE = 'orders.fmt',
|
||||
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage',
|
||||
TABLOCK);
|
||||
|
||||
|
||||
BULK INSERT OrderLines
|
||||
FROM 'orderlines.bcp'
|
||||
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
|
||||
FORMATFILE = 'orderlines.fmt',
|
||||
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage',
|
||||
TABLOCK);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
select c.Name, c.Contact, STRING_AGG(p.Name,',') as Products
|
||||
from Company c
|
||||
join Product p on c.CompanyID = p.CompanyID
|
||||
group by c.CompanyID, c.Name, c.Contact
|
||||
|
||||
select c.Name, c.Contact,
|
||||
'[' + STRING_AGG('"' + STRING_ESCAPE(p.Name) + '"',',') + ']' as Products
|
||||
from Company c
|
||||
join Product p on c.CompanyID = p.CompanyID
|
||||
group by c.CompanyID, c.Name, c.Contact
|
||||
|
||||
select c.Name, c.Contact,
|
||||
JSON_QUERY('[' + STRING_AGG('"' + STRING_ESCAPE(p.Name) + '"',',') + ']') as Products
|
||||
from Company c
|
||||
join Product p on c.CompanyID = p.CompanyID
|
||||
group by c.CompanyID, c.Name, c.Contact
|
||||
for json path;
|
||||
|
||||
WITH CustomerAlsoBuy as (
|
||||
select p.ProductID, p2.Name, p2.ProductID as RelatedProductID,
|
||||
ROW_NUMBER() OVER (PARTITION BY p.ProductID ORDER BY count(ol2.OrderID) desc) orders
|
||||
from Product p
|
||||
join OrderLines ol1
|
||||
on p.ProductID = ol1.ProductID
|
||||
join OrderLines ol2
|
||||
on ol1.OrderID = ol2.OrderID
|
||||
and ol1.ProductID <> ol2.ProductID
|
||||
join Product p2
|
||||
on ol2.ProductID = p2.ProductID
|
||||
group by p.ProductID, p.Name, p2.ProductID, p2.Name
|
||||
)
|
||||
select ProductID,
|
||||
'['+STRING_AGG(
|
||||
CONCAT('{"ProductID":',RelatedProductID,',"Product":"',STRING_ESCAPE(Name,'json'),'"}'),',') + ']' Products
|
||||
from CustomerAlsoBuy
|
||||
where orders <= 5
|
||||
group by ProductID
|
||||
+1
@@ -0,0 +1 @@
|
||||
bcp "select OrderLineID, OrderID, 15 + StockItemId%%14 as ProductID, Quantity, UnitPrice, TaxRate from Sales.OrderLines" queryout .\orderlines.bcp -S "." -d WideWorldImporters -T
|
||||
+1
@@ -0,0 +1 @@
|
||||
bcp "select o.OrderID, OrderDate, case when OrderDate > '2016-04-28' then 'Cancelled' when OrderDate <= '2016-04-28' and OrderDate > '2016-01-01' then 'Delivering' when OrderDate <= '2016-01-01' and OrderDate > '2013-01-10' then 'Delivered' else 'Open' end as Status, DATEADD(day, 3 + (o.OrderID %% 7),OrderDate) as DeliveryDate, ReturnedDeliveryData as Data from Sales.Orders o join Sales.Invoices i on o.OrderId = i.OrderId" queryout .\orders.bcp -S "." -d WideWorldImporters -T
|
||||
@@ -0,0 +1 @@
|
||||
bcp dbo.OrderLines in .\orderlines.bcp -f orderlines.fmt -S 127.0.0.1 -d ProductCatalog -T
|
||||
@@ -0,0 +1 @@
|
||||
bcp dbo.Orders in .\orders.bcp -f orders.fmt -S 127.0.0.1 -d ProductCatalog -T
|
||||
+1
@@ -0,0 +1 @@
|
||||
bcp "select OrderLineID, OrderID, 15 + StockItemId%%14 as ProductID, Quantity, UnitPrice, TaxRate from Sales.OrderLines" queryout .\orderlines.bcp -S "." -d WideWorldImporters -T
|
||||
Binary file not shown.
@@ -0,0 +1,8 @@
|
||||
13.0
|
||||
6
|
||||
1 SQLINT 0 4 "" 1 OrderLineID ""
|
||||
2 SQLINT 0 4 "" 2 OrderID ""
|
||||
3 SQLINT 1 4 "" 3 ProductID ""
|
||||
4 SQLINT 0 4 "" 4 Quantity ""
|
||||
5 SQLDECIMAL 1 19 "" 5 UnitPrice ""
|
||||
6 SQLDECIMAL 1 19 "" 6 TaxRate ""
|
||||
Binary file not shown.
@@ -0,0 +1,7 @@
|
||||
13.0
|
||||
5
|
||||
1 SQLINT 0 4 "" 1 OrderID ""
|
||||
2 SQLDATE 1 3 "" 2 OrderDate ""
|
||||
3 SQLCHAR 2 10 "" 3 Status Latin1_General_100_CI_AS
|
||||
4 SQLDATE 1 3 "" 4 DeliveryDate ""
|
||||
5 SQLNCHAR 8 0 "" 5 Data Latin1_General_100_CI_AS
|
||||
Reference in New Issue
Block a user