Skip to content

Commit 0c6e57a

Browse files
Merge pull request #224302 from dzsquared/sqlbindings-oopcsharp
adding out of process docs for sqlbindings
2 parents d8bc065 + 1a9bef0 commit 0c6e57a

File tree

3 files changed

+267
-9
lines changed

3 files changed

+267
-9
lines changed

articles/azure-functions/functions-bindings-azure-sql-input.md

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ For information on setup and configuration details, see the [overview](./functio
2121

2222
::: zone pivot="programming-language-csharp"
2323

24-
More samples for the Azure SQL input binding are available in the [GitHub repository](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csharp).
24+
[!INCLUDE [functions-bindings-csharp-intro](../../includes/functions-bindings-csharp-intro.md)]
2525

2626
# [In-process](#tab/in-process)
2727

28+
More samples for the Azure SQL input binding are available in the [GitHub repository](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csharp).
29+
2830
This section contains the following examples:
2931

3032
* [HTTP trigger, get row by ID from query string](#http-trigger-look-up-id-from-query-string-c)
@@ -119,7 +121,121 @@ The stored procedure `dbo.DeleteToDo` must be created on the SQL database. In t
119121

120122
# [Isolated process](#tab/isolated-process)
121123

122-
Isolated worker process isn't currently supported.
124+
More samples for the Azure SQL input binding are available in the [GitHub repository](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-outofproc).
125+
126+
This section contains the following examples:
127+
128+
* [HTTP trigger, get row by ID from query string](#http-trigger-look-up-id-from-query-string-c-oop)
129+
* [HTTP trigger, get multiple rows from route data](#http-trigger-get-multiple-items-from-route-data-c-oop)
130+
* [HTTP trigger, delete rows](#http-trigger-delete-one-or-multiple-rows-c-oop)
131+
132+
The examples refer to a `ToDoItem` class and a corresponding database table:
133+
134+
:::code language="csharp" source="~/functions-sql-todo-sample/ToDoModel.cs" range="6-16":::
135+
136+
:::code language="sql" source="~/functions-sql-todo-sample/sql/create.sql" range="1-7":::
137+
138+
<a id="http-trigger-look-up-id-from-query-string-c-oop"></a>
139+
### HTTP trigger, get row by ID from query string
140+
141+
The following example shows a [C# function](functions-dotnet-class-library.md) that retrieves a single record. The function is triggered by an HTTP request that uses a query string to specify the ID. That ID is used to retrieve a `ToDoItem` record with the specified query.
142+
143+
> [!NOTE]
144+
> The HTTP query string parameter is case-sensitive.
145+
>
146+
147+
```cs
148+
using System.Collections.Generic;
149+
using System.Linq;
150+
using Microsoft.AspNetCore.Http;
151+
using Microsoft.AspNetCore.Mvc;
152+
using Microsoft.Azure.Functions.Worker;
153+
using Microsoft.Azure.Functions.Worker.Extensions.Sql;
154+
using Microsoft.Azure.Functions.Worker.Http;
155+
156+
namespace AzureSQLSamples
157+
{
158+
public static class GetToDoItem
159+
{
160+
[FunctionName("GetToDoItem")]
161+
public static IActionResult Run(
162+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "gettodoitem")]
163+
HttpRequest req,
164+
[Sql("select [Id], [order], [title], [url], [completed] from dbo.ToDo where Id = @Id",
165+
CommandType = System.Data.CommandType.Text,
166+
Parameters = "@Id={Query.id}",
167+
ConnectionStringSetting = "SqlConnectionString")]
168+
IEnumerable<ToDoItem> toDoItem)
169+
{
170+
return new OkObjectResult(toDoItem.FirstOrDefault());
171+
}
172+
}
173+
}
174+
```
175+
176+
<a id="http-trigger-get-multiple-items-from-route-data-c-oop"></a>
177+
### HTTP trigger, get multiple rows from route parameter
178+
179+
The following example shows a [C# function](functions-dotnet-class-library.md) that retrieves documents returned by the query. The function is triggered by an HTTP request that uses route data to specify the value of a query parameter. That parameter is used to filter the `ToDoItem` records in the specified query.
180+
181+
```cs
182+
using System.Collections.Generic;
183+
using Microsoft.AspNetCore.Http;
184+
using Microsoft.AspNetCore.Mvc;
185+
using Microsoft.Azure.Functions.Worker;
186+
using Microsoft.Azure.Functions.Worker.Extensions.Sql;
187+
using Microsoft.Azure.Functions.Worker.Http;
188+
189+
namespace AzureSQLSamples
190+
{
191+
public static class GetToDoItems
192+
{
193+
[FunctionName("GetToDoItems")]
194+
public static IActionResult Run(
195+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "gettodoitems/{priority}")]
196+
HttpRequest req,
197+
[Sql("select [Id], [order], [title], [url], [completed] from dbo.ToDo where [Priority] > @Priority",
198+
CommandType = System.Data.CommandType.Text,
199+
Parameters = "@Priority={priority}",
200+
ConnectionStringSetting = "SqlConnectionString")]
201+
IEnumerable<ToDoItem> toDoItems)
202+
{
203+
return new OkObjectResult(toDoItems);
204+
}
205+
}
206+
}
207+
```
208+
209+
<a id="http-trigger-delete-one-or-multiple-rows-c-oop"></a>
210+
### HTTP trigger, delete rows
211+
212+
The following example shows a [C# function](functions-dotnet-class-library.md) that executes a stored procedure with input from the HTTP request query parameter.
213+
214+
The stored procedure `dbo.DeleteToDo` must be created on the SQL database. In this example, the stored procedure deletes a single record or all records depending on the value of the parameter.
215+
216+
:::code language="sql" source="~/functions-sql-todo-sample/sql/create.sql" range="11-25":::
217+
218+
```cs
219+
namespace AzureSQL.ToDo
220+
{
221+
public static class DeleteToDo
222+
{
223+
// delete all items or a specific item from querystring
224+
// returns remaining items
225+
// uses input binding with a stored procedure DeleteToDo to delete items and return remaining items
226+
[FunctionName("DeleteToDo")]
227+
public static IActionResult Run(
228+
[HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "DeleteFunction")] HttpRequest req,
229+
ILogger log,
230+
[Sql("DeleteToDo", CommandType = System.Data.CommandType.StoredProcedure,
231+
Parameters = "@Id={Query.id}", ConnectionStringSetting = "SqlConnectionString")]
232+
IEnumerable<ToDoItem> toDoItems)
233+
{
234+
return new OkObjectResult(toDoItems);
235+
}
236+
}
237+
}
238+
```
123239

124240
<!-- Uncomment to support C# script examples.
125241
# [C# Script](#tab/csharp-script)

articles/azure-functions/functions-bindings-azure-sql-output.md

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ For information on setup and configuration details, see the [overview](./functio
2121

2222
::: zone pivot="programming-language-csharp"
2323

24-
More samples for the Azure SQL output binding are available in the [GitHub repository](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csharp).
24+
[!INCLUDE [functions-bindings-csharp-intro](../../includes/functions-bindings-csharp-intro.md)]
2525

2626
# [In-process](#tab/in-process)
2727

28+
More samples for the Azure SQL output binding are available in the [GitHub repository](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csharp).
29+
2830
This section contains the following examples:
2931

3032
* [HTTP trigger, write one record](#http-trigger-write-one-record-c)
@@ -154,7 +156,144 @@ namespace AzureSQLSamples
154156

155157
# [Isolated process](#tab/isolated-process)
156158

157-
Isolated worker process isn't currently supported.
159+
More samples for the Azure SQL output binding are available in the [GitHub repository](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-outofproc).
160+
161+
This section contains the following examples:
162+
163+
* [HTTP trigger, write one record](#http-trigger-write-one-record-c-oop)
164+
* [HTTP trigger, write to two tables](#http-trigger-write-to-two-tables-c-oop)
165+
* [HTTP trigger, write records using IAsyncCollector](#http-trigger-write-records-using-iasynccollector-c-oop)
166+
167+
The examples refer to a `ToDoItem` class and a corresponding database table:
168+
169+
:::code language="csharp" source="~/functions-sql-todo-sample/ToDoModel.cs" range="6-16":::
170+
171+
:::code language="sql" source="~/functions-sql-todo-sample/sql/create.sql" range="1-7":::
172+
173+
174+
<a id="http-trigger-write-one-record-c-oop"></a>
175+
176+
### HTTP trigger, write one record
177+
178+
The following example shows a [C# function](functions-dotnet-class-library.md) that adds a record to a database, using data provided in an HTTP POST request as a JSON body.
179+
180+
:::code language="csharp" source="~/functions-sql-todo-sample/PostToDo.cs" range="4-49":::
181+
182+
<a id="http-trigger-write-to-two-tables-c-oop"></a>
183+
184+
### HTTP trigger, write to two tables
185+
186+
The following example shows a [C# function](functions-dotnet-class-library.md) that adds records to a database in two different tables (`dbo.ToDo` and `dbo.RequestLog`), using data provided in an HTTP POST request as a JSON body and multiple output bindings.
187+
188+
```sql
189+
CREATE TABLE dbo.RequestLog (
190+
Id int identity(1,1) primary key,
191+
RequestTimeStamp datetime2 not null,
192+
ItemCount int not null
193+
)
194+
```
195+
196+
197+
```cs
198+
using System;
199+
using System.Collections.Generic;
200+
using System.IO;
201+
using System.Threading.Tasks;
202+
using Microsoft.Azure.Functions.Worker.Extensions.Sql;
203+
using Microsoft.Azure.Functions.Worker;
204+
using Microsoft.Azure.Functions.Worker.Http;
205+
using Microsoft.Extensions.Logging;
206+
using Newtonsoft.Json;
207+
208+
namespace AzureSQL.ToDo
209+
{
210+
public static class PostToDo
211+
{
212+
// create a new ToDoItem from body object
213+
// uses output binding to insert new item into ToDo table
214+
[FunctionName("PostToDo")]
215+
public static async Task<IActionResult> Run(
216+
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "PostFunction")] HttpRequest req,
217+
ILogger log,
218+
[Sql("dbo.ToDo", ConnectionStringSetting = "SqlConnectionString")] IAsyncCollector<ToDoItem> toDoItems,
219+
[Sql("dbo.RequestLog", ConnectionStringSetting = "SqlConnectionString")] IAsyncCollector<RequestLog> requestLogs)
220+
{
221+
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
222+
ToDoItem toDoItem = JsonConvert.DeserializeObject<ToDoItem>(requestBody);
223+
224+
// generate a new id for the todo item
225+
toDoItem.Id = Guid.NewGuid();
226+
227+
// set Url from env variable ToDoUri
228+
toDoItem.url = Environment.GetEnvironmentVariable("ToDoUri")+"?id="+toDoItem.Id.ToString();
229+
230+
// if completed is not provided, default to false
231+
if (toDoItem.completed == null)
232+
{
233+
toDoItem.completed = false;
234+
}
235+
236+
await toDoItems.AddAsync(toDoItem);
237+
await toDoItems.FlushAsync();
238+
List<ToDoItem> toDoItemList = new List<ToDoItem> { toDoItem };
239+
240+
requestLog = new RequestLog();
241+
requestLog.RequestTimeStamp = DateTime.Now;
242+
requestLog.ItemCount = 1;
243+
await requestLogs.AddAsync(requestLog);
244+
await requestLogs.FlushAsync();
245+
246+
return new OkObjectResult(toDoItemList);
247+
}
248+
}
249+
250+
public class RequestLog {
251+
public DateTime RequestTimeStamp { get; set; }
252+
public int ItemCount { get; set; }
253+
}
254+
}
255+
```
256+
257+
<a id="http-trigger-write-records-using-iasynccollector-c-oop"></a>
258+
259+
### HTTP trigger, write records using IAsyncCollector
260+
261+
The following example shows a [C# function](functions-dotnet-class-library.md) that adds a collection of records to a database, using data provided in an HTTP POST body JSON array.
262+
263+
```cs
264+
using Microsoft.AspNetCore.Http;
265+
using Microsoft.AspNetCore.Mvc;
266+
using Microsoft.Azure.Functions.Worker;
267+
using Microsoft.Azure.Functions.Worker.Extensions.Sql;
268+
using Microsoft.Azure.Functions.Worker.Http;
269+
using Newtonsoft.Json;
270+
using System.IO;
271+
using System.Threading.Tasks;
272+
273+
namespace AzureSQLSamples
274+
{
275+
public static class WriteRecordsAsync
276+
{
277+
[FunctionName("WriteRecordsAsync")]
278+
public static async Task<IActionResult> Run(
279+
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "addtodo-asynccollector")]
280+
HttpRequest req,
281+
[Sql("dbo.ToDo", ConnectionStringSetting = "SqlConnectionString")] IAsyncCollector<ToDoItem> newItems)
282+
{
283+
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
284+
var incomingItems = JsonConvert.DeserializeObject<ToDoItem[]>(requestBody);
285+
foreach (ToDoItem newItem in incomingItems)
286+
{
287+
await newItems.AddAsync(newItem);
288+
}
289+
// Rows are upserted here
290+
await newItems.FlushAsync();
291+
292+
return new CreatedResult($"/api/addtodo-asynccollector", "done");
293+
}
294+
}
295+
}
296+
```
158297

159298
<!-- Uncomment to support C# script examples.
160299
# [C# Script](#tab/csharp-script)

articles/azure-functions/functions-bindings-azure-sql.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@ Functions execute in the same process as the Functions host. To learn more, see
3535

3636
Add the extension to your project by installing this [NuGet package](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.Sql).
3737

38+
```bash
39+
dotnet add package Microsoft.Azure.WebJobs.Extensions.Sql --prerelease
40+
```
41+
3842
# [Isolated process](#tab/isolated-process)
3943

4044
Functions execute in an isolated C# worker process. To learn more, see [Guide for running C# Azure Functions in an isolated worker process](dotnet-isolated-process-guide.md).
4145

42-
> [!NOTE]
43-
> In the current preview, Azure SQL bindings aren't supported when your function app runs in an isolated worker process.
46+
Add the extension to your project by installing this [NuGet package](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.Sql/).
4447

45-
<!--
46-
Add the extension to your project by installing this [NuGet package](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.SignalRService/).
47-
-->
48+
```bash
49+
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.Sql --prerelease
50+
```
4851

4952
<!-- awaiting bundle support
5053
# [C# script](#tab/csharp-script)

0 commit comments

Comments
 (0)