Microsoft Azure Step By Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 465 [warning: Documents this large are best viewed by clicking the View PDF Link!]

Microsoft Azure Step by Step
Guide
This free book is provided by courtesy of C# Corner and Mindcracker Network and
its authors. Feel free to share this book with your friends and co-workers. Please do
not reproduce, republish, edit or copy this book.
Nitin Pandit
Software Engineer,
Microsoft.Net Consultant Noida
C# corner Delhi Chapter Lead
C# corner MVP
Amit Mishra
(Co-Author, Format Editor)
Index
Page No
Chapter 1 - Getting Started With Microsoft Azure
1-17
Chapter 2 - How To Host Your Web Apps On Azure Via FileZilla
18-31
Chapter 3 - Configuring A Custom Domain Name For An Azure
Website
32-40
Chapter 4 - Virtual Machine In Microsoft Azure Step by Step - Part
One
41-50
Chapter 5 - Creating A Virtual Disk Virtual Machine In Microsoft
Azure Step by Step: Part Two
51-66
Chapter 6 - Use of SQL Azure In Visual Studio 2015 Step By Step
Guide
67-98
Chapter 7 - Entity Framework With Microsoft Azure SQL
99-113
Chapter 8 - Introducing U-SQL- Make Big Data Processing Easy
114-117
Chapter 9 - Installation Of Azure SDK And Blob Storage In Azure:
Part 1
118-139
Chapter 10 - Upload Image To Azure Blob Storage In ASP.NET: Part
2
140-163
Chapter 11 - Demo Project Azure Blob Storage With ASP.NET MVC
5.0: Part 3
164-204
Chapter 12 - Getting Started With Azure DocumentDB: Day 1
205-224
Chapter 13 - Querying In The Azure DocumentDB: Day Two
225-242
Chapter 14 - Manage Azure Mobile Services: Part One
243-259
Chapter 15 - How To Create A Mobile App In Microsoft Azure
260-277
Chapter 16 - Manage Database Settings Of Azure Mobile Services
278-281
Chapter 17 - Get Twitter Account Connected Into Microsoft Azure
Blob Storage Live
282-333
Chapter 18 - Cloud Services Scheduler In Microsoft Azure
334-339
Chapter 19 - Azure Media Services Overview And Playing Media
340-352
Chapter 20 - How To Use API Apps In Azure
353-362
Chapter 21 - Manage Data In Microsoft Azure Machine Learning
363-389
Chapter 22 - Working With Azure RemoteApp
390-402
Chapter 23 - Microsoft Azure Service Bus
403-417
Chapter 24 - Top Interview Questions and Answers Of Cloud
Computing And Microsoft Azure
418-465
About Author
He is a Microsoft Professional having Master's degree in Computer Science. With over
5 years of experience his rick skill set includes designing, integrating, implementing,
ORM with LINQ, WCF, MVC & managing IT/Web applications. He is an expert in
C#.NET ADO.NET, LINQ to SQL, LINQ to EF, ASP.NET 2.0/3x/4.0, WCF, MVC 5.0
(Razor), WPF and Silverlight, including client side technologies jQuery and AngularJS.
He has trained more than one lakh students and professionals as a speaker for
workshops and AppFests conducted in more than 25 universities of North India.
Nitin Pandit
Software Engineer,
Microsoft.Net Consultant Noida
C# corner Delhi Chapter Lead
C# corner MVP
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
1
Getting Started With Microsoft Azure
I am going to start a new article for users who want to learn Microsoft Azure from the scratch.
Let's start with a step by step approach.
Firstly, go to http://Azure.microsoft.com/ and create a free Microsoft Azure Account.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2
When you log in Microsoft Azure with any Microsoft Account like Outlook, Live or other, you
will get an Account. After that go to https://manage.windowsAzure.com/ and login, then click on
portal link.
You can see the image in the following portal.
Now you can manage your Azure portal from web account or directly from your Visual Studio
using Azure SDK. If you are using VS2012, download Azure SDK first.
And after installing Azure SDK, your Visual Studio Server Explorer will be visible.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
3
Right Click on SDK and click ―Connect to Microsoft Azure Subscription‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
4
It will open a window to log in Microsoft Azure Account.
Enter your Microsoft Account Id on which you have Azure Subscription, then password details
and press enter to login.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
5
Now go to Azure SDK in Visual Studio Server Explorer and open Web Sites. Now you can see
all the web sites created in your Azure Account.
If you need to add or create a new website in your Azure Account, we have the following two
ways:
1. By Microsoft Azure SDK in Visual Studio.
2. By Microsoft Azure Web Portal.
So let’s see both one by one and step by step.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
6
By Microsoft Azure SDK in Visual Studio:
Step - Go to Azure SDK in Server Explorer and right click on Web Sites and find the link
Create New Site‖.
Now you’ll see a window where you have to enter the following details:
1. Site Name.
2. Select Region.
3. Database Server (if you have).
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
7
Let say I have entered my new site name meandmy‖, if the name is available it will allow me
to create web site. Select a region like I selected East Asia. At last select database if you have
any database on Azure for your web site.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
8
When you click on create button, you can see your web site to SDK or Azure web portal.
Now go to your Azure portal and refresh. After refreshing it, you can see the same site in your
web portal created by SDK.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
9
By Microsoft Azure Web Portal
Now I am going to create web site from Web Portal, so let’s go to web portal and see the NEW
button on the left side bottom and click on it.
Now click COMPUTE, then WEB APP and QUICK CREATE:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
10
Now enter a URL for Website on which you need to host you web site and select APP SERVICE
PLAN and lastly click CREATE WEB APP:
After that enter all the details,
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
11
Now web site is ready to use.
If you want to use this in SDK, open SDK and right click on Refresh.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
12
Now you can see the websites on SDK.
Create Web Project in Visual Studio to host on Web site that is already created on Azure
Account. Open Visual Studio, File, and then New Project and select Web. After that click
ASP.NET Empty Web Application and press OK.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
13
Open Solution Explorer now and right click on Project, then add any Default page to View the
Test Output.
Write any name for view page.
I just wrote a welcome message on page for testing.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
14
If I run this web application on local system, the output will be the following:
Now I will upload this to web site in Azure.
Firstly, right click on the project and find the link Publish...
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
15
Select the publish target select type and if you want to publish on Microsoft Azure, select the
first link in the following window and click Publish button.
Now select your website name from the dropdown where you need to upload.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
16
After selecting the website, it will download all the publish profiles from Azure and will fill all
the details.
Click on Validate Connection button to check all the settings are valid or not. If all are valid,
clickPublish Button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
17
Now see the upload progress in output window.
After Publish succeeds it will directly open the URL in the Web Browser. Now you can
check this link anywhere or any device like mobile.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
18
How to Host Your Web Apps on Azure via FileZilla
In this chapter, I’m going to upload a website to the Microsoft Azure via FileZilla (FTP).
FileZilla is a cross platform FTP application which is totally free software. We’ll create a Web
App then download and publish profile of that Web App.
For creating a new Web App you must login into Azure.
Go to WEB APPS, and select ―NEW‖ button at the bottom of the window.
Go to COMPUTE, WEB APP, And QUICK CREATE.
Enter the name of your Web App.
Enter your subscription.
Select region/location of your Web App.
Click on to ―CREATE WEB APP”.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
19
Wait for few seconds to complete processing, your WebApp is created and in running mode.
After creating our Web App on Azure, you can start with Visual Studio, write some text in to
HTML page and that text will show in browser. We’ll see how we can publish our website to
Azure without Visual Studio.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
20
So, create a simple application from Visual Studio and make some HTML text in to HTML page.
Here's the screenshot,
So, now our Web App and HTML text are completed, go back to your Web App.
Click on to your Existing Web App and select ―DASHBOARD‖ to download the publish profile
of your Web App.
In dashboard panel, click on to ―Download the Publish Profile‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
21
Right click on the downloadable file and open it with Notepad to collect all the FTP information.
In the following screenshot, you will get publishUrl (which is your host name), username and
user password.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
22
Now you will need to download FileZilla software from its official website. For more you can
see the following screenshot,
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
23
Accept the terms of license agreement by clicking on ―I Agree‖.
This will ask for administration of FileZilla whether you want for Anyone or only for you, so
here I’m selecting all users, click on to next button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
24
Next step of setup is selecting components of FileZilla which you want to install, so mark all the
components and click on the next button.
Select the path of installation, and click on the next button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
25
The following screenshot is asking for start menu and if you do not want to create a shortcut then
check on "Do not create shortcut", and click on Install‖ button.
Your setup is being installed.
Click on to finish button to complete the process of setup.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
26
In File menu select ―Site Manager‖.
Under the site manager, click on to New Site‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
27
Give a name to your new site.
Under the General tab in the right side give the following values according to the following
screenshot,
Host: Paste the hostname from publishUrl obtained from the published settings file above in
Notepad.
Logon Type: Set this to Normal.
User: Paste the userName obtained from the publish settings file above in Notepad.
Password: Paste the userPWD obtained from the publish settings file above in Notepad.
Click on Connect to connect to the site over FTP.
You will see two folders under the root: Logfiles and Site.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
28
Or you can follow the following procedure,
After connecting to the server your status will be successful and wwwroot folder will get created
in directory.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
29
Now go to your Visual Studio project and select all the files of project, by clicking right, upload
it.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
30
So, we created an html file with ―info‖ named in my Visual Studio, change the name of file
info.html to index.html. For more see the following screenshot.
We have a connection with the FTP Server on Azure Cloud and on our local PC.
If everything is completed successfully then go back to your Web App and click on it.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
31
So here I got my result, I have successfully deployed our HTML page. We can publish our
website to Azure without Visual Studio.
I hope you enjoyed this chapter.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
32
Configuring a Custom Domain Name for an Azure
Website
A domain name is a URL which is responsible to access websites. Domain name identifies IP
address. Domain name is an identification string which defines a scope of administrative
autonomy, control within Internet. Any domain name locates Internet address for an URL at
points to Internet and particular host server named.
For managing our custom domain name, you must have to login with Azure.
Firstly, we need to create a Web App, so go to Web Apps blade and click on ―New‖ button in the
bottom of the page.
In compute section creates a Web App.
Give a name to your Web App; here I have given it ―customdomainweb‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
33
Select your Service Plan whether you want the location of your Web App and then click on to
CREATE WEB APP‖.
Web App will be successfully created, click on it for further.
Click on ―SCALE‖ panel to select the mode for your Web App.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
34
In the following screenshot, for custom domain name we need to select app service plan pricing
tier which is available in Shared, Basic and Standard modes for the Web App. Select Shared plan
and save it.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
35
Click on to ―Dashboard‖ to manage custom domain, in the bottom of the window hit on
―Manage Domains‖.
So, my Web App has a domain and virtual IP Address, we need to map this IP Address to the
specific domain which we can buy from the Godaddy website.
Put your domain name which you want to set, when you want to put your domain name it will
mark as red because we do not map with specific domain name.
Here also we can see the IP Address and CNAME (Canonical Name) to map our particluar
website to specific domain name.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
36
Log into your domain provider, here, I’m using Godaddy account, and go to ―DNS Zone File‖
panel to make some changes.
We need to add a record of Host, click on to ―Add Record‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
37
A record, maps a domain name to an IP Address means customdomainweb.com mapped to my
IP.
Record Type: Record Type would be A(Host) from the dropdown menu.
Host: In the Host panel give to @ symbol.
Points To: Enter IP Address of your Web App.
According to your choice give Time to Live to it.
Click on to finish button to add this record.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
38
A record will be created, for saving the change click on to Save Changes.
Now we need to add CNAME, which maps a domain name to another domain name means
www.customdomainweb.com mapped to customdomainweb.Azurewebsites.net.
To create a CNAME click on to ―Add Record‖.
Choose the CNAME from the dropdown menu.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
39
An awverify record is used to determine whether our own domain is going to attempt to use
something means CNAME record that maps from awverify.customdomainweb.Azurewebsites.net
to awverify.
Add another CNAME points to map.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
40
Click on to save changes to save all the changes.
So here in the following screenshot put your domain name, it will be mark as green, means you
have completed custom domain name.
Just Click on button which is marked in circle and enjoy your website live on Azure with the
domain name you want.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
41
Virtual Machine in Microsoft Azure Step by Step
You can create a virtual machine in Azure using one of the following methods:
Use Quick Create when you need only one virtual machine for your solution, or when
you need to connect it to a virtual network.
Use From Gallery when you need a solution that requires multiple virtual machines or a
virtual machine with advanced settings.
A virtual machine is a server in the cloud that you can control and manage. After you
create a virtual machine in Azure, you can stop and restart it whenever you need to.
When you create a virtual machine, it is automatically started for you. You’ll know when
a virtual machine is running because its status is Running. You can stop and restart a
virtual machine at any time.
Before you stop a virtual machine, you should make sure that applications running on the
virtual machine are shut down appropriately. The applications that are running on a
virtual machine are automatically shut down when the virtual machine is stopped.
To stop a virtual machine, click its name, and then click Shutdown. When you stop the
virtual machine, it shuts down just as your server in your office shuts down when you
stop it. While the virtual machine is shutting down, its status is stopping. When the
virtual machine is stopped, its status is stopped. If you set up load balancing for your
cloud service, you should consider how stopping the virtual machine will affect your
application.
When a virtual machine is running and you want to restart it, click its name, and then
click Restart. While the virtual machine is restarting, its status is Restarting. When the
virtual machine is ready for you to use, its status is Running.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
42
Step 1: Login to Microsoft Azure.
Step 2: By login in to Microsoft Azure, select "NEW at the bottom of the window.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
43
Step 3: Under "COMPUTE‖, select "Virtual Machine‖ and select "Quick Create‖.
Here, I am going to create Quick Create when we need only one virtual machine for our
solution.
You will see the following figure,
DNS: The DNS name must contain between 3 and 15 characters. The name can contain only
letters, numbers, and hyphens. The name must start with a letter and must end with a letter or a
number.
Image: Choose an Operating System image from the drop down menu; here I want to show
"Windows Server 2012 R2 Datacenter."
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
44
Size: In size, you can give a size to your virtual machine from different sizes in the dropdown
menu.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
45
User Name: User Name is mandatory for the VM, it would be used at the time of credentials.
Here I gave "pandit.
Password: Give the password for logging in to the virtual machine.
Region: I’m from India, so I am going to select "East Asia.
Hit on check mark to create a virtual machine (shown in the rectangle).
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
46
After the creation of the virtual machine, we need to connect it where we can access it remotely.
Select your virtual machine and click on "Connect‖ button.
A file with extension ".rdp‖ will be downloaded.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
47
It will display a message for the file.
Open the downloaded RDP file.
Go to the downloaded RDP file and open it. Remote Desktop is enabled by default.
Click on "Connect‖ button.
After clicking on connect button it will start the remote connection.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
48
Now, we need to complete security by entering credentials, user name and password which we
setup during creation of the virtual machine.
Click on "OK‖ button.
Click on "Yes‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
49
After entering your credentials, click on "yes" and you will be connected with your Virtual
Machine.
Here is the Dashboard.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
50
Make sure you don't forget to shut down your VM after completing your work. You can shut
down it from shutdown option as in the following:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
51
Creating a Virtual Disk Virtual Machine in Microsoft
Azure Step by Step
In the previous chapter we discussed how we can create a virtual machine from ―Quick Create‖
option in part one of the series.
Now, here I am going to create a virtual machine using the "FROM GALLERY" option. This
is for when you need a solution that requires multiple virtual machines or a virtual machine with
advanced settings.
Select COMPUTE and VIRTUAL MACHINE and select FROM GALLERY".
Azure gallery provides Windows + Linux and their entire version. Select your desired operating
system image and click next. I have selected Windows Server 2012 R2 Datacenter.
So select an image, whatever you want to show with your virtual machine.
Here, I’m going to select ―Windows Server 2012 R2 Datacenter‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
52
Give the Virtual Machine name, Tier, size, username and password.
Hit on Next (Shown in Rectangle at the bottom of the page).
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
53
Here next is configuration window; all is set by default, so click next.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
54
You can see the following window, it will ask for configuration extensions and security
extensions, so here all is set by default.
Click on to Next.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
55
Here you can see, your virtual machine is creating, which is ―gofornitin‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
56
For using the virtual machine, click on ―CONNECT‖ button.
So, here you can see portal retrieving a file with .rdp extension.
Click on file to open it.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
57
Click ―Connect‖ button.
Enter your user name and password, my user name is nitinpandit‖.
Click on ―OK‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
58
You can see I’m getting connecting to my virtual machine.
Click on ―Yes‖ button.
In the following screen shot, I’m successfully connected with my virtual machine; you can see
Dashboard of the Server Manager.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
59
Go to the ―All Servers‖ and check the server name.
Click on to ―All Servers‖, you will find ―gofornitin‖ server is available there.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
60
In the following screenshot, you can check your virtual machine’s overall details, where you can
see free space, host name and other related information of Virtual Machine.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
61
Now, I’m going to add extra space in machine and an extra disk drive on it.
For this, select your appropriate Virtual Machine and click on ―ATTACH‖ button.
It will ask to fill in the details for the empty disk.
It will take machine name (gofornitin), storage location, and file name by default.
Give the size for disk which you want to be created; here I have given it 15GB.
Click on the check button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
62
It may take some time to create a new empty virtual machine.
Here you can see, our virtual machine is creating.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
63
So now, your virtual machine is ready for use and is in running mode.
Click on ―Connect‖ button to retrieve the .rdp file.
Follow the last process, click on to downloaded .rdp file.
Now in the following screenshot, I’m connecting with the new Empty disk drive.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
64
To check there is 15 GB disk in or not, simply go to the ―File and Storage Services‖.
Click on to ―Disks‖ button and refresh the current page.
Click on to ―gofornitin‖ and you can see our 15 GB virtual disk successfully added.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
65
Let’s check it to Disk Management.
There is also 15GB virtual disk available, and status of disk is online.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
66
Make sure, click on ―Shut Down‖ button to shut your virtual machine.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
67
Use of SQL Azure in Visual Studio 2015 Step By Step
Guide
In this we have to learn all about SQL Azure, it’s very simple to use and also a powerful
tool provided by Microsoft.
Microsoft made a revolution in Cloud History by Microsoft Azure. Microsoft does a lot of
changes in Azure day by day providing best possibility for every user or developer. In Azure, we
have lot of support for database such as NoSQL, SQL Server and more. In Microsoft Azure we
can use SQL database by the following two ways:
1. Microsoft SQL Server
We can use Microsoft SQL server in Microsoft Azure Virtual Machines (Azure VM)
which is used as IAAS (Infrastructure as a Service) where we have full access by RDA
(Remote Desktop Access) on this VM where we can use any Microsoft SQL Server.
2. SQL Azure
Sql Azure in the second option to use Sql Database on cloud which behaves
like PAAS and it’s very simple or easy to access by web portal of Microsoft Azure or in
Visual Studio Azure SDK.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
68
3.
What is SQL Azure Database?
In short, SQL Azure database is simply a way to get connected in Cloud Services where we can
store our database into Cloud. Microsoft SQL Services and Microsoft SQL Data Services are
now known as Microsoft SQL Azure and SQL Azure Database. Microsoft Azure is the best way
to use PAAS (Platform as a Service) where we can host multiple databases on the same
Account.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
69
Microsoft SQL Azure has the same feature of SQL Server, i.e. high availability, scalability and
security in the core.
Built-in Automatic Backup in Microsoft Azure SQL Database
Microsoft Azure SQL Database have a best feature, it automatically creates backups of every
active database. Every hour a backup is taken and geo-replicated to enable the 1 hour recovery
point objective (RPO) for Geo-Restore. Additionally, transaction log backups are taken every 5
minutes to enable Point in Time Restore.
Backup Storage
As per MSDN:
"Backup storage is the storage associated with your automated database backups that are used
for Point in Time Restore and Geo-Restore. Azure SQL Database provides up to 200% of your
maximum provisioned database storage of backup storage at no additional cost. For example, if
you have a database in the Standard service tier with a provisioned size of 250 GB, you will be
provided with 500 GB of backup storage at no additional charge. If your database exceeds the
provided backup storage, you can choose to reduce the retention period by contacting Azure
Support or pay for the extra backup storage billed at standard Read-Access Geographically
Redundant Storage (RA-GRS) rate. For more information on RA-GRS billing, see Storage
Pricing Details."
Now let’s see how to create SQL Azure Step by Step
In your Azure Account Portal you have a link to use your Sql Databases on SQL Azure, so go to
the Link.
Login now and go to portal Link where you can see Sql Database,
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
70
If you click on this section, you’ll find all the already created databases and will get the link to
go to servers, every database exist in any physical server, but for SQL Azure we don’t need any
hardware or software to use that because it’s a PAAS on Cloud Services provided by Microsoft
Azure but still you have to create a server where you want to use your datacenter or replica of
your database. So first we want to create a server, now let’s see how to create Data Server, but
one thing you should remember, we don’t have RDA (Remote Desktop Access) permission on
this server we can use this database in our Visual Studio or in our Microsoft SQL Server
Management Studio so that we can create table and can execute our .sql script file for database
management. So finally in this article we’ll learn all the things step by step which is divided into
the following seven parts:
1. How to Create Servers in Microsoft Azure.
2. How to Configure Server for Client access by IP Addresses.
3. How to create Database on specified Server.
4. How to get the connection strings.
5. How to Use SQL Azure in Visual Studio.
6. How to Use SQL Azure Table for CRUD Operations in ADO.Net.
7. How to Use SQL Azure Table for CRUD Operations by Entity Framework.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
71
So I hope you’ll enjoy this, now it’s time to do everything by yourself, firstly login into your
Microsoft Azure Account.
Now let’s see How to Create Servers in Microsoft Azure
We need a server first to create any Sql DB to host on SQL Azure, so click on SQL Databases
link in main menu and get the entire database already created and on the top there is a link.
Servers find that link as I have shown in the following image:
After clicking on Servers, you’ll get the entire server which already running on the cloud service.
Now the next step is find the button Add to add or create a new Server as in the following image.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
72
Now fill this form to set all the necessary details for a server like Login Name, Password,
Confirm Password and region for selection of Datacenter and after filling all the information
press enter to submit all the details and then that will create a server by a logical server name. It
will be a combination of alphabetic and numeric digits which we have to use to create a
database.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
73
After submitting all the information about our server it will take a few seconds to create it.
After that now server is ready to use with a logical name but if you use this server to host your
database, you don’t have permission to use on any client IP, so we need to configure this server
for specified client IP addresses and go to the next step.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
74
Now let’s see How to Configure Server for Client access by IP Addresses-
Every server is a must be configure for every client system IP address, so let’s see, first click on
the server logical name to open the server dashboard or configuration sections. Now open the
configuration section, so click as per the following screenshot:
In this section there is an option to add details for every client where you need to login this
server. Add all IP addresses with the rule name and you want to access the same system, so there
is directly a link to add current system in the list as in the following image.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
75
Now your system and all those systems are able to get connected with this server and those IP
address is added in the list.
At last, just save the setting which have changed before leaving this section or creating new
database.
Now let’s see How to create Database on specified Server
Now the time is to create a SQL Azure Database on cloud as PAAS, so now find the + New
Button on the left side bottom in your Web portal of Azure and click that.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
76
Now go to Data Services, then SQL Databases and Quick Create or Custom Create.
If you select Quick create, you have to enter only the name of database and select the server by
the logical name of the server and click ―Create SQL Database‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
77
If you select Custom Create, you have many options to fill or select Database Name,
Subscription Type, Max Size and Server Name and then click ok.
After submitting all the details it takes few seconds to be ready for use and you can see the
progress in the Progress bar.
Now your server is ready to use from your Visual Studio or SQL Server Management Studio.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
78
Now let’s see How to get connection Strings
Now the most important part is if you want to connect this database to store your data as per
need in any Application by any framework or technologies like in php or java and more. So the
first thing which we need is Connections String to connect, so let’s see how to get the connection
string. Firstly, go to the Dashboard of the database.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
79
In the Dashboard section there is an option in quick glance Show Connection Strings and
below that there is the server name by which you have to connect in Visual Studio Server
Explorer. So click on the link of Connection Strings.
Now you will see the window of connection string where we have all maximum connection
string for every developer for every technology ADO.Net, JDBC, PHP and ODBC, so you can
use it anywhere just add password in the connection string.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
80
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
81
Now let’s see How to Use SQL Azure in Visual Studio
Now let’s see how to use SQL DB to your Visual Studio Server Explorer or may be in SQL
Server Management Studio. Open your visual studio first.
Open Server Explorer to connect SQL Azure Database.
Right click on Data Connections and add Connections, then there is a window, fill all the details
and copy the Server Name from Database Dashboard in Azure web portal and paste in this
window. Now enter the user name which was entered by you at the time of server creation like
ServerUserLoginName@ServerLogicalName and password. Select your database name
from the list and click ok or Test Connection button if you want to check your connection is
valid or not.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
82
This is the same process for Microsoft SQL Server Management Studio, the people who don’t
have Visual Studio like DBA, they can use there .sql script file over there.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
83
So let’s see with Visual Studio I connect my Azure Database server in the Server Explorer.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
84
Now if you want to add a new table right clicks on Tables and find two options Add new Table
and New Query. Select any one.
If you want to create table by query, select New Query or if I want to add by wizard, click on
Add new Table and then you get a window on your screen where you have to add columns and
add constraint if required. After writing your query or adding columns, just click on Update
Button. It will confirm and save the schema.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
85
In this window we have two options: Click Generate Script if you want to create Script file and it
will save automatically in the solution or any specified address. The second option is to Update
Database if you want to update all schemas without saving query file, it will update SQL Azure
Database with new Schema.
You can see the update of progress in the Data Tools Operations.
Now open server explorer and refresh connection, now you can see your table in Tables options.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
86
Now use this table anywhere with the connection string that we get by our Microsoft Azure
portal.
Now let’s see How to Use SQL Azure Table for CRUD Operations in ADO.NET-
If you want to connect this use your SQL Azure table in any application such as Windows Form,
WPF Application, Console Application or any web application. This is the same process so I am
using a web application project in Visual Studio. Open Visual Studio, File, New Project, then
select Web and click ASP.NET Empty Web Application.
Give a name and location and press OK.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
87
Now I have an empty Web Application Solution, so add any page to show the data or use CRUD
operations in SQL Azure Table. I just added a page default.aspx to do that.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
88
Now add a new Connection String into Connections String Sections in your web.config, copy
connection string from Azure Web portal and paste with password in Web.config like the
following image:
Use some control like GridView and other control like TextBox and Button event.
I just use a simple code of ADO.NET with SqlClient provider, on the button I run command and
if it runs successfully, I called a function where I bind the grid from my Azure SQL Client.
Code:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public void BindGrid()
{
SqlDataAdapter da = new SqlDataAdapter("Select * from Employee", ConfigurationManager
.ConnectionStrings["MyDB"].ConnectionString);
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
89
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
public int RunCommand(string Query)
{
SqlConnection con = new SqlConnection(ConfigurationManager
.ConnectionStrings["MyDB"].ConnectionString);
SqlCommand com = new SqlCommand(Query,con);
con.Open();
int res=com.ExecuteNonQuery();
con.Close();
return res;
}
protected void Button1_Click(object sender, EventArgs e)
{
string qry = "Insert into Employee Values ('" + TextBox1.Text + "','"
+ TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')";
if (RunCommand(qry) > 0)
{
BindGrid();
}
}
Here's the output
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
90
Now let’s see How to Use SQL Azure Table for CRUD Operations by Entity Framework
Database First approach
In the last point of this article we’ll learn how to use SQL Azure Table in your Application with
Entity Framework code first approach. So let’s start, just create a basic empty application as the
previous point.
In Empty Solution, firstly, add an ADO.NET Entity Data Model.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
91
Give a name to your model and click OK.
In this window, select Generate from database and click next.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
92
Now make a connection with SQL Azure Data server to ADO.NET Entity Model.
Enter all the details as per the previous point where we discussed how to connect in Server
Explorer.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
93
Click Next if the Next Button is enabled and if not, select a Radio Button, which is yes include
the sensitive data to connection string and give a name to your Entity class name. Name it
anything you want like I just gave a name MyDB. After that click on to next.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
94
Now select how many tables you want, select them and press Finish Button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
95
Now you’re Model and Entity File is ready to use.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
96
Again create a view for the same as before for performing the CRUD operations on Entity
model.
On the button click, I just created the object of my entity class and added this into entity
collection and call the SaveChange()‖ function of DbSet Class to save or update all the data to
SQL Azure Table to live data.
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestSQLAzureByEF
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Bind();
}
MyDB db = new MyDB();
private void Bind()
{
GridView1.DataSource = db.Employees.Select(p=>p).ToList();
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
97
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Employee emp = new Employee()
{
Id=Convert.ToInt32(TextBox1.Text),
Name=TextBox2.Text,
Salary=Convert.ToInt64(TextBox3.Text),
City=TextBox4.Text
};
db.Employees.Add(emp);
db.SaveChanges();
Bind(); } } }
Output:
Initial output:
After button click:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
98
After that go to Server Explorer, right click on table and show the table data.
Here's the data in table in Visual Studio.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
99
Entity Framework with Microsoft Azure SQL
To use SQL Azure with your Entity Framework, first create an SQL database first in your
Azure account.
So, let’s make an SQL database. Here are the steps,
Go to your Azure account find New, Data + Storage, and then click SQL Databases.
Fill the details and proceed to next.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
100
After creating SQL Azure database successfully, then just go to Visual Studio and open Server
Explorer. Just login with Azure Account and find the database created in Azure.
Right click on it and open it with SQL server Object Explorer so that we can create table and
manage my database from wizard.
Just enter your database authentication details which were entered by you on the Azure portal to
create a database and make sure that a client IP address has been added in to server
configurations.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
101
After connection with SQL Azure database into Server Explorer, you can see the database and all
its tables and other things but I am not having any table yet, so make a table first. For that, right
click on tables section and add a new table.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
102
Just write your create table script on the script section and at last just click on update button.
Now, you’ll get dialog where you have to click on “Update Database‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
103
Now it’s creating a table you see your query updates in Data tools options.
Just go to back on SQL Server object explorer and refresh tables. You’ll get the new table
created by your query.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
104
Now just add an ASP.NET or any other application where you want to use this database. I am
going to use an ASP.NET project to use this database with entity framework.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
105
Now just add an Entity framework.
Select any one, but I am going to use Database first with entity.
Now just make connection with Azure cloud service where your SQL Azure is hosted. For that
you can go to your portal and get connection string.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
106
Copy that and open it in text file and find server name.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
107
Copy Server name and user id,
Use them and make a successful connection.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
108
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
109
Give a name to your entity and select your tables which you want to use.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
110
Now this is your model file.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
111
Now just add an ASP Web Form to write operations.
Just use some textbox to get the test information from page and a button. On button click event
write code to add an object to an entity table as in the following code.
Test code to save object information to table.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
112
Now at last just run this application and enter some details and click on button.
After that, you’ll get the message ―Done !!‖ as I use to notify all done successfully.
Go to table and right click on table in SQL server object explorer and find View Data option.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
113
So this is the data entered by you from ASP form.
I hope you enjoyed this chapter.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
114
Introducing U-SQL- Make Big Data Processing Easy
Microsoft recently announced a new feature for Azure Services, Azure Data Lake Services for
analytics in the Microsoft Azure cloud services. It is having a large scale repository and also a
default service YARN that provide a benefit for analyzing the large scale data to a developer or
DBA and also for the data scientists. YARN is Apache Hadoop NextGen MapReduce known as
MapReduce 2.0 (MRv2) or called YARN.
Azure Data Lake has a managed way to manage all Hadoop, Spark and HBase Services. Azure
Data Lake use U-SQL. It is a language that explores the benefits of SQL and is scalable and
provides a distributed query capabilities for a developer and DBA those are currently working
with Big Data. Also, it provides you to efficiently analyze your data in the store and across
relational stores such as Microsoft SQL Azure Database.
Benefits of Data Lake-
Batch, Real-time, and interactive analytics made it easy to:
Store and analyze data of any kind and size.
Develop faster, debug and optimize smarter.
Interactively explore patterns in your data.
No learning curvesuse U-SQL, Spark, Hive, HBase, and Storm.
Managed and supported with an enterprise-grade SLA.
Dynamically scales to match your business priorities.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
115
Enterprise-grade security with Azure Active Directory.
Built on YARN, designed for the cloud.
Why use U-SQL?
U-SQL is a large scalable Language and if we see the behaviour of Big Data analytics we can
have several requirements. Here are the major requirements as per the MSDN blog.
Process any type of data From analysing BotNet attack patterns from security logs to
extracting features from images and videos for machine learning, the language needs to
enable you to work on any data.
Use custom code easily to express your complex, often proprietary business algorithms.
The example scenarios above may all require custom processing that is often not easily
expressed in standard query languages, ranging from user defined functions, to custom
input and output formats.
Scale efficiently to any size of data without you focusing on scale-out topologies,
plumbing code, or limitations of a specific distributed infrastructure.
How to use U-SQL?
If you want to see how we can use U-SQL. Go through the MSDN blog example. Let’s assume I
have downloaded my Twitter history of all my tweets, retweets, and mentions as a CSV file and
placed it into my Azure Data Lake Store. Here top 50 rows in the .csv file can be seen:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
116
In this just count the number of tweets for each of the authors in the tweet ―network‖:
@t = EXTRACT date string
, time string
, author string
, tweet string
FROM "/input/MyTwitterHistory.csv"
USING Extractors.Csv();
@res = SELECT author
, COUNT(*) AS tweetcount
FROM @t
GROUP BY author;
OUTPUT @res TO "/output/MyTwitterAnalysis.csv"
ORDER BY tweetcount DESC
USING Outputters.Csv();
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
117
Here are the following three major steps of processing data with U-SQL from the above U-SQL
script:
1. Extract data from your source. Datatypes are based on C# data types and the built-in
extractor’s library to read and schematize the CSV file is used.
2. Transform using SQL and/or custom user defined operators. A familiar SQL expression
that does a GROUP BY aggregation is used in the preceding example.
3. Output the result into a file. You can also store it into U-SQL table for further
processing.
This was just an introduction to U-SQL, for further understanding refers the msdn blog examples
to:
Add additional information about the people mentioned in the tweets.
Extend my aggregation to return how often people in my tweet network are authoring
tweets and how often they are being mentioned.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
118
Installation of Azure SDK and Blob Storage in Azure
This is about Azure SDK and Storage Account in Azure. In this article you will also learn how to
store images in Blob storage to Azure. So, let's start with the following three steps:
1. How to install Microsoft Azure SDK in Visual Studio 2013?
2. How to install Azure Explorer?
3. How to create Storage Account in the Azure Microsoft Account?
How to install Microsoft Azure SDK in Visual Studio 2013
Step 1: Open Visual Studio 2013, go to Templates, then Visual C# and click Cloud.
Step 2: Select ―Get Microsoft Azure SDK for .NET‖.
Step 3: In the name section, it will take a default name ―MicrosoftAzure1‖.
Step 4: Click OK button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
119
In the following figure, click on to Download Microsoft Azure SDK foe .NET. (Shown in
red rectangular)
After that the installation will continue as in the following:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
120
Now click ―Install‖ button for further processing:
Now in the following figure, by clicking I Acceptwe agree to the license terms for the third
party as in the following:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
121
Click OK button to close your previous applications, which are running in the background:
Now Azure SDK is installing as in the following figure:
Click on the ―Continue‖ button for further installation of SDK.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
122
So, now our SDK is ready for use in Visual Studio 2013.
Click on to the ―Finish‖ button to finish the installation of Azure SDK.
Now we are going to install Azure Explorer.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
123
Azure Explorer: Azure Explorer is a free Microsoft Azure storage tool; in this we can manage
all our Microsoft Azure blobs at a common place or one place. Azure Storage Explorer is a
useful GUI tool for inspecting and altering the data.
Step 1: For the installation of Azure Explorer we need go to its download link,
After going to above link, you will see the following window. Click DOWNLOAD AZURE
EXPLORER‖ (in red circle).
In the following figure the installation of Azure Explorer progress can be seen:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
124
After completing the installation, just go in the search box and type Azure Explorer and open it.
You need to check License Agreement, and click on ―I Accept‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
125
And finally we installed Azure Explorer successfully.
Here we need to add our account with the Azure Explorer, so right click on Azure Storage
Accounts‖ and choose ―Add Connection to Azure Storage‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
126
So in the following figure it will provide you different options to connect with the storage
account, here we need to choose Connect to Azure with a Storage Account Name and an
Access key‖.
Click on ―Next‖ button.
So, next window will ask you about ―Storage Account Name‖ and ―Storage Access Key‖.
Provide the storage account name and storage access key. You will get this information from
your Microsoft Azure Account.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
127
Just go to your Microsoft Account login where you can get the name and key from your storage
account.
For this, simply go to your Microsoft login page. Here's the screenshot:
Fill the email address and click on ―Continue:"
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
128
Fill the password detail and click on ―Sign in‖ as in the following figure:
So, for creating a storage account, you need to go to New, Data + Storage, then Storage
account.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
129
After clicking on the ―Storage account‖, you will see the following window.
Here, in the Select a deployment model(in the left side of the window), you need to select
Classic‖ from the dropdown items.
Click on the ―Create‖ button. After clicking, you will see the right portion of the window.
In the Storage option, you need to give your storage account name; here I am giving the storage
name ―nitindemostorage‖, you can give the storage name as your wish.
Finally, click on the ―Create‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
130
After clicking the ―Create‖ button, you will get the storage account of your name.
And now, simply go to the Storage‖ and then ―nitindemostorage‖, which is my storage
account name and then go to the MANAGE ACCESS KEYS‖, for the Storage Account Name
and Primary Access Key.
Now you will need to copy the Storage Account Name and Primary Access Key as in the
following screenshot:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
131
Now, copy the storage account name and primary access key, and paste over here as in the
following screenshot. After that, click on the ―Test Connection‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
132
Connection is successful, so click on OK button.
\
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
133
Once again click on OK button to proceed.
Finally, we have created our storage account with Azure Explorer. You can see in the red
rectangle that is ―nitindemostorage‖.
Now we have to create blob storage for accessing services in Azure Storage, so right click on
storage account and click on ―New Blob Container‖.
Now in the name section, give the name to blob container as you wish, but you can’t use
uppercase letters while creating blob storage.
Here I have given ―mysample‖ name to my blob container.
Now, if you are an owner of your account, choose the ―Private- Access to account owner only
option from the dropdown list.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
134
Click on OK button.
This will create a blob container.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
135
In the following figure, ―mysample‖ blob container successfully created. (In the red area).
Here is another blob with name $logs‖, so don’t worry, since it is created by Azure Storage by
default. We will focus on ―mysample‖ named blob storage.
So, we have successfully created our ―mysample‖ named blob storage.
Now, I am going to store images in my Azure explorer. You can directly paste the image or any
file inside the ―mysample‖ blob.
Here, I am going to paste two images: ―Nitin1‖ and ―Nitin2‖ as in the following figure:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
136
Right click on your favorite image and click "Copy path".
Now you can paste the path in our browser directly.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
137
It gives an error because we need to change the access level of our blob container.
So, right click and choose ―Permissions‖.
Here you can choose the ―Blob - Public read access for Blobs‖ option.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
138
Click on OK button.
Now, again right click on the image and copy full path of the image.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
139
Paste the copied path in the web browser.
So, finally we will get our image.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
140
Upload Image to Azure Blob Storage in ASP.NET
In this chapter, I’ll tell you the uses of Azure Blob Storage and how to use blob storage in
ASP.NET Project. Firstly, create an ASP.NET project in Visual Studio, so open Visual Studio
and follow these steps:
Step 1: Open Visual Studio and select the File menu, New, then Project.
Step 2: Go to Templates, Visual C#, and then ASP.NET Web Application.
Step 3: In the name section, give a name to project. Here I have given the name ―Azure Blob
Storage‖.
Step 4: Click on ―OK‖ button to create a project.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
141
In the following figure, choose "Empty" project.
Click ―OK‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
142
We can see, in our project, there are three types of categories items, properties, References, and
Web.config.
Now we need to install the Microsoft Azure Storage package for using Azure Storage Access
and all supported classes.
Now go to the ―TOOLS‖ menu, select ―NuGet Package Manager‖, and select ―Manage
NuGet Packages for Solution‖.
In the following figure, go to the search box and type Azure‖. After this you will find the
Microsoft Azure Storage‖, click on ―Install‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
143
Now in the following figure, you can see the installation of the ―Microsoft Azure Storage‖.
Here in the following window, you will be asked to install Microsoft Azure Storage for the
current project, so here we want to install the packages for current project.
Simply click on ―OK‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
144
The installing process is going on.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
145
Here in the following figure, click on ―I Accept‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
146
Now we successfully installed the ―Microsoft Azure Storage‖ (in the small red circle).
Click on ―Close‖ button.
Here we also need to install the Azure storage client packages. So go to the search box, type the
Azure Storage Client‖. After typing, click on ―Install‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
147
We successfully installed the ―Azure Storage Client‖ package also.
Click on ―Close‖ button.
Go to your project, here my project name is ―Azure Blob Storage‖.
Right click on to project, select ―Add‖ and select ―New Item‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
148
Here, I am going to create a Web Form, so select a Web Form in the list and give the name to
your Web Form; here I gave the name MyFormto my Web Form, you can give any name to
your Web Form.
Click on ―Add‖ button.
After creating a form, you can see your project, where form will be created successfully with the
name ―MyForm.aspx
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
149
In the ―MyForm.aspx.cs‖ page, let me do some changes.
In the ―MyForm.aspx.cs‖ page, I have added all namespaces which is minimum required.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
150
Now we need accountnameand accesskeyto get connected with Azure Account, so go to
Azure portal.
Go to Storage option where I have created a Storage Account with the name
nitindemostorage‖ in my previous chapter.
Here, after clicking nitindemostroagenamed Storage Account, go to bottom of the window
and select ―Manage Access Keys‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
151
You will find the following window, copy the ―Storage Account Name‖ and ―Primary Access
Key‖ from here,
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
152
Go back in your project and make some changes over there.
In the ―accountname‖ and ―accesskey‖ paste the copied item over there.
And make some other code over there for getting connections with Azure storage account with
CloudStorageAccount‖ class object and at last convert your image into stream upload as in the
following image:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
153
Code
1. string accountname = "nitindemostorage";
2.
3. string
accesskey = "8TeQ8Li/b5U75aSPeTUvmg6w6s6emWSC5KKksNR7hRmSS5GmxzY8wxXFxJtNlwnwSbvlWGAX07
dnR0W8jrb+Og==";
4.
5. try
6.
7. {
8.
9. StorageCredentialscreden = newStorageCredentials(accountname, accesskey);
10.
11. CloudStorageAccountacc = newCloudStorageAccount(creden, useHttps: true);
12.
13. CloudBlobClient client = acc.CreateCloudBlobClient();
14.
15. CloudBlobContainercont = client.GetContainerReference("mysample");
16.
17. cont.CreateIfNotExists();
18.
19. cont.SetPermissions(newBlobContainerPermissions
20.
21. {
22.
23. PublicAccess = BlobContainerPublicAccessType.Blob
24.
25. });
26.
27. CloudBlockBlobcblob = cont.GetBlockBlobReference("Sampleblob.jpg");
28.
29. using(Stream file = System.IO.File.OpenRead(@ "D:\amit\Nitin sir\Nitinpandit.jpg"))
30.
31. {
32.
33. cblob.UploadFromStream(file);
34.
35. }
36.
37. } catch (Exception ex)
38.
39. {
40.
41. }
One more thing, here we need to create a Web App Profile for publishing our app into the cloud.
Simply go to “WEB APPS” in your Azure Portal and click on ―New‖ to add a new Web App.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
154
After clicking on ―New‖, you will see the following window.
Go to “COMPUTE” option, select “WEB APP”, and select “QUICK CREATE".
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
155
In the URL section, you can write your Web App name. Here I have given
nitinblobstoragedemo‖, if the entered name is correct it will create a check sign (in blue
color).
In the “APP SERVICE PLAN” click on ―Default1 (East Asia, Shared)‖.
Click on “CREATE WEB APP”.
We have created our Web App successfully. After doing this, we can see our created Web APP
(―nitinblobstoragedemo‖).
And it is in running mode.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
156
We make some code in the Web Config file also.
Here in the <add value=‖MyForm.aspx‖/>, ―My Form‖ is my Web form.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
157
In the ―MyForm.aspx‖ you can give any message for output. Whenever you build your project,
the typed message will be your output.
So, here in the div section I have given a message, “Creating Blob---By Nitin Pandit”.
Now we need to publish our created project in the cloud. It will also save in the Azure Explorer.
You can publish your project by two ways. Here, I am going to discuss both ways. Firstly, right
click on your project. Here my project is ―AzureBlobStorage‖, so select the ―Publish‖ option.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
158
In the following figure, you can see nitinblobstoragedemois my Web App name, which we
have created in the starting. Select your Web APP profile and click "Publish‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
159
After clicking on ―publish‖ button the project would publish on cloud as well as in your Azure
Explorer. The second way to publish your project is to go for cloud. You can download your
profile from the Azure account in your Web Apps. For this click on your Web App profile, in
the Publish your app section, click on ―Download the publish profile‖.
After clicking, your profile will be downloaded in your system.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
160
After this, now we can see the status of our publishing. So my nitinblobstoragedemo app
service published successfully.
Now build your project and get your output.
Now run the application, press F5.
So, finally we got our output.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
161
We can see the changes of this in our Azure Explorer.
Now refresh the Azure Explorer to check your image.
“Sampleblob.jpg” is my image which is generated by programming,
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
162
Right clicks on image and open it and if you want to get that images by code on your view, you
can see in my next article. So, wait for that and stay tuned.
It can take few moments to download the image is in progress.
Here's the final output and you can see my image:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
163
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
164
Demo Project Azure Blob Storage with ASP.NET
MVC 5.0
This is my third one chapter on Azure Blob Storage in this I just tried to use blob storage in
ASP.NET MVC application with helping of ASP.NET Web role it’s a kind of demo project
Application to store Images file to blob storage and edit update them and also get the list.
So, let’s start now firstly you need Azure blob storage which I already explained in the previous
chapter. After that open your Visual Studio and create Azure Cloud Service with ASP.NET Web
roles as in the following steps:
Step 1: Open Visual Studio, Templates, Visual C#, and then Cloud.
Step 2: Select ―Azure Cloud Service‖.
Step 3: In the name section, I have given name to my project ―AzureCloudDemoMVC‖.
Step 4: Click on OK button.
After clicking on ―OK‖ button choose ―ASP.NET Web Role‖.
Web Role: Web Role is a font and application or we can say a user interface.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
165
Click on (>) button (In the red circle).
Hit on ―OK‖ button.
Click on ―MVC‖ because I am going to create an MVC Application.
Click on ―OK‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
166
In the following figure, I have a Window Azure Cloud Project with name
AzureCloudDemoMVC‖; you can see In Solution Explorer.
And ―WebRole1‖ is a regular MVC project in my Solution Explorer.
You can notice here, a new file with the name WebRole.cs‖. This file contains entry point
settings of MVC application. You need this file while MVC running in Cloud Services. The
information written in this file executes before the execution of ―Global.asax‖ file.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
167
The other project is AzureCloudDemoMVC‖, this is actually a cloud project. Inside this project,
you will find the ―Roles‖ folder and in the ―Roles‖ folder you will find ―WebRole1‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
168
Now we need to connect with Azure, so go in the ―Server Explorer‖ and right click on Azure
(Reenter your credentials)" and go to ―Connect to Microsoft Azure Subscription‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
169
Fill the Login detail and click on ―Sign in‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
170
So, we have successfully connected with the Azure Account, and right click on the ―Storage
and select ―Attach External Storage‖.
In the ―Account Name‖ section and in ―Account key‖ section provide name and key from the
Azure Storage, so go to your Azure Storage and copy name and key from account.
In the Cloud, go to STORAGEand click on your storage account, here my storage account is
nitindemostorage‖, and click on ―MANAGE ACCESS KEYS‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
171
Copy the ―Storage Account Name‖ and "Primary Access Key‖ from here and paste it over
there.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
172
After pasting name and key click on ―OK‖ button.
We got my storage account nitindemostorageand in this we have a ―Blob‖ and ―mysample‖
named Blob Container.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
173
Now go in to project which is ―WebRole1‖, right click on project, select ―Add‖ and then select a
Class‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
174
Give a name to your class, whatever you want; here I have given it ―BlobServices‖.
Then click on ―Add‖ button.
Now, let’s go ahead and add a method that will get cloud blob container information. Right click
near ―CloudBlobContainer‖ and import a namespace, select ―Resolve‖ and click on using
Microsoft.WindowsAzure.Storage.Blob‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
175
We need to write some important namespaces, so go ahead and type the namespaces.
using System.IO;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;
using System.Configuration;
The method will be ―GetCloudBlobContainer‖ as in the following screenshot:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
176
Here, I have creating a string to give the string of my storage account. You can copy the
connection string from your Azure Explorer. So go in Azure Explorer and copy the string from
Azure Storage.
public CloudBlobContainer GetCloudBlobContainer()
{
string connString = "DefaultEndpointsProtocol=https;AccountName=nitindemostorage;AccountK
ey=jH/5WgOZzQ7wSQv6BSZ2fbJW3sSEAzinmOUhorUWFpfJSUKPUopAD5uDIP3lvJujmnw1hBoD2RFS
zHlPYt2nLw==;";
string destContainer = "mysample";
// Get a reference to the storage account
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(destContainer);
if (blobContainer.CreateIfNotExists())
{
blobContainer.SetPermissions(newBlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
}
return blobContainer;
}
And ―mysample‖ is my Blob Container in the Cloud.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
177
Now go into Azure Explorer and copy the connection string from storage account.
Go into search box and type Azure Explorer.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
178
Now right click on Azure Storage account that is nitindemostorage and select Copy
Connection string‖ to copy it. Paste the connection string in the ―string connString‖.
Now I am going to use here,
CloudBlobClient:- To create the cloud blob service client.
CloudBlobContainer: To check if there is any Blob Container of ―mysample‖ named in the
storage account.
And here I have used if condition to check if there is any Blob Container, so just go in to the
storage account and create a Blob Storage of namemysample‖.
And finally it will return Blob Container. Container is nothing but just a folder in the cloud.
If there is a blob container then it will use existing.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
179
Now go ahead and use this setup in my Controller.
Go to ―HomeController.cs‖.
I’m going to add a new Action method inside this ―HomeController‖.
Blob Services class is nothing but a ―BlobServices.cs‖ class which is used in the previous
―BlobServices.cs‖ class.
We have created a ―Upload()‖ method with ActionResult Return type.
Inside the ―Upload()‖, create ―CloudBlobContainer‖ and list name all the blobs and
―_blobServices‖ is object of my class.
Inside this Action Result, list available blobs, Uri in the container and return it to the view page
to display all the blobs.
Means if you upload the blobs which is image it will list all the images and look through the
images and return the Uri of the images to the view page.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
180
BlobServices _blobServices = newBlobServices();
publicActionResult Upload()
{
CloudBlobContainer blobContainer = _blobServices.GetCloudBlobContainer();
List < string > blobs = newList < string > ();
foreach(var blobItem in blobContainer.ListBlobs())
{
blobs.Add(blobItem.Uri.ToString());
}
return View(blobs);
}
Now, I’m going to implement its post version means user will be able to upload his Blob also,
publicActionResult Upload(FormCollectionimage)- It will collect the posted file image
information, and upload it to the Blob Container and at the end this will redirect to the same view
to make recently image visible to user.
[HttpPost]
public ActionResult Upload(FormCollection image)
{
foreach(string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item] asHttpPostedFileBase;
if (file.ContentLength == 0)
continue;
if (file.ContentLength > 0)
{
CloudBlobContainer blobContainer = _blobServices.GetCloudBlobContainer();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.FileName);
blob.UploadFromStream(file.InputStream);
}
}
return RedirectToAction("Upload");
}
When we write ―CloudBlobContainer‖ we need to resolve it by right clicking near
‖CloudBlobContainer‖. So simply go to Resolve and select
―usingMicrosft.WindowsAzure.Storage.Blob;‖
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
181
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
182
Now we need to create one more method to delete the blobs, so let’s go ahead and create a
method for deleting the blob.
You can see, this method will accept name as a parameter and return stream message which is
String and will return ―File Successfully Deleted‖.
―Name‖ is nothing but ―Uri‖ of the images, Uri means the complete address of the image.
―uri.LocalPath‖ is a path that contains the actual description of the images.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
183
[HttpPost]
public string DeleteImg(string Name)
{
Uri uri = newUri(Name);
string filename = System.IO.Path.GetFileName(uri.LocalPath);
CloudBlobContainer blobContainer = _blobServices.GetCloudBlobContainer();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename);
blob.Delete();
return "File Successfully Deleted";
}
Now we need to add a view for the design that design will be shown on our browser, means that
we want to show in the browser.
Simply go near to for each statement and click right over there and go to ―Add View‖, for
adding a view.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
184
In the following image, in the view name section, give any name to view whatever you want,
here I have given it ―Upload‖.
Click on ―Add‖ button to add a view.
After adding a view, in the paragraph tag <P> </P>, I am creating a form section and using
Razor syntax here.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
185
Upload: My ActionResult
Home: My HomeController, and form method will be Post,
@using(Html.BeginForm("Upload", "Home", FormMethod.Post, new
{
enctype = "multipart/form-data"
}))
{
enctype = "multipart/form-data"
} - This code defines;
//we can select the multi part of the image.
Now in the < div > section, I’ m going to print a message that is Upload Image”.
Now I m going to create some Html tags. < inputtype = "file"
name = "avatar" / > -This will allows us to upload an image. < inputtype = "submit"
value = "upload" / > -This is upload a button.
I have created a table also for image and delete, image for Images and Delete for the Delete. So
create a table class and in the ―style‖ you can give the widths according to your choice. I have
given 50% to image and 25% to delete.
Here, I’m going to create also a script to delete button. So create a function ―deleteimage(item)‖
and in the URL section give the Url of DeleteImage.
@ {
ViewBag.Title = "Upload";
} < h2 > Upload The Image < /h2> < p >
@using(Html.BeginForm("Upload", "Home", FormMethod.Post, new
{
enctype = "multipart/form-data"
})) { < div > Upload Image < /div> < inputtype = "file"
name = "avatar" / >
< inputtype = "submit"
value = "upload" / >
} < /p> < table >
< tr > < td >
< tableclass = "table"
style = "width:200px; " >
< tr >
< tdstyle = "width:50%" > Image < /td> < tdstyle = "width:25%" > Delete < /td> < /tr>
@foreach(var item in Model) { < tr >
< td > < imgsrc = "@item"
alt = "image here is"
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
186
width = "100"
height = "100" / > < /td> < td > < inputtype = "button"
id = "@item"
onclick = "deleteImage('@item');"
value = "Delete" / > < /td> < /tr>
} < /table></td > < tdstyle = "width:100px" > < /td> < /table> < script >
function deleteImage(item) {
var url = "/Home/DeleteImg";
$.post(url,
{
Name: item
}, function(data)
{
window.location.href = "/Home/Upload";
});
} < /script>
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
187
Finally, we successfully created a Delete button to delete the image and ―upload‖ button to
upload an image. Now let’s check our output, Build your project by Ctrl+Shift+B and after
successful build you can run it by pressing F5 key.
Finally, I got my Output. Click on Choose file button and click on upload button.
And choose your favorite image which you want to upload in the cloud as well as in the Azure
Explorer and to show it on the browser.
I have successfully uploaded my image, now go ahead and check on its changes on cloud and as
well as Azure Explorer.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
188
Sign In Azure Portal and go into Storage Account and select the Blob Container.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
189
Go to ―STORAGE‖, and select ―nitindemostroage‖.
In the ―nitindemostorage‖, select the ―CONTAINERS‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
190
After clicking on the CONTAINERS‖, you will see your Blob Container; my Blob Container
is ―mysample‖.
Click on ―mysample‖.
Finally, we can see the changes in my Cloud account, I got my output simply. We can see there
ARE three images which can be seen in the browser as well.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
191
Now go in to the Azure Explorer to see the above changes.
In the following figure, we can check our output as well. Here also three images with the same
name means all changes on the browser will be made on cloud as well as Azure Explorer.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
192
Now we can check the same output in Server Explorer as well in Visual Studio.
In the Server Explorer, select your storage accounts, my account is ―nitindemostorage‖, go in
Blobs and then ―mysample‖ Container.
Double click on ―mysample‖ container to see the changes.
Now here also you can see the same output as well.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
193
In the ―Upload.cshtml‖, now we have to create an edit button to edit an image.
<td style="width:25%">
Edit</td>
<td>
<input type="button" id="@item" onclick="EditImage('@item');" value="Edit" />
</td>
This Html Code will create an ―Edit‖ button to the browser and just a div to load a partial view
for Editing an image by jQuery.
<td>
<div id="ForEdit">
</div>
</td>
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
194
And write a jQuery function to call on that edit button click for requesting that partial
view to edit a record.
function EditImage(item)
{
$("#ForEdit").load("/Home/Edit?Name=" + item, function()
{
}).css
({
"border-color": "red",
"border-width": "5px",
"border-style": "solid"
});
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
195
You can edit your image in the browser as well, now go for code,
Here we created a new Action Edit with ActionResult Return type for both type of request
[httpget] and also for [httppost]. First one only return a partial view with a string type of Model
binder object which contain the image uri which I need to edit and second Action with [httppost]
will use when a user will submit the request to server for image editing.
Code:
//First Action for [httpGet] request:
[HttpGet]
publicActionResult Edit(string Name)
{
return PartialView("Edit", Name);
}
//Second Action for [HttpPost] request:
[HttpPost]
publicActionResult Edit(FormCollection images)
{
try {
Uri uri = newUri(images["ImageName"]);
string filename = System.IO.Path.GetFileName(uri.LocalPath);
CloudBlobContainer blobContainer = _blobServices.GetCloudBlobContainer();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename);
blob.Delete();
foreach(string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item] asHttpPostedFileBase;
if (file.ContentLength == 0)
continue;
if (file.ContentLength > 0)
{
string nn = file.FileName;
CloudBlobContainer blobContainer1 = _blobServices.GetCloudBlobContainer();
string[] arr = images["ImageName"].Split('/');
CloudBlockBlob blob1 = blobContainer1.GetBlockBlobReference(arr[arr.Length - 1]);
blob1.UploadFromStream(file.InputStream);
}
}
return RedirectToAction("Upload");
} catch (Exception)
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
196
{
return PartialView("Edit", images["ImageName"]);
}
}
Also changes can be seen in the following image:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
197
Now go in ―Views‖ folder and add another view.
Here, I gave ―Edit‖ name to my view. I’m adding a ―Partial View‖.
Click on ―Add‖ button to add a partial view.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
198
And now just write that minimum code in which I have to use the previous image and 1
fileupload control to upload new image and a submit button and a hidden control to store the
previous image id/URI to change that with new image.
Code
@model System.String < p >
@using(Html.BeginForm("Edit", "Home", FormMethod.Post, new
{
enctype = "multipart/form-data"
})) { < div > Edit Image: < imgsrc = @Model alt = "image here is"
width = "100"
height = "100" / > < /div> < inputtype = "hidden"
value = @Model name = "ImageName" / >
< inputtype = "file"
name = "avatar" / >
< inputtype = "submit"
value = "upload" / >
} < /p>
Can also show as in the following image:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
199
Now, finally we can edit our image; click on your favorite image which you want to change.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
200
Here, I’m going to edit first image.
Click on ―Edit‖ button.
After Clicking on ―Edit‖ button, you will see that image will open near ―Delete‖ button.
Click on ―Choose file‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
201
Select your favorite image what you want to replace from old one.
Click on ―Open‖ button to open that image.
After clicking on ―Open‖ button, you will see the selected image name (Nitinpandit.jpg).
Click ―Upload‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
202
Finally, I got my output. That image successfully edits and updated, as well as in my cloud,
Azure Explorer, and in Server Explorer (in Visual Studio).
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
203
To check our changes, just go in to Azure Explorer and right click on the top image and select
Open‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
204
Below is my editable image and final output.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
205
Getting Started With Azure DocumentDB
What is DocumentDB?
DocumentDB is a fully managed, highly scalable, query-able, truly schema-free JSON document
database, delivered as a service for modern applications.
Azure DocumentDB is a NoSQL document database service designed from the ground up to
natively support JSON and JavaScript directly inside the database engine.
Why
Produce and consume data at a staggering rate while requiring near instantaneous
response times to much user expectations.
Increasingly complex, compensating for the lack of query and transactional processing in
today’s NOSQL database system.
Experience unpredictable and explosive growth.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
206
What is a database account?
A database account holds a set of databases, each database can containing multiple collections so
every collection can contain stored procedure, triggers and related documents, JSONfiles.
Azure DocumentDB is a NoSQL document database service, support JSON and JavaScript
directly inside the database engine.
DocumentDB is designed for modern mobile and web applications. Azure DocumentDB
supports querying using a SQL language.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
207
To use Azure DocumentDB, you must have a DocumentDB account, a database, a collection,
and documents. So for this, simply go to Microsoft Azure login page and login with your
account.
After the ―Sign in‖, go to ―Homeand click on to ―Portal‖. After clicking on the portal you will
see the following window. For DocumentDB you have to do:
Step 1: Click on ―New‖ button.
Step 2: Scroll down and select ―Data+Storage‖.
Step 3: Again scroll down and you will see ―Azure DocumentDB‖, Click on it.
After clicking on the Azure DocumentDB you will automatic jump on the DocumentDB
account. This will ask to you for the ―ID‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
208
Give the IDname here, Id name is simply your ―DocumentDB account‖ name what you want
to use for further operations. Here I gave it nitindocdb‖, you can give any name here what you
want. Make sure ID name cannot contain / \ #? or space between the words.
When your name is validated, this will mark a green check.
In the Resource Group you can choose the Default- SQL-EastAsia‖, this is nothing but
your location where you are going to use this account.
In the ―Location‖ section you can choose your location.
After this, hit on the ―Create‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
209
Here in the following figure, I have successfully created my DocumentDB account with name
nitindocdb".
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
210
Here you can see different types of operations like settings, Add Database, Document Explorer,
Query Explorer, Script Explorer, Import Data, Move and Delete Account.
Click on key button (shown in middle red rectangle), you can see in the right side of the window
it holds ―URI‖ and ―Keys‖. URI and Keys are the particular information of the DocumentDB
account.
Now, I’m going to create a database.
After clicking on my account name, which I created ―nitindocdb‖, we will redirect on the next
database window.
Click on ―Add Database‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
211
After clicking on the Add Database this will redirect in to new window, and again ask for
ID‖, here IDis your database name, so in the IDsection give the database name which is
you want to use, here I gave ―docdatabase‖ name to my database.
Hit on ―OK‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
212
Now you can see I have created a database with name ―docdatabase‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
213
Now we need to add Collection.
Collections are the transaction boundary for stored procedures and triggers, and the entry point to
queries and CRUD operations.
Click on the docdatabasewhich I have created just and it will redirect you to new window,
here you can see the ―Add Collection‖ option.
Hit on ―Add Collection‖.
In the next window it will again ask for the ID‖, here IDis your collection ID, give the name
in ―ID‖ section, here I gave ―Record‖ name to my collection.
Click on ―OK‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
214
I have created a collection, click on it.
Click on the ―Record‖ collection and you can find ―Add Documents‖ in the next window.
In the next window you can add the records and documents in the collection section. We can add
any record or documents in the collection section.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
215
It will redirect to next page, here you can upload 100 JSON documents, but make sure document
must be less than 512kb. To upload JSON documents click on the red rectangle.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
216
Here I have selected two JSON files, animal.json and info.json with extension of json.
Click on ―Upload‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
217
So, here I had successfully uploaded two files, you can see the result is succeeded.
Now, go to the ―Document Explorer‖ to check your uploaded files. Click on ―Document
Explorer‖ it will redirect you to the document explorer window.
You can see, there are two json files ID, it creates the file in the format of ―ID‖ by default.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
218
View, create, and edit documents with Document Explorer-
Document Explorer allows you to easily create, edit and delete documents. I’ m going to edit my
document, click on any one to edit.
I’m going to edit top Id, click on it, you will see the code of the JSON, scroll down and in the
ID‖ section give the name to ―ID‖ whatever you want. Here I gave ―Nitin‖, and then click on to
Save‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
219
After clicking on Savebutton, you can see in the left side of the window, the Id name will be
changed.
You can edit everything in the document section whatever you want, after edit the document,
click on save button it will be changed.
Now, I’m going to create a document.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
220
To create a document, simply click the CreateDocument command and a minimal JSON
snippet is provided
.
If you do not provide an "id" property, then Document Explorer automatically adds an id
property and generates a GUID as the id value.
Simply type the JSON content of the document you wish to create and click on Save
command, to commit your document.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
221
The changes can be seen in the left side of the window, with ―Nitin Pandit‖ ID.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
222
By the Delete command, you can delete the existing record, let’s do it, I have record with default
ID, click on Delete command to delete this document.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
223
Click on ―Yes‖ button.
The document will be deleted.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
224
So, I had successfully deleted previous record and now only two collections are remaining.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
225
Querying In the Azure DocumentDB
As you easily understood DocumentDB through the previous chapter, now the question is:
Question: Can we perform the querying in DocumentDB also?
So, the answer is yes, we can perform edit, delete, and create querying operations in the
DocumentDB.
So let’s do it step by step as in the following:
Go to Microsoft Azure Sign In window and fill the username and password to log in your
account.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
226
In the Azure portal, scroll down and go to the Browse option and select DocumentDB
Accounts‖.
Note: I already created DocumentDB Account in the previous article.
You will see your DocumentDB Account in the next window, if there is no account simply go to
the ―Add‖ option and create your account.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
227
Here I have my account ―nitindocdb,‖ I’m going to click on it, clicking it will redirect to next
window. In this window we can find our database, if it is there, scroll down and click on your
database, I have created ―docdatabase‖ named database. If there you don’t have any database, go
to the ―Add Database‖ option in the menu.
After clicking on the database you will be redirected to the ―Document Explorer‖ window, where
you will find your created database and collations.
I need to create documents, so click ―Create Document‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
228
In the next figure, you can see there is a JSON type script where we need to enter a unique id
value; it could be your name or your friend’s name.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
229
Here, I gave the name ―Nitin‖ in the ID section.
Click ―Save‖ button to save the script.
After clicking on the ―Save‖ button, it gives you a particular id. (See in the Document Explorer).
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
230
I’m going to write some JSON in the same document, you can write any type of JSON script in
that document.
After completing your script, click on the ―Save‖ button to save your script.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
231
Script will be saved; you can call your script through the query, so let’s go for querying.
Go to backward in the menus and select ―Query Explorer‖.
In the Query Explorer window you will see a short command with
your Database and Collections.
Here c is an object. We can call our JSON property through the c object, here c object is not
mandatory for you, you can take any type of object like (a,b,f…..etc).
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
232
In the next figure, I am adding some more commands and calling my JSON property through the
c object.
After completing the command process, hit on the Run query‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
233
I have successfully obtained my result that is JSON script, in which I have defined in the
document explorer. So we can edit, delete, and create our script according to use, and can call the
script through the query.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
234
Let’s understand the menus in the DocumentDB Account.
Settings:
In the DocumentDB the first level would be Settings, it contains the whole DocumentDB
setting information like keys of the DocumentDB account, where you can find the‖URI‖ and
―Keys‖ of the account. In the properties, you can find the status and location of your account.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
235
Add Database:
In this you can create a particular database, with the unique id, where you can add collections.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
236
Document Explorer:
Document Explorer is a place where you can see your databases and collections in the dropdown
menu. In this, you can also see your list of documents, and also it means means your created and
uploaded documents in the JSON extension.
Let’s focus on Document Explorer menu-
Create Document:
In this, you can create JSON scripts; after saving the JSON scripts, it will save as a ID and
known as document.
Add Document: You can upload 100 JSON documents.
Refresh: You can refresh the Document Explorer.
Settings: In the settings, you can select the items per page like custom or unlimited.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
237
Query Explorer:
In the Query Explorer, you can choose the database and collections according to use, which
means in which database and collection you want to perform Querying.
You can also Load the Query from the outsource collection, and perform the operations on that
selected script.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
238
Script Explorer:
It is one of the important menu items of the Document Explorer. In Script Explorer you can
create stored procedures and triggers as well as user defined functions. And if you save any
script then it gets saved as a script.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
239
Import Data: You can import data in to DocumentDB also,
You can import JSON file data to DocumentDB.
You can import CSV file data to DocumentDB.
You can import SQL Server data to DocumentDB.
You can import MongoDB data to DocumentDB.
You can import data from Azure Table storage to DocumentDB.
You can import data from Amazon DynamoDB to DocumentDB.
You can import data from HBase to DocumentDB.
You can migrate data between DocumentDB collections.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
240
Move: You can move to resources.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
241
Delete Account:
You can delete your accounts, by simply selecting your account which you want to delete and
click on ―Delete Account‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
242
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
243
Manage Azure Mobile Services
What is Azure Mobile Services?
Azure Mobile Services is one of the Microsoft Azure features; Microsoft Azure provides many
features like Virtual Machine, Cloud Services, App Services, SQL Databases, etc. Window
Azure mobile services client libraries are available in many platforms, and can be used on IOS,
Android, Windows Phone, and Windows Store Apps.
Azure Mobile Services has many advantages:
You can manage tables for storing data of the apps easily.
Easy to connect scalable cloud backend to your mobile app.
Push Notifications
You can easily store structured data in the cloud.
Storing Data in the Cloud
Storing Data in the Cloud with Microsoft Azure Mobile Services is very easy for clients.
Whenever we are going to create Mobile Service, it automatically takes the SQL Database inside
Microsoft Azure.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
244
Firstly, I need to create server and database for our Mobile Service, where I can save my
information of the application.
For creating SQL Database in Azure, first of all I need to create a server for the database, where I
can manage multiple databases.
For this, follow the simple steps:
Step 1: Login to Microsoft Azure.
Step 2: In ―All Items‖ panel, go to the ―SQL Databases‖ and click on it.
Step 3: Create a server, select ―SERVERS‖.
Step 4: Click on ―Add‖ button at the bottom of the page.
You can see there are many servers in the following figure, but I’m going to create a new one.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
245
Step 5: Give the login name, what you want to like, I have given Nitinfor the SQL login used
to authenticate to the server.
Step 6: Create password for the server, and confirm password.
Step 7: Select appropriate ―Region‖.
Step 8: Click on check button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
246
You can see it is going to create a new server.
When it is going to create it will ask to enable SQL auditing firewall settings for the current
server.
Click on ―Enable Now‖ to enable the settings. (In rectangle, check mark)
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
247
In the next page, I need to enable default auditing settings, like storage account, table name, and
storage access key.
Click on ―Save‖ button to save the current settings.
After clicking on save button, it will give message for saving stream.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
248
So, our server has been created successfully, and is ready for use.
Now, I need to create a database, go back to the servers and select current server.
Click on ―New‖ button to create a new database.
Go to Data Services, SQL Database, then Custom Create.
I going to choose ―Custom Create‖, where we can decide whether to use an existing server or
create a new one.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
249
Now, fill all the details for creation of the database.
Give the name to database, which you want to create; I have given ―StudentDemo‖, select
Service Tier‖ and then the server name.
Click on check button to create database.
Note: If you are going to create a server and want to give the name to server like ―Nitin‖, so it
will create a default name to server, likeNitin‖ to ―plq4stu4ut‖.
You can see, the creation of database is on processing.
Now, I have successfully created database ―StudentDemo‖ and named with server ―Nitin‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
250
Now our, Server and Database is ready for use. Now, I’m going to create a Mobile Service.
Click on Mobile Services‖, we still don’t have any Mobile Services. Click on to New for
creating a Mobile Service.
Click on to COMPUTE, MOBILE SERVICE, and then CREATE.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
251
Give the Name for your Mobile Service and for the database. Here, I’m going to select existing
database, which I just created.
Select your ―Region‖, where you want to create your Service.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
252
Backend provides two options for the use, .NET and JavaScript; these options generate APIs for
performing operations on the backend table data.
Click on next arrow.
Specify the Database Settings; select your database, Login Name, and Password.
Click on to check button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
253
After clicking on check, you will find Mobile Service will be created as in the following
screenshot and is ready for use.
Click on your Mobile Service, you will find more things for the service.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
254
This page provides information about the platform for consuming the Mobile service. We have
options like iOS, Windows, Android, HTML/JavaScript, etc. The portal provides a ready App for
guiding us through the use of Mobile Services. On the top of the page, we have options to create
Logs, Identity, Scheduler, PUSH configuration, the Host configuration, etc.
So, under the Choose A Platform‖, expand the Create A New Windows or Windows Phone
App‖.
This displays the three easy steps to create a Windows Store app connected to your mobile
service.
1. Install Visual Studio Professional 2013, if you don’t have followed the first point and
install on your local computer.
2. Select the language for your App (I have selected C#).
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
255
3. Click on ―Download‖.
After clicking on to ―Download‖, it will download a project, click on to open it.
Browse to the location where you saved the compressed project files, expand the files on your
computer, and open the solution file in Visual Studio.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
256
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
257
Open the Visual Studio file.
Right click on to your service project and set it ―Set as Startup Project‖.
Save your project and press F5 to run your project.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
258
I’ve successfully run my application; follow the rectangle and click over it.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
259
You can see, there is complete API Documentation of the Mobile Service.
You can perform any operation on the browser and after you can check Cloud where Mobile
Service reside.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
260
How to Create a Mobile App in Microsoft Azure
Mobile Apps in Microsoft Azure offer many available mobile application development
platforms.
Build native and cross platform apps:
We can create native IOS, Android, and Windows apps or cross-platform Xamarin or Cordova
(PhoneGap) apps, you can also consider many advantages of App Service using native SDKs.
Connect to enterprise systems: Mobile Apps you can connect to your enterprise cloud
resources.
Push Notifications to billions in seconds
Mobile App Features:
The following features are important to cloud-enabled mobile development:
Authentication and Authorization
Integrate the SDK
Data Access
Offline synchronization
Push Notifications
For creating an Android App you must have to login into the Azure Portal
Click on to New, Web + Mobile, then Mobile App,
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
261
In the name box, give the app name whatever you want to create, and subscription would be
default.
In the Resource Group, I have a resource group named NitinPandit, you can also create your by
clicking on new resource group.
Now click on ―Create‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
262
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
263
After clicking on the button, your app will be created, click on app and go into the settings.
In the settings, click on to ―Quick start‖ which is our client app platform,
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
264
Now in the following screenshot, click on to any platform which one you want to create an
application.
Here, I’m going to select ―Androidbecause I’m creating an Android app.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
265
Here is the first step that connects to database, so click on to create or select the existing database
which you have.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
266
There are no data connections, Click on to ―Add‖ button to add a database.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
267
Choose SQL Database, and then click on to required settings.
You will find a new database window, if you don’t have any existing database click on to create
a new database.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
268
For creating database I also need of a server, so in the left side of the window give the name to
your database and then click on to server for creating a new server. Whenever you click on it, the
new server window will be open.
For creating a server give a unique name to your server, login name, and password with confirm
password.
After filling all the necessary details, click on to ―OK‖ button. So now our database and server is
ready for use.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
269
Click on to Connection string, type the login and password values for your database, then click
OK twice.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
270
After that our first step of creating the database would be ready, now go for the second step.
Under create a table API, select your Backend language, either C# or Node.js:
Node.js backend (via portal):
Click on to create TodoItem table and this creates a new TodoItem table in your database.
NET backend (C#):
Click Download, extract the compressed project files to your local computer, open the
solution in Visual Studio, build the project to restore the NuGet packages, and then
deploy the project to Azure. You Mobile App backend is now ready to use with your
client app.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
271
I’m going to select C#, and click on to ―Download‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
272
A file will be downloaded, extract the file and open it.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
273
You’ll get the solution of your Android mobile app with ―.sln‖, so you can directly use this into
your Visual Studio to customize that.
Now just right click on ―.sln‖ file and click Open with...
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
274
From here just select any IDE by which you want to edit this.
This is your solution now.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
275
We have a test API controller already created. Open it and change it as you want to get data.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
276
Press the Run 'app' button to build the project and start the app in the Android simulator,
If you want to use Node.js, then select Node.js backend.
Your TodoItem table is ready. To check it just go to ―Easy tables‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
277
Click on to ―TodoItem‖ to check your table.
Now if you want to add new tables, then make them.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
278
Manage Database Settings of Azure Mobile Services
In this chapter, I’m going to explain how we can change databases in existing Azure Mobile
Services. The server of the new database will be the same. For creating databases you will need
to create a server for the database. For creating databases as well as server you can refer to my
below given chapter:
Manage Azure Mobile Services
In the following figure, I have a mobile service, click on it.
To change the database of this mobile service, click on ―CONFIGURE‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
279
So, here in the following figure you can see SQL Database and SQL Server.
Click on to ―ChangeDatabase‖ in the bottom of the window.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
280
If you have more databases in your server then you can choose which you want to change, in this
condition you must have to choose existing database. In my view I don’t have any existing
databases so I’m going to choose a new SQL Database instance option.
For further information, click on the arrow
In the following screenshot, give the database name and choose the server, my server is to be
existing.
Give the server login name and password (Server login name and password would be that which
you used at the time of creation your server)
Click on to check button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
281
So here I have successfully changed my database.
So the benefit of changing the database is that we can use our best database in our existing
mobile service.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
282
Get Twitter Account Connected Into Microsoft Azure
Blob Storage Live
In this chapter we’ll learn how to connect our Twitter account with Microsoft Azure Blob
storage account and get the live tweets to Azure step by step.
Here are the steps:
Step1: Go to Microsoft account login page and type your login details.
Step2: Click ―Sign in‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
283
For working on the Azure Portal, go to the ―HOME‖ button on the top of the window and click
on it.
Click on to Portal‖.
First, I need to create a ―Storage Account‖ and ―Blob Container.‖
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
284
So, go to ―Browse‖ and scroll down, and select ―Storage accounts (classic).
You will see the following figure, click on ―Add‖ button to create a ―Storage Account‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
285
In the name section give whatever name you want, I gave it storagenitintwitter. I need to
create a new ―Resource Group‖ also. Resource Group is a place where I managed all the related
account of particular storage account.
I gave ―IMNitin‖ name to my resource group.
Select the location of your storage. My location of the storage is East Asia‖, so I selected East
Asia. Click on ―Create button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
286
Now, I need to create a ―Blob Container‖ for the account, so just click on Storage Account and
then ―Blobs‖ to create a blob.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
287
Now in the next window, click on to Container‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
288
Give the name to your Container; here I have given it ―nitincontainer.‖ Access type will be
―Private‖ for your access only.
Click on ―Create‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
289
Now, I need to create an ―Azure Blob Storage Connector.‖ Click on ―Browse‖ and in the
search panel type ―Marketplace.
Click on to Marketplace.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
290
In the search box type ―Azure Storage Blob Connector.
Click on to Azure Storage Blob Connector.‖
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
291
Click on ―Create‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
292
After clicking on Create button, give the name for Blob Connector, here I have given it
Azure Storage Blob Connector.‖
Then click on ―Package Settings," it will ask for Blob Container Uri, so just go to your container
and copy the path of Blob Container.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
293
Click on ―Browse‖ and go to your storage account.
Click on to Blobs.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
294
After clicking on ―Blobs,‖ you will find your container; here my container is nitincontainer.
Whenever I click on it, it will redirect to container properties.
Click on to Properties.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
295
Copy the ―URL‖ from container properties.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
296
Paste the copy ―URL‖ over here in the package settings.
Click on to ―OK‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
297
In the App service plan, give the service plan name; I have given nitinlogicplan.
Select the ―resource group‖ which is ―IMNitin.‖
And click on ―Create‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
298
Now, I need to create our ―Twitter Connector,‖ for it go to ―Browse‖ and type ―Marketplace.‖
Click ―Marketplace‖.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
299
In search box, type ―Twitter Connector."
You will find ―Twitter Connector." Click on it as in the following:
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
300
Click on ―Create‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
301
Here in the below figure, give the name for Connector; here I have given it ―TwitterConnector."
Next is ―Package Settings," after clicking on it you will be redirected to package settings
window. This window will ask for ―Consumer key‖ and ―Consumer Secret."
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
302
For the ―Consumer key‖ and ―Consumer secret," go to the https://apps.twitter.com/.
Note: Make sure for this process you should log in to your twitter account.
Click on to Create New App."
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
303
After clicking on button, you will see the below page, here I need to create an application, where
we can find our ―Consumer key‖ and ―Consumer Secret."
In the name section, I have given ―StorageNitinTwitter."
In the description section, you can give any script; I have given ―app for Azure."
In the ―Website‖ section, give the temporary ―URL‖ over here, your applications publicly
accessible home page, where users can go to download, make use of, or find out more
information about your application. This fully-qualified URL is used in the source attribution for
tweets created by your application and will be shown in user-facing authorization screens.
(If you don't have a URL yet, just put a placeholder here but remember to change it later.)
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
304
Check on ―Yes," and then click ―Create your Twitter application."
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
305
I have successfully created an application of twitter, click on Keys and Access Tokenson the
top of the page, and copy the ―Consumer Key‖ and ―Consumer Secret‖ from here.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
306
Paste both the keys over here in the ―Package Settings."
Click on ―OK‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
307
Select the app logic plan which is nitinlogicplan, and select the resource group which is
IMNitin."
Click on ―Create‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
308
So, I have successfully created my ―Twitter Connector,‖ so now I need to copy the ―URI‖ of the
Twitter Connector and provide in the Twitter Application, in ―Website‖ and ―Callback URL‖
section.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
309
Go to the ―Resource groups‖ and select the ―IMNitin‖ resource.
Here are all my apps in one resource, ―IMNitin‖ named resource.
For the ―URL‖, click on to ―Twitter Connector".
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
310
Click on to ―Authentication."
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
311
Copy the ―Redirect URI‖ from here.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
312
Paste the ―URI‖ in ―WebSite‖ and ―Callback URL‖ in the twitter application page.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
313
Click on ―Update Settings‖ button to save the details.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
314
Now, I need to create logic app, so go to ―New," and select ―Web+Mobile," and select ―Logic
App."
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
315
Give the name to your logic app; here I have given ―NitinLogicApp‖ and select app service plan
which is ―nitinlogicplan," select resource group which is ―IMNitin."
Click ―Create‖ button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
316
For creating Triggers and actions, go to the ―resources groups," click on resource group, which is
―IMNitin," and select ―NitinLogicApp," which is just created.
Click on ―Triggers and actions."
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
317
Select the ―Create from Scratch‖ panel.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
318
Now, you will see following figure. I need to create recurrence where I get the schedule of the
recurrence.
Click on ―Recurrence," right side of the window in resource group.
Choose the frequency and interval of the recurrence schedule, and click on the green check
button.
©2016 C# CORNER.
SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
319
Now, we need to create twitter connector, where I fetch my twitter applciation.
So just select the ―Twitter Connector."