Skip to content

Commit 28e7452

Browse files
Merge pull request #233667 from dzsquared/sqlbinding-april2023-csharp
cleaning up csharp docs
2 parents f59a9b5 + 78e3300 commit 28e7452

File tree

4 files changed

+370
-34
lines changed

4 files changed

+370
-34
lines changed

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

Lines changed: 147 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ namespace AzureSQLSamples
6464
public static IActionResult Run(
6565
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "gettodoitem")]
6666
HttpRequest req,
67-
[Sql("select [Id], [order], [title], [url], [completed] from dbo.ToDo where Id = @Id",
68-
CommandType = System.Data.CommandType.Text,
69-
Parameters = "@Id={Query.id}",
70-
ConnectionStringSetting = "SqlConnectionString")]
67+
[Sql(commandText: "select [Id], [order], [title], [url], [completed] from dbo.ToDo where Id = @Id",
68+
commandText: System.Data.CommandType.Text,
69+
parameters: "@Id={Query.id}",
70+
connectionStringSetting: "SqlConnectionString")]
7171
IEnumerable<ToDoItem> toDoItem)
7272
{
7373
return new OkObjectResult(toDoItem.FirstOrDefault());
@@ -96,10 +96,10 @@ namespace AzureSQLSamples
9696
public static IActionResult Run(
9797
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "gettodoitems/{priority}")]
9898
HttpRequest req,
99-
[Sql("select [Id], [order], [title], [url], [completed] from dbo.ToDo where [Priority] > @Priority",
100-
CommandType = System.Data.CommandType.Text,
101-
Parameters = "@Priority={priority}",
102-
ConnectionStringSetting = "SqlConnectionString")]
99+
[Sql(commandText: "select [Id], [order], [title], [url], [completed] from dbo.ToDo where [Priority] > @Priority",
100+
commandType: System.Data.CommandType.Text,
101+
parameters: "@Priority={priority}",
102+
connectionStringSetting: "SqlConnectionString")]
103103
IEnumerable<ToDoItem> toDoItems)
104104
{
105105
return new OkObjectResult(toDoItems);
@@ -161,10 +161,10 @@ namespace AzureSQLSamples
161161
public static IActionResult Run(
162162
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "gettodoitem")]
163163
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")]
164+
[SqlInput(commandText: "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")]
168168
IEnumerable<ToDoItem> toDoItem)
169169
{
170170
return new OkObjectResult(toDoItem.FirstOrDefault());
@@ -194,10 +194,10 @@ namespace AzureSQLSamples
194194
public static IActionResult Run(
195195
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "gettodoitems/{priority}")]
196196
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")]
197+
[SqlInput(commandText: "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")]
201201
IEnumerable<ToDoItem> toDoItems)
202202
{
203203
return new OkObjectResult(toDoItems);
@@ -227,8 +227,8 @@ namespace AzureSQL.ToDo
227227
public static IActionResult Run(
228228
[HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "DeleteFunction")] HttpRequest req,
229229
ILogger log,
230-
[Sql("DeleteToDo", CommandType = System.Data.CommandType.StoredProcedure,
231-
Parameters = "@Id={Query.id}", ConnectionStringSetting = "SqlConnectionString")]
230+
[SqlInput(commandText: "DeleteToDo", commandType: System.Data.CommandType.StoredProcedure,
231+
parameters: "@Id={Query.id}", connectionStringSetting: "SqlConnectionString")]
232232
IEnumerable<ToDoItem> toDoItems)
233233
{
234234
return new OkObjectResult(toDoItems);
@@ -237,10 +237,137 @@ namespace AzureSQL.ToDo
237237
}
238238
```
239239

240-
<!-- Uncomment to support C# script examples.
241240
# [C# Script](#tab/csharp-script)
242241

243-
-->
242+
243+
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-csharpscript).
244+
245+
This section contains the following examples:
246+
247+
* [HTTP trigger, get row by ID from query string](#http-trigger-look-up-id-from-query-string-csharpscript)
248+
* [HTTP trigger, delete rows](#http-trigger-delete-one-or-multiple-rows-csharpscript)
249+
250+
The examples refer to a `ToDoItem` class and a corresponding database table:
251+
252+
:::code language="csharp" source="~/functions-sql-todo-sample/ToDoModel.cs" range="6-16":::
253+
254+
:::code language="sql" source="~/functions-sql-todo-sample/sql/create.sql" range="1-7":::
255+
256+
<a id="http-trigger-look-up-id-from-query-string-csharpscript"></a>
257+
### HTTP trigger, get row by ID from query string
258+
259+
The following example shows an Azure SQL input binding in a *function.json* file and a [C# script function](functions-reference-csharp.md) that uses the binding. 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.
260+
261+
> [!NOTE]
262+
> The HTTP query string parameter is case-sensitive.
263+
>
264+
265+
Here's the binding data in the *function.json* file:
266+
267+
```json
268+
{
269+
"authLevel": "anonymous",
270+
"type": "httpTrigger",
271+
"direction": "in",
272+
"name": "req",
273+
"methods": [
274+
"get"
275+
]
276+
},
277+
{
278+
"type": "http",
279+
"direction": "out",
280+
"name": "res"
281+
},
282+
{
283+
"name": "todoItem",
284+
"type": "sql",
285+
"direction": "in",
286+
"commandText": "select [Id], [order], [title], [url], [completed] from dbo.ToDo where Id = @Id",
287+
"commandType": "Text",
288+
"parameters": "@Id = {Query.id}",
289+
"connectionStringSetting": "SqlConnectionString"
290+
}
291+
```
292+
293+
The [configuration](#configuration) section explains these properties.
294+
295+
Here's the C# script code:
296+
297+
```cs
298+
#r "Newtonsoft.Json"
299+
300+
using System.Net;
301+
using Microsoft.AspNetCore.Mvc;
302+
using Microsoft.Extensions.Primitives;
303+
using Newtonsoft.Json;
304+
using System.Collections.Generic;
305+
306+
public static IActionResult Run(HttpRequest req, ILogger log, IEnumerable<ToDoItem> todoItem)
307+
{
308+
return new OkObjectResult(todoItem);
309+
}
310+
```
311+
312+
313+
<a id="http-trigger-delete-one-or-multiple-rows-csharpscript"></a>
314+
### HTTP trigger, delete rows
315+
316+
The following example shows an Azure SQL input binding in a *function.json* file and a [C# script function](functions-reference-csharp.md) that uses the binding to execute a stored procedure with input from the HTTP request query parameter. In this example, the stored procedure deletes a single record or all records depending on the value of the parameter.
317+
318+
The stored procedure `dbo.DeleteToDo` must be created on the SQL database.
319+
320+
:::code language="sql" source="~/functions-sql-todo-sample/sql/create.sql" range="11-25":::
321+
322+
Here's the binding data in the *function.json* file:
323+
324+
```json
325+
{
326+
"authLevel": "anonymous",
327+
"type": "httpTrigger",
328+
"direction": "in",
329+
"name": "req",
330+
"methods": [
331+
"get"
332+
]
333+
},
334+
{
335+
"type": "http",
336+
"direction": "out",
337+
"name": "res"
338+
},
339+
{
340+
"name": "todoItems",
341+
"type": "sql",
342+
"direction": "in",
343+
"commandText": "DeleteToDo",
344+
"commandType": "StoredProcedure",
345+
"parameters": "@Id = {Query.id}",
346+
"connectionStringSetting": "SqlConnectionString"
347+
}
348+
```
349+
350+
:::code language="csharp" source="~/functions-sql-todo-sample/DeleteToDo.cs" range="4-30":::
351+
352+
The [configuration](#configuration) section explains these properties.
353+
354+
Here's the C# script code:
355+
356+
```cs
357+
#r "Newtonsoft.Json"
358+
359+
using System.Net;
360+
using Microsoft.AspNetCore.Mvc;
361+
using Microsoft.Extensions.Primitives;
362+
using Newtonsoft.Json;
363+
using System.Collections.Generic;
364+
365+
public static IActionResult Run(HttpRequest req, ILogger log, IEnumerable<ToDoItem> todoItems)
366+
{
367+
return new OkObjectResult(todoItems);
368+
}
369+
```
370+
244371
---
245372

246373
::: zone-end

0 commit comments

Comments
 (0)