Aug 13, 2011
no image

Recently I was working with some database restoring and configuring stuff. And I was getting errors due to the conflicts on database users, server logins and their permissions.

In the ideal scenario, there should be a 'login' in the database server, and a corresponding 'user' for the database. Sometimes people get confused with the difference of these two. But they are for two entirely different purposes.

  • Login - Used for user authentication, and created in the server level

  • User (Database user) – Provide the database access and used for permission validations on the data base. Created for each database specifically.

A login is required for accessing the database server. And in the usual case a user account (corresponding to each of those logins) is associated for each database which provide access to the database. If a database user account is not not associated with a login, the user is not able to log into SQL Server server. This usually happens when a database is restored to a different server (which was the problem I had)

Restored database contains a set of users and permissions but there may not be any corresponding logins or the logins may not be associated with the same users. This situation is usually is known as having "orphaned users". Following steps will be able to resolve this situation.

-- Creating the login

Use master

sp_addlogin 'test', 'password', 'TestDB'



-- Granting the access to the created loginUse TestDB

Use TestDB

sp_grantdbaccess 'test'



-- Resolve orphaned users

Use TestDB

sp_change_users_login 'update_one', 'test', 'login_pswd'

In above the first step (creating the login), TestDB is the default database for the login. Which is the first database login connected after logging in.

The converse of the above described scenario; if no user account exists in a database for a specific login, the user that is using that login cannot access the database even though the user may be able to connect to SQL Server.

But there’s a single exception to this. Even if there is no user account is created in a database for a particular login, still the login can access the database trough a “guest user”. This guest user is created automatically for each database when it was created. And it is set to disabled by default. Another important thing to mention is, this guest login cannot be deleted from the database. 

You can enable and disable that account using the below query.
-- To give access to guest account from SA login:

USE TEST_TABLE  

GRANT CONNECT TO GUEST  



-- To revoke access to guest account from SA login:

USE TEST_TABLE  

REVOKE CONNECT FROM GUEST  



-- To grant permission on tables for guest user

GRANT SELECT ON TestDB.EmloyeeDetails TO GUEST
Granting the permission can be also done with using the SQL server Management Studio through it’s GUI.  Now try a SELECT query on the database table, with a login which its database user is not created in the TestDB database. You should be able to perform the operation even without having the database user name.

However it is not recommended for giving the sensitive database permissions for guest user. I found some articles on the web (link) where, people have published about possible security risks/bugs in using the guest user and granting permissions on it. 
Aug 9, 2011
no image


If you have experience with SQL Server, you may have heard the terms MDF, NDF and LDF already. Those are the commonly used file name extensions in SQL Server for specify the Primary Data files, Secondary data files and Log files respectively.

In SQL Server, data and log information are never stored in the same file. Furthermore, those individual files (primary data, secondary data and log) are used by only one server. However, below is a brief description about the three file types in SQL Server.

Primary data files

Primary data file is the starting point of the database. It points to the other files in the database. Therefore, every database has one primary data file. Also, all the data in the database objects (tables, stored procedures, views, triggers.. etc.) are stored in the primary data files. The recommended and the most common file name extension for primary data files is ".mdf".

Secondary data files

You can only have one primary data file for a database. Rest made up by secondary data files. But its not necessary to have a secondary data file. Therefore some databases may not have any secondary data file. But its also possible to have multiple secondary data files for a single database. ".ndf" is usually recommended to denote secondary data files. It’s also possible to store the secondary data file in a separate physical drive than the one which primary data file is stored.

Log files

Log files in SQL Server databases hold all the log information. Those information can be later used to recover the database. Size of the log file is determined by the logging level you have set up on the database.  There must be at least one log file for each database. But it is also possible to have more than one log file for a single database. The recommended file name extension for log files is ".ldf".


IMPORTANT : Though it’s recommended to use these .mdf, .ndf and .ldf filename extensions, SQL Server never enforces to use the same file extensions.

Locations of all the files of user databases (primary, secondary and log files of each) are stored in the primary data file of the database and in the master database. When the SQL Server Database Engine want to use the file location information of those  files, it retrieves the data from the master database.
Feb 4, 2011
no image

If you are looking for a free email server to use in your C# application, Gmail SMTP server can be a good choice. Using.Net framework built in classes in the System.Net.Mail namespace, you can easily create and send emails via Gmail SMTP server.

Following example is a very simple implementation of using Gmail to send an email.

using System.Net.Mail;

namespace GmailSample.Utility
{
    public class GmailSmtpSample
    {
        public void SendEmail(string to, string subject, string message)
        {
            MailMessage mailMessage = new MailMessage();

            // Set from, to email addresses. Cc and Bcc can be configured in the same way.
            mailMessage.From = new MailAddress("Your Gmail Address"); 
            mailMessage.To.Add(new MailAddress(to));

            // Set email subject and body
            mailMessage.Subject = subject;
            mailMessage.Body = message;

            // You can also add attachments
            mailMessage.Attachments.Add(new Attachment(@"C:\Users\Kalana\Desktop\SampleAttachment.txt"));

            // Define whether email body contain HTML content
            mailMessage.IsBodyHtml = true;

            // Create the SMTP client
            SmtpClient gmailSmtpClient = new SmtpClient();
            gmailSmtpClient.Host = "smtp.gmail.com";
            gmailSmtpClient.Port = 587;
            gmailSmtpClient.EnableSsl = true;

            // Specify authentication details for your Google account
            gmailSmtpClient.Credentials = new System.Net.NetworkCredential("Your Google Username","Your Password"); 

            gmailSmtpClient.Send(mailMessage);
        }

    }
}
That's easy ! Isn't it?

Is Gmail SMTP the right choice?

Using Gmail to send emails for your application can be helpful if you’re working on a small personal project and you just need to get that output email anyway. Also, if you are testing your application and need an SMTP server to test the email functionality, Gmail would be fine.

But before configuring Gmail you have to keep in mind that there are restrictions introduced by Google which limits the number of emails sent per day and the number of recipients per message. So you would only be able to send 2000 mails in a 24-hour period. Anything more will even lead to temporarily blocking your Gmail account.

Therefore, you might want to think about your requirement and the amount of emails you want to send before selecting Gmail as yout SMTP server of choice.
Jan 29, 2011
no image

Like any other computer program, a program written in C# should have a default entry point. his entry point is where your code start executing.  Similar to many high level programming languages C# entry point is the “Main” method. (Libraries and services do not require a Main method as an entry point.) Main is also a member of a C# class or struct. This post is going to explain about few tricky points about usual C# main method and program execution.


Different ways of declaring “Main” method

There are four different syntax which we can use to declare main method.




Main must be static and should be declared as ‘private’ (even though access modifier is not mandatory). It should not be public. (In the earlier example, it receives the default access of private). When we declare main method as ‘int’ operating system waits for a return value. If the return value is 0, program execute without any error. But if it returns any other value than 0 it denotes the executions was a failure.

That return point value is cached internally for inter process communication. It’s very useful if the program is a part of a system of application (rest of the system can determine the success of the task assigned to the program)


Multiple Entry Points in a code

Even though its not recommended, it is possible to have more than one entry point in the same code. Consider the following code sample. Imagine this is stored in test.cs


using system;

  namespace Application {
   class FirstPro {
     static void Main (){
      // main block
     }
   }

   class SecondPro {
     static void Main (){
      // main block
     }
   }
  }
This code has two entry points and both are valid. Therefore we have to manually specify the desired entry point to run the program. This can be done using the ‘main’ compiler option.

   csc /main:Application.FirstPro test.cs


Command Line Arguments

The Main method can be declared with or without a string[] parameter. But that parameter contains command-line arguments which can be used to provide the program some values at the moment of execution. If you are using Visual Studio to create Windows Forms applications, (which is, the most obvious case) you can add the parameter manually or else can use the “Environment” class to obtain the command-line arguments. Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument.

However, to enable command-line arguments in the Main method in a Windows Forms application, you must manually modify the signature of Main in program.cs. The code generated by the Windows Forms designer creates a Main without an input parameter.