Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Tuesday, September 27, 2011

SQL SERVER – List All Database

This is the Stored Procedure which will retrieve database name and size in kb and remark

Exec sp_databases

This stored procedure will retrieve more column then previous sp and return size in mb rather than kb.
And owner of db, created date, and status of the database like status, updatability, useraccessibility etc.

Exec sp_helpdb


Select * From sys.databases


SQL SERVER – List All Tables of Database

USE YourDBName
GO
SELECT *
FROM sys.Tables
GO

or

USE YourDBName
GO
SELECT  *
FROM sysobjects where xtype='u'
GO


This will return all the tables in the database which user have created.



Tuesday, September 20, 2011

Pivot 3 Column in a table Sql Server

1: Table Structure

 2: Code 



SELECT ExhibitorID,
sum(CASE WHEN Installment ='Installment1'  THEN ReceivedAmount ELSE 0 END) AS Installment1,
max(CASE WHEN Installment ='Installment1' THEN ChequeDetails ELSE '' END) AS ChequeDetails1,

sum(CASE WHEN Installment ='Installment2'  THEN ReceivedAmount ELSE 0 END) AS Installment2,
max(CASE WHEN Installment ='Installment2' THEN ChequeDetails ELSE '' END) AS ChequeDetails2,

sum(CASE WHEN Installment ='Installment3'  THEN ReceivedAmount ELSE 0 END) AS Installment3,
max(CASE WHEN Installment ='Installment3' THEN ChequeDetails ELSE '' END) AS ChequeDetails3,

sum(CASE WHEN Installment ='Installment4'  THEN ReceivedAmount ELSE 0 END) AS Installment4,
max(CASE WHEN Installment ='Installment4' THEN ChequeDetails ELSE '' END) AS ChequeDetails4,

sum(CASE WHEN Installment ='Installment5'  THEN ReceivedAmount ELSE 0 END) AS Installment5,
max(CASE WHEN Installment ='Installment5' THEN ChequeDetails ELSE '' END) AS ChequeDetails5

FROM View_Test
GROUP BY ExhibitorID;


3: Out Put


































Monday, September 19, 2011

How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?


One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships.
One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships.Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table.

What’s the difference between a primary key and a unique key?

Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn’t allow NULLs, but unique key allows one NULL only.

What are different type of Collation Sensitivity?


Case sensitivity
A and a, B and b, etc.
Accent sensitivity
a and á, o and ó, etc.
Kana Sensitivity
When Japanese kana characters Hiragana and Katakana are treated differently, it is called Kana sensitive.
Width sensitivity
When a single-byte character (half-width) and the same character when represented as a double-byte character (full-width) are treated differently then it is width sensitive.

What is Collation?

Collation refers to a set of rules that determine how data is sorted and compared. Character data is sorted using rules that define the correct character sequence, with options for specifying case-sensitivity, accent marks, kana character types and character width.

What is a Linked Server?


Linked Servers is a concept in SQL Server by which we can add other SQL Server to a Group and query both the SQL Server dbs using T-SQL Statements. With a linked server, you can create very clean, easy to follow, SQL statements that allow remote data to be retrieved, joined and combined with local data.
Storped Procedure sp_addlinkedserver, sp_addlinkedsrvlogin will be used add new Linked Server.

What is the use of DBCC commands?

DBCC stands for database consistency checker. We use these commands to check the consistency of the databases, i.e., maintenance, validation task and status checks.
E.g. DBCC CHECKDB – Ensures that tables in the db and the indexes are correctly linked.
DBCC CHECKALLOC – To check that all pages in a db are correctly allocated.
DBCC CHECKFILEGROUP – Checks all tables file group for any damage.

What is cursors?


Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.
In order to work with a cursor we need to perform some steps in the following order:
Declare cursor
Open cursor
Fetch row from the cursor
Process fetched row
Close cursor
Deallocate cursor

What are the different index configurations a table can have?


A table can have one of the following index configurations:
No indexes
A clustered index
A clustered index and many nonclustered indexes
A nonclustered index
Many nonclustered indexes

What is the difference between clustered and a non-clustered index?


clustered index is a special type of index that reorders the way records in the table are physically stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain the data pages.
nonclustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a nonclustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.

What is Index?


An index is a physical structure containing pointers to the data. Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name. The users cannot see the indexes, they are just used to speed up queries. Effective indexes are one of the best ways to improve performance in a database application. A table scan happens when there is no index available to help a query. In a table scan SQL Server examines every row in the table to satisfy the query results. Table scans are sometimes unavoidable, but on large tables, scans have a terrific impact on performance.
Clustered indexes define the physical sorting of a database table’s rows in the storage media. For this reason, each database table may have only one clustered index.
Non-clustered indexes are created outside of the database table and contain a sorted list of references to the table itself.

What is View?

A simple view can be thought of as a subset of a table. It can be used for retrieving data, as well as updating or deleting rows. Rows updated or deleted in the view are updated or deleted in the table the view was created with. It should also be noted that as data in the original table changes, so does data in the view, as views are the way to look at part of the original table. The results of using a view are not permanently stored in the database. The data accessed through a view is actually constructed using standard T-SQL select command and can come from one to many different base tables or even other views.

What is Trigger?


A trigger is a SQL procedure that initiates an action when an event (INSERT, DELETE or UPDATE) occurs. Triggers are stored in and managed by the DBMS.Triggers are used to maintain the referential integrity of data by changing the data in a systematic fashion. A trigger cannot be called or executed; the DBMS automatically fires the trigger as a result of a data modification to the associated table. Triggers can be viewed as similar to stored procedures in that both consist of procedural logic that is stored at the database level. Stored procedures, however, are not event-drive and are not attached to a specific table as triggers are. Stored procedures are explicitly executed by invoking a CALL to the procedure while triggers are implicitly executed. In addition, triggers can also execute stored procedures.
Nested Trigger: A trigger can also contain INSERT, UPDATE and DELETE logic within itself, so when the trigger is fired because of data modification it can also cause another data modification, thereby firing another trigger. A trigger that contains data modification logic within itself is called a nested trigger.

What is Stored Procedure?


A stored procedure is a named group of SQL statements that have been previously created and stored in the server database. Stored procedures accept input parameters so that a single procedure can be used over the network by several clients using different input data. And when the procedure is modified, all clients automatically get the new version. Stored procedures reduce network traffic and improve performance. Stored procedures can be used to help ensure the integrity of the database.
e.g. sp_helpdb, sp_renamedb, sp_depends etc.

What is normalization?

Database normalization is a data design and organization process applied to data structures based on rules that help build relational databases. In relational database design, the process of organizing data to minimize redundancy. Normalization usually involves dividing a database into two or more tables and defining relationships between the tables. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database via the defined relationships.

What is RDBMS?

Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. Relationships may be created and maintained across and among the data and tables. In a relational database, relationships between data items are expressed by means of tables. Interdependencies among these tables are expressed by data values rather than by pointers. This allows a high degree of data independence. An RDBMS has the capability to recombine the data items from different files, providing powerful tools for data usage.

Friday, September 16, 2011

Adding Dynamic Meta Tags with Master Pages in ASP.NET using C #

Today each and every website is getting dynamic, every website for example have just on page and is being used to display the contents for various categories, hence the need for the dynamic meta title, meta description and meta keywords arises especially for implementing Search Engine Optimization(SEO) properly on your website having dynamic pages.

In a website what people do is create a Master Page and then this Master Page further contains a number of content or child pages, now what we want to have is having separate meta title, meta description and meta keywords for every child page, but here is the problem as once you run you run your website – preference is given to the meta title, meta description and meta keywords you have given in you Master Page and hence all the child pages will have same meta title, description and keywords.

Basically In ASP.NET you do not have any direct function to re write the title,description and keywords when you are working with Master Pages. To solve this problem I have written some sample code:


Firstly in your master page find this code

< head runat=”server”>
< title>Untitled Page< /title>

Than Replace it with the following code :

< head runat=”server” id=”master_Head”>
< meta name=”description” runat=”server” id=”master_Description” content=”" />
< meta name=”keywords” runat=”server” id=”master_keywords” content=”" />
< title>Untitled Page< /title>

That’s all you have to change in your master page.
Now in your child or content page include one Header File:

using System.Web.UI.HtmlControls;

Once all this done, now in your child or content page , on load write following code:

void Page_Load(Object sender, EventArgs e)
{
    HtmlHead objHead = new HtmlHead();
    objHead = (HtmlHead)Master.FindControl(“master_Head”);
    HtmlMeta objMetaDescription = (HtmlMeta)objHead.FindControl(“master_Description”);
    HtmlMeta objMetaKeywords = (HtmlMeta)objHead.FindControl(“master_keywords”);

    Title = “[Write You Title Here]“;
    objMetaDescription.Content = “[Write You Description Here]“;
    objMetaKeywords.Content = “[Write You Keywords Here]“;

}

Tuesday, September 6, 2011

Thursday, September 1, 2011

Show Current User Count On Your Website

Web application contained a file called the "global.asax". The Global.asax file is event driven. It can contain four event procedures: Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd.

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["WhoOn"] = 0;

    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        Application.Lock();
        Application["WhoOn"] =Convert.ToInt32(Application["WhoOn"]) + 1;
        Application.UnLock();
     
    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.
        Application.Lock();
        Application["WhoOn"] =Convert.ToInt32( Application["WhoOn"]) - 1;
        Application.UnLock();

    }
      
</script>

Then to display the count on any ".aspx" page in your web site just do something like this.

<%= Application("WhoOn") %> USERS CURRENTLY ONLINE

Now, the Session_OnEnd part basically means that if a user has no activity for a period of time that (1) will be subtracted from the current user count. That time period being whatever you have the Session.Timeout set to. This means that if your Session.Timeout is set to 20 minutes that it may take up to 20 minutes for the count to update after a user leaves.

The other thing to realize is that a user can inflate the count by closing their browser and coming back to your site over and over. This problem takes care of it itself over time as all those counts will eventually expire and go away on their own but it is something to be aware of. Sometimes you'll notice this happen and it will be some jerk messing around to see if they can make your count go up. Other times it will be search engine bots indexing your site that cause the count to spike.

Also remember that if the web is not set up to run as an application the "global.asax" will not run. You'll need to make sure the web is an application. Most Virtual Domains are by default, but sub webs usually are not.

For the sub webs to run the 'global.asax" they need to be an application as the root usually is. In NT this is accomplished via the Internet Service Manager under the properties of the sub web you want to make an application