How to create Database User programmatically?

November 3, 2009

Well… we need to assign a server login to has access on a database, let’s say the SQL Server Login we had created programmatically should has access on database “Database1” and for some reasons we should do that programmatically

Well. Create any C# project type (Windows, Class Library or even Console one), add reference to Microsoft.SqlServer.Smo, and Microsoft.SqlServer.ConnectionInfo

/// <summary>

/// Creates database user

/// </summary>

private void CreateDatabaseUser(Database database, String username)


User sqlServerUser = new User(datawarehouseDatabase, username); //initializes new User object and we say to which database it belongs and its name

sqlServerUser .UserType = UserType.SqlLogin; //SqlLogin not anything else

sqlServerUser .Login = “login_name”; //associated the user to login name, login name should be valid login name

sqlServerUser .Create();//here’s we create the user on the database and till now the user don’t have any permission on database objects

sqlServerUser .AddToRole(“db_owner”); //or any role like db_databasereader, db_databasewriter,…


P.S: There’s catastrophic mistake in Microsoft documentation of User.Login as they documented it as ReadOnly field although it’s not.sqlloginHow we can call the above method?

Server sqlInstance = new Server(

new Microsoft.SqlServer.Management.Common.ServerConnection(

new System.Data.SqlClient.SqlConnection(“.”))); //connects to the local server

Database database = sqlInstance.Databases[“adventureworks”]; //initialize new object from database adventureworks

CreateDatabaseUser(database, “RamyMahrous”); //creates user RamyMahrous on database adventureworks


Deploying Reports in Reporting Services Programmatically

October 18, 2009

May be it’s my bad luck may be something else, but I didn’t find a complete post how to deploy Reports, and Data Sources in Reporting Services in away like BIDS.

First it’s a tool I developed to automate the deployment of BI solution (Creating Data warehouse, installing SSIS packages, creating Jobs, create Cubes and installing reports on Reporting Services Server).

If you don’t have time to read and just need to download the tool, here you’re
Source Code

Herein, I’ll talk about the last thing which is deploying reports.

P.S: It’s my way in designing such task (Installing reports on Reporting Services Server) and it’s not standard or anything else just (follow your heart :))

Read the rest of this entry »

Integrating SSIS with C# to deliver very scalable Data-Driven Solution

August 14, 2009

نوضح في هذه المقاله استخدام ال SSIS  لبناء C# application to get very scalable data-driven solution  و كيفيه ارتباط ال C#  بالـ SSIS

ما هم ال SSIS  ؟؟ كيفيه الحصول عليه ؟؟ وماهي مميزاته؟؟ وفيما يستخدم اكثر؟؟

اولا SSIS  تعني SQL Server Integration Services  وهى احدى منتجات ميكروسوفت ويأتي مع جميع نسخ ال SQL Server  ما عدا ال Express  كان يسمى فيما سبق DTS  لكن SSIS  يعتبر منتج جديد وليس تحديثا من ال DTS

هي اداه قامت ميكروسوفت بتطويره و اخراجه للسوق عام 2007 وتهدف الى نقل البيانات من اكثر من Data Repository  الى Data Repository  اخر مثلا نقل البيانات من ملف نصي، ملف Excel  او قاعاده بيانات SQL or Access  الى جدول في قاعده بيانات في ال SQL Server  دون الحاجه لوجود SQL Server  ولا يجب مصدر البيانات او وصول البيانات SQL Server بأختصار يكمن استخدمها لنقل البيانات من ملف نصي الى قاعده بيانات Oracle تعتبر ال SSIS  اداه ETL (Extract, Transform, and Load)  اي انها تقوم بأخد البيانات من المصادر تحسين البيانات بشكل وتنظيمه وبعد هذا نقلها الى نقطه الوصول.
Read the rest of this entry »

How to create SQL Server Login programmatically?

April 28, 2009

I got this question from Daniweb C# forum, and of course when I need to do something against SQL Server object from .net I go to SMO (SQL Server Management Objects) which provides a great functionalities to do anything with SQL Server instead of going on SSMS and create some scripts then embedding them into stored procedure then call it…. off…

Anyway to avoid showing how much I’m talkative…

Create any C# project type (Windows, Class Library or even Console one), add reference to Microsoft.SqlServer.Smo, Microsoft.SqlServer.ConnectionInfo
and Microsoft.SqlServer.SqlEnum

Server sqlServerInstance = new Server(
new Microsoft.SqlServer.Management.Common.ServerConnection(
new System.Data.SqlClient.SqlConnection("Data Source=.;Initial Catalog=Master;Integrated Security=True")));// your connection string I place mine for illustration..
Login loginObj = new Login(sqlServerInstance, @"DomainName\UserName");
loginObj.DefaultDatabase = "Master";
loginObj.LoginType = LoginType.WindowsUser;
loginObj.Create("password"); //set the password
//there're many properties to do some tasks related to Login object...

If you used LoginType.WindowsUser, be sure to provide valid windows username
and if you aren’t on Domain use the machine name instead.

If you need to create SQL login use LoginType.SqlLogin…

You can explore Login class more on