Monday, December 29, 2014

Sql Azure : Way Around for Sp_depends in Sql Azure

You can use following query to find out table used in Stored procedures similar to sp_depends.

Sp_depends helps us to provide information where sql object in referred in Views, stored procedure.
sp_depends is not available in sql azure but following query will provide similar functionality.
SELECT ReferencingObjectType = o1.type,
       ReferencingObject = SCHEMA_NAME(o1.schema_id)+'.',
       ReferencedObject = SCHEMA_NAME(o2.schema_id)+'.'+ed.referenced_entity_name,
       ReferencedObjectType = o2.type
FROM   sys.sql_expression_dependencies ed
       INNER JOIN  sys.objects o1
         ON ed.referencing_id = o1.object_id
       INNER JOIN sys.objects o2
         ON ed.referenced_id = o2.object_id
WHERE o1.type in ('P','TR','V', 'TF') and ed.referenced_entity_name like '<Table Name>'
ORDER BY ReferencingObjectType, ReferencingObject

replace <Table name>  with your actual table name which you want to search

Happy Coding !!

Steps to Create a customize Role for Azure database

Steps to Create a customize Role for Azure database
Download "Azure User Management Console" tool from

Create a Login
Use “Azure User Management Console”
EG: dbdevwebuser
Create User for login on master
Connect to master database, and execute the following:
CREATE USER dbdevwebuser FOR LOGIN dbdevwebuser WITH DEFAULT_SCHEMA=[dbo]

This has created user “dbdevwebuser” for the login “dbdevwebuser”.
Note: You can keep the login and the user name separate.

Create User for login on the database to be used by application
Connect to application database, and execute the following:
CREATE USER dbdevwebuser FOR LOGIN dbdevwebuser WITH DEFAULT_SCHEMA=[dbo]

Create Customized Role 
Connect to application database, and execute the following:
This will create a role by the name “db_executor”

Grant Permissions to Customized Role 
Here I am trying to create a role to grant execute for all stored procedure
GRANT EXECUTE TO [db_executor]

This will grant execute permission to the role “db_executor”
Note: You can “GRANT” more permission to this role, to make it more customized.

Assign Roles to User Created
Assign different roles of your choice to the user
sp_addrolemember @rolename = 'db_executor', @membername = 'dbdevwebuser'
sp_addrolemember @rolename = 'db_datareader', @membername = 'dbdevwebuser'
sp_addrolemember @rolename = 'db_datawriter', @membername = 'dbdevwebuser'

This will assign roles of “db_executor”, “db_datawrited” and “db_datareader” to the user 'dbdevwebuser' on the application database.
1. These kind of roles are usually used when you want your application to execute procedures, read and write data. But do not want them to manipulate any Schema.

Testing working of the User for the Roles Assigned

This user should
1. Execute procedure
2. Select, Insert, Update, Delete from the database
3. Create Temporary tables
4. Select View
This user should not:
1. Create, ALTER, Delete Table
2. Create, ALTER, Delete Procedure
3. Create, ALTER, Delete View
4. Create, ALTER, Delete Function
5. See Content “sp_helptext” for a Procedure

Happy Coding !!

How to make a database copy from production to staging server in Azure

On the same server

We need 1 user name with its password on staging database and production database. It is 1 time process only. If same user is not present in source and destination database then we can not create backup of database.
Note: It is required only if your Source and destination database is present on different server. If they are present in same server then you need to execute only following query with your respected values.

1.       Connect to destination server (Here Staging DB) “master” database with productions username and pwd  because we need to have same username with password on source and destination DB.

2.       Execute the following command:
b.      IMP: Replace the NEWDATABASENAME, SOURCE_SERVERNAME and SOURCEDATASE with correct names

To track progress you can run following command or you can connect to Azure portal and check the progress in database tab

3.       Execute: Select *  from sys.dm_database_copies, this will give you the stats of the % of database copied

hHappy Coding !!

How to rename a SQL Azure database?

1. Connect to your Database
2. Select Master Database
3. Type following Query

alter database <old-database-name> modify name = <new-database-name>

<old-database-name> is your old DB name
<new-database-name> is your new DB name
Note: Don't use single or double quotes around DB Name

alter database MyStudent modify name = StudentMaster

Your DB name is changed :)

Happy Coding !!

Thursday, April 10, 2014

Error Logging using Log4Net for azure and normal applications

Following are good links for error logging using Log4Net

For Normal Applications:

For Azure Applications:

Wednesday, April 9, 2014

Azure Active Directory integration in MVC

Azure Active Directory integration in MVC

Pre- requisite:

1. Visual Studio 2012 Professional or Visual Studio 2012 Ultimate
2. Identity and Access Tools for Visual Studio 2012
3. Azure Subscription

Step 1:

 You need to create Active directory in your Azure subscription. You need to register your application in azure subscription.
 Active directory is called as tenant. Your users from your active directory(tenant) are able to login to your application.
 other users are not able to login. To make other users access available then you need to implement step 3 as well.
Details explained in following link.

Step 2:

You can achieve following:
 1. Getting full user details
 2. Creating and Updating Users
 3. Getting a list of groups
 4. Updating group membership
Details explained in following link.

Step 3:

 If you want to allow access to other user from your organization. then you need to implement multiple tenant(active directory) in your application.
 So other user will be able to login to your application.
Details explained in following link.

Happy Coding !!

Monday, April 7, 2014

Azure: Building and Packaging Virtual Applications within Azure Projects

Check following links for proper deployment. Which deploys published code instead of complete code solution.

Thursday, January 23, 2014

Windows Azure: Upload and Download Functionality- upload/download image to blob

Following class is useful to download or upload image from blob. Provide necessary parameters to function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Configuration;
using Microsoft.WindowsAzure;
using System.IO;
using Microsoft.WindowsAzure.ServiceRuntime;
namespace AzureDemo
    public class AzureBlobManager
        public string uploadFilesToAzureBlob(FileUpload PostedFile, string FileName, string containerName, Guid userid)
            string blobURLs = string.Empty;
                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                // Create the blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                // Retrieve a reference to a container.            
                CloudBlobContainer container = blobClient.GetContainerReference(containerName);
                // Create the container if it doesn't already exist.
                if (PostedFile.PostedFile.ContentLength > 0)
                    System.IO.Stream inputStream = PostedFile.PostedFile.InputStream;
                    inputStream.Position = 0;
                    byte[] myBinary = new byte[PostedFile.PostedFile.ContentLength];
                    //PostedFile.InputStream.Read(myBinary, 0, (int)PostedFile.ContentLength);
                    using (var binaryReader = new System.IO.BinaryReader(inputStream))
                        myBinary = binaryReader.ReadBytes(PostedFile.PostedFile.ContentLength);
                    // Retrieve reference to a blob named "myblob".
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(string.Concat(userid, "_", PostedFile.FileName));
                    // Create or overwrite the "myblob" blob with contents from a local file.                    
                    blockBlob.UploadFromStream(new System.IO.MemoryStream(myBinary));
                    // blockBlob.UploadFromStream(PostedFile.PostedFile.InputStream);
                    blobURLs = blockBlob.Uri.ToString();
            catch (Exception ex)
            return blobURLs;

//Directly reads from blob and sends it to user
        public void DownloadFileFromBlob(string fileName,string containerName)
            CloudStorageAccount account = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
            CloudBlobClient blobClient = account.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
            MemoryStream memStream = new MemoryStream();
            HttpContext.Current.Response.ContentType = blob.Properties.ContentType;
            HttpContext.Current.Response.AddHeader("Content-Disposition", "Attachment; filename=" + fileName.ToString());
            HttpContext.Current.Response.AddHeader("Content-Length", blob.Properties.Length.ToString());

Happy Coding!!