Connection string moved to settings file

Connection string in json/ProductCatalog and json/React apps are moved
to settings file. Changed namespaces in ReactCommentsApp
This commit is contained in:
Jovan Popovic
2016-10-25 10:13:16 -07:00
parent d5d7d4950f
commit 8f715933bb
9 changed files with 60 additions and 21 deletions
@@ -4,4 +4,6 @@
bin/*
obj/*
*.sln
*.log
*.log
Properties/PublishProfiles/*
appsettings.development.json
@@ -7,7 +7,7 @@
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>c8014cba-1952-414a-b78e-f60f9e5c5625</ProjectGuid>
<RootNamespace>CommentsApp</RootNamespace>
<RootNamespace>ReactCommentsApp</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
@@ -4,7 +4,7 @@ using System.Data.SqlClient;
using System.IO;
using System.Threading.Tasks;
namespace CommentApp.Controllers
namespace ReactCommentsApp.Controllers
{
[Route("api/[controller]")]
public class commentsController : Controller
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using System.IO;
namespace TodoApp
namespace ReactCommentsApp
{
public class Program
{
@@ -46,9 +46,28 @@ To run this sample, you need the following prerequisites.
3. From Visual Studio 2015, open the **CommentsReactApp.xproj** file from the root directory. Restore packages using right-click menu on the project in Visual Studio and by choosing Restore Packages item. As an alternative, you may run **dotnet restore** from the command line (from the root folder of application).
4. Locate Startup.cs file in the project, change connection string in ConfigureServices() method to reference your database (default value CommentsDb database on local instance with integrated security), and build solution using Ctrl+Shift+B, right-click on project + Build, Build/Build Solution from menu, or **dotnet build** command from the command line (from the root folder of application).
4. Add a connection string in appsettings.json or appsettings.development.json file. An example of the content of appsettings.development.json is shown in the following configuration:
5. Run the sample app using F5 or Ctrl+F5 in Visual Studio 2015, or using **dotnet run** executed in the command prompt of the project root folder.
```
{
"ConnectionStrings": {
"CommentsDb": "Server=.;Database=CommentsDb;Integrated Security=true"
}
}
```
If your database is hosted on Azure you can add something like:
```
{
"ConnectionStrings": {
"ProductCatalog": "Server=<<SERVER>>.database.windows.net;Database=CommentsDb;User Id=<<USER>>;Password=<<PASSWORD>>"
}
}
```
### Build and run sample
1. Run the sample app using F5 or Ctrl+F5 in Visual Studio 2015, or using **dotnet run** executed in the command prompt of the project root folder.
1. Open /index.html Url to get all comments from database,
2. Add new comment using the form below the list of comments.
@@ -7,7 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Data.SqlClient;
namespace TodoApp
namespace ReactCommentsApp
{
public class Startup
{
@@ -26,8 +26,7 @@ namespace TodoApp
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//const string ConnString = "Server=SERVERNAME.database.windows.net;Database=DATABASENAME;User Id=USERNAME;Password=PASSWORD";
const string ConnString = "Server=.;Database=CommentsDb;Integrated Security=true";
string ConnString = Configuration["ConnectionStrings:CommentsDb"];
services.AddTransient<IQueryPipe>( _=> new QueryPipe(new SqlConnection(ConnString)));
services.AddTransient<ICommand>( _=> new Command(new SqlConnection(ConnString)));
@@ -5,4 +5,6 @@ bin/*
obj/*
*.sln
*.log
*.lock.json
*.lock.json
Properties/PublishProfiles/*
appsettings.development.json
@@ -39,15 +39,38 @@ To run this sample, you need the following prerequisites.
## Run this sample
### Setup
1. Create a database on SQL Server 2016 or Azure SQL Database and set compatibility level to 130.
2. From SQL Server Management Studio or Sql Server Data Tools connect to your SQL Server 2016 or Azure SQL database and execute [sql-scripts/setup.sql](sql-scripts/setup.sql) script that will create and populate Product table and create required stored procedures.
3. From Visual Studio 2015, open the **ProductCatalog.xproj** file from the root directory. Restore packages using right-click menu on the project in Visual Studio and by choosing Restore Packages item. As an alternative, you may run **dotnet restore** from the command line (from the root folder of application).
4. Locate Startup.cs file in the project, change connection string in ConfigureServices() method to reference your database (default value ProductCatalog database on local instance with integrated security), and build solution using Ctrl+Shift+B, right-click on project + Build, Build/Build Solution from menu, or **dotnet build** command from the command line (from the root folder of application).
4. Add a connection string in appsettings.json or appsettings.development.json file. An example of the content of appsettings.development.json is shown in the following configuration:
5. Run the sample app using F5 or Ctrl+F5 in Visual Studio 2015, or using **dotnet run** executed in the command prompt of the project root folder.
```
{
"ConnectionStrings": {
"ProductCatalog": "Server=.;Database=ProductCatalog;Integrated Security=true"
}
}
```
If your database is hosted on Azure you can add something like:
```
{
"ConnectionStrings": {
"ProductCatalog": "Server=<<SERVER>>.database.windows.net;Database=ProductCatalog;User Id=<<USER>>;Password=<<PASSWORD>>"
}
}
```
### Build and run sample
1. Build solution using Ctrl+Shift+B, right-click on project + Build, Build/Build Solution from menu, or **dotnet build** command from the command line (from the root folder of application).
2. Run the sample app using F5 or Ctrl+F5 in Visual Studio 2015, or using **dotnet run** executed in the command prompt of the project root folder.
1. Open /index.html Url to get all products from database,
2. Use **Add** button to add a new product,
3. Edit a product using **Edit** button in table,
@@ -26,9 +26,7 @@ namespace ProductCatalog
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//const string ConnString = "Server=<<SERVER>>.database.windows.net;Database=<<DATABASE>>;User Id=<<USER>>;Password=<<PASSWORD>>";
const string ConnString = "Server=.;Database=ProductCatalog;Integrated Security=true";
string ConnString = Configuration["ConnectionStrings:ProductCatalog"];
services.AddTransient<IQueryPipe>(_ => new QueryPipe(new SqlConnection(ConnString)));
services.AddTransient<ICommand>(_ => new Command(new SqlConnection(ConnString)));