{"id":13775027,"url":"https://github.com/azure-samples/azure-sql-db-dotnet-rest-api","last_synced_at":"2025-05-11T07:31:37.782Z","repository":{"id":53093453,"uuid":"228347469","full_name":"Azure-Samples/azure-sql-db-dotnet-rest-api","owner":"Azure-Samples","description":"REST API using .Net, Dapper and Azure SQL ","archived":false,"fork":false,"pushed_at":"2023-10-23T18:36:20.000Z","size":44,"stargazers_count":32,"open_issues_count":0,"forks_count":28,"subscribers_count":18,"default_branch":"master","last_synced_at":"2024-04-11T17:02:21.497Z","etag":null,"topics":["azure","azure-sql-database","azure-sql-server","dapper","json","net-core","rest-api","restful-api"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Azure-Samples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-12-16T09:15:20.000Z","updated_at":"2023-11-24T07:52:23.000Z","dependencies_parsed_at":"2024-01-13T11:32:36.678Z","dependency_job_id":null,"html_url":"https://github.com/Azure-Samples/azure-sql-db-dotnet-rest-api","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fazure-sql-db-dotnet-rest-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fazure-sql-db-dotnet-rest-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fazure-sql-db-dotnet-rest-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Fazure-sql-db-dotnet-rest-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Azure-Samples","download_url":"https://codeload.github.com/Azure-Samples/azure-sql-db-dotnet-rest-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253533504,"owners_count":21923447,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["azure","azure-sql-database","azure-sql-server","dapper","json","net-core","rest-api","restful-api"],"created_at":"2024-08-03T17:01:32.944Z","updated_at":"2025-05-11T07:31:37.498Z","avatar_url":"https://github.com/Azure-Samples.png","language":"C#","readme":"---\r\npage_type: sample\r\nlanguages:\r\n- tsql\r\n- sql\r\n- aspx-csharp\r\n- json\r\nproducts:\r\n- azure\r\n- dotnet\r\n- aspnet\r\n- aspnet-core\r\n- azure-app-service\r\n- vs-code\r\n- azure-sql-database\r\ndescription: \"Creating a modern REST API with .NET Core and Azure SQL, using Dapper and Visual Studio Code\"\r\nurlFragment: \"azure-sql-db-dotnet-rest-api\"\r\n---\r\n\r\n# Creating a REST API with .NET Core and Azure SQL\r\n\r\n![License](https://img.shields.io/badge/license-MIT-green.svg)\r\n\r\n\u003c!-- \r\nGuidelines on README format: https://review.docs.microsoft.com/help/onboard/admin/samples/concepts/readme-template?branch=master\r\n\r\nGuidance on onboarding samples to docs.microsoft.com/samples: https://review.docs.microsoft.com/help/onboard/admin/samples/process/onboarding?branch=master\r\n\r\nTaxonomies for products and languages: https://review.docs.microsoft.com/new-hope/information-architecture/metadata/taxonomies?branch=master\r\n--\u003e\r\n\r\nThanks to native JSON support, creating a REST API with Azure SQL and .NET Core is really a matter of a few lines of code:\r\n\r\n```csharp\r\n    var qr = await conn.ExecuteScalarAsync\u003cstring\u003e(\r\n        sql: procedure,\r\n        param: parameters,\r\n        commandType: CommandType.StoredProcedure\r\n    );\r\n    \r\n    var result = JsonDocument.Parse(qr);\r\n```\r\n\r\nThis is possible thanks to [Azure SQL native support to JSON](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-json-features) format and the MicroORM [Dapper](https://medium.com/dapper-net/get-started-with-dapper-net-591592c335aa) that removes all the plumbing code and returns not tables and columns but a fully deserialized object. Object that can be a POCO object or just JSON for maximum flexibilty.\r\n\r\n## Install Sample Database\r\n\r\nIn order to run this sample, the WideWorldImporters database is needed. Install WideWorldImporters sample database:\r\n\r\n[Restore WideWorldImporters Database](https://github.com/yorek/azure-sql-db-samples#restore-wideworldimporters-database)\r\n\r\n## Add Database Objects\r\n\r\nOnce the sample database has been installed, you need to add some stored procedures that will be called from .NET. The SQL code is available here:\r\n\r\n`./SQL/WideWorldImportersUpdates.sql`\r\n\r\nIf you need any help in executing the SQL script, you can find a Quickstart here: [Quickstart: Use Azure Data Studio to connect and query Azure SQL database](https://docs.microsoft.com/en-us/sql/azure-data-studio/quickstart-sql-database)\r\n\r\n## Run sample locally\r\n\r\nMake sure you have [.NET Core 6.0](https://dotnet.microsoft.com/download) SDK installed on your machine. Clone this repo in a directory on your computer and then configure the connection string in `appsettings.json`.\r\n\r\nIf you don't want to save the connection string in the `appsettings.json` file for security reasons, you can just set it using an environment variable:\r\n\r\nLinux:\r\n\r\n```bash\r\nexport ConnectionStrings__DefaultConnection=\"\u003cyour-connection-string\u003e\"\r\n```\r\n\r\nWindows (Powershell):\r\n\r\n```powershell\r\n$Env:ConnectionStrings__DefaultConnection=\"\u003cyour-connection-string\u003e\"\r\n```\r\n\r\nYour connection string is something like:\r\n\r\n```text\r\nSERVER=\u003cyour-server-name\u003e.database.windows.net;DATABASE=\u003cyour-database-name\u003e;UID=DotNetWebApp;PWD=a987REALLY#$%TRONGpa44w0rd!\r\n```\r\n\r\nJust replace `\u003cyour-server-name\u003e` and `\u003cyour-database-name\u003e` with the correct values for your environment.\r\n\r\nTo run and test the REST API locally, just run\r\n\r\n```bash\r\ndotnet run\r\n```\r\n\r\n.NET will start the HTTP server and when everything is up and running you'll see something like\r\n\r\n```text\r\nNow listening on: https://localhost:5001\r\n```\r\n\r\nUsing a REST Client (such as [Visual Studio](https://learn.microsoft.com/aspnet/core/test/http-files), [Insomnia](https://insomnia.rest/), [Curl](https://curl.se/docs/httpscripting.html) or PowerShell's [Invoke-RestMethod](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/invoke-restmethod)), you can now call your API, for example:\r\n\r\n```bash\r\ncurl -k -X GET https://localhost:5001/customer/123\r\n```\r\n\r\nand you'll get info on Customer 123:\r\n\r\n```json\r\n[\r\n    {\r\n        \"CustomerID\": 123,\r\n        \"CustomerName\": \"Tailspin Toys (Roe Park, NY)\",\r\n        \"PhoneNumber\": \"(212) 555-0100\",\r\n        \"FaxNumber\": \"(212) 555-0101\",\r\n        \"WebsiteURL\": \"http://www.tailspintoys.com/RoePark\",\r\n        \"Delivery\": {\r\n            \"AddressLine1\": \"Shop 219\",\r\n            \"AddressLine2\": \"528 Persson Road\",\r\n            \"PostalCode\": \"90775\"\r\n        }\r\n    }\r\n]\r\n```\r\n\r\nCheck out more samples to test all implemented verbs here:\r\n\r\n[cUrl Samples](./Sample-Usage.md)\r\n\r\n## Deploy to Azure\r\n\r\nNow that your REST API solution is ready, it's time to deploy it on Azure so that anyone can take advantage of it. A detailed article on how you can that that is here:\r\n\r\n- [Create an ASP.NET Core app in App Service on Linux](https://docs.microsoft.com/en-us/azure/app-service/containers/quickstart-dotnetcore)\r\n\r\nThe only thing you have do in addition to what explained in the above article is to add the connection string to the Azure Web App configuration. Using AZ CLI, for example:\r\n\r\n```bash\r\nappName=\"azure-sql-db-dotnet-rest-api\"\r\nresourceGroup=\"my-resource-group\"\r\n\r\naz webapp config connection-string set \\\r\n    -g $resourceGroup \\\r\n    -n $appName \\\r\n    --settings DefaultConnection=$ConnectionStrings__DefaultConnection \\\r\n    --connection-string-type=SQLAzure\r\n```\r\n\r\nJust make sure you correctly set `$appName` and `$resourceGroup` to match your environment and also that the variable `$ConnectionStrings__DefaultConnection` as also been set, as mentioned in section \"Run sample locally\". \r\n\r\nAn example of a full script that deploys the REST API is available here: `azure-deploy.sh`.\r\n\r\n## Learn more\r\n\r\nIf you're new to .NET and want to learn more, there are a lot of tutorial available on the [Microsoft Learn](https://docs.microsoft.com/en-us/learn/browse/?products=dotnet) platform. You can start from here, for example:\r\n\r\n- https://docs.microsoft.com/en-us/learn/modules/build-web-api-net-core/?view=aspnetcore-3.1\r\n\r\nIf you also want to learn more about Visual Studio Code, here's another resource:\r\n\r\n[Using .NET Core in Visual Studio Code](https://code.visualstudio.com/docs/languages/dotnet)\r\n\r\n## Contributing\r\n\r\nThis project welcomes contributions and suggestions.  Most contributions require you to agree to a\r\nContributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us\r\nthe rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.\r\n\r\nWhen you submit a pull request, a CLA bot will automatically determine whether you need to provide\r\na CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions\r\nprovided by the bot. You will only need to do this once across all repos using our CLA.\r\n\r\nThis project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).\r\nFor more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or\r\ncontact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.\r\n","funding_links":[],"categories":["Code Samples"],"sub_categories":["REST"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazure-samples%2Fazure-sql-db-dotnet-rest-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fazure-samples%2Fazure-sql-db-dotnet-rest-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazure-samples%2Fazure-sql-db-dotnet-rest-api/lists"}