Search This Blog

Wednesday, February 9, 2011

How can Use LINQ in C#

Language Integrated Query (LINQ) adds the ability to query objects using .NET languages. The LINQ to SQL object/relational mapping framework provides the following basic features:
LINQ has a power of querying on any source of data, data source could be the collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable interface. Microsoft basically divides LINQ into three areas and that are give below.

•LINQ to Object {Queries performed against the in-memory data}
•LINQ to ADO.Net
◦LINQ to SQL (formerly DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported}
◦LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables}
◦LINQ to Entities {Microsoft ORM solution}
•LINQ to XML (formerly XLinq) { Queries performed against the XML source}
Tools to create classes (usually called entities) mapped to database tables
Compatibility with LINQ’s standard query operations
The DataContext class, with features such as entity record monitoring, automatic SQL statement generation, record concurrency detection

Code for LINQ**************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using pmsContract.entities;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace pms.Webproject
{
//we have create class usertest and table nam: pms_user

[Table(Name = "pms_user")]//define table name
public class usertest //this is classs
{
[Column]
public string userfname { get; set; } //get or set table fields
[Column]
public string userlname { get; set; }
[Column]
public string useremailID { get; set; }

}

public partial class test : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
string conn = ConfigurationManager.ConnectionStrings["stringConnection"].ToString();
DataContext db = new DataContext(conn);//Interface between linq or SQL
Table<usertest> pmsusettest = db.GetTable<usertest>();//create object of table
var query = from d in pmsusettest orderby d.userfname select d; //select linq query for data selection.
GridView1.DataSource = query;//provide datasource to gridview
GridView1.DataBind();
}

}
}

Tuesday, February 1, 2011

What is Triggers ???

Triggers are not new to SQL Server. But prior to SQL Server 2005 triggers were DML triggers, which were raised only when there is an INSERT,UPDATE or DELETE action. A new table, database or user being created raises a DDL event and to monitor those, DDL triggers were introduced in SQL Server 2005.
Sometimes a trigger is referred as a special kind of stored procedure in term of procedural code inside its body. The difference between a trigger and a stored procedure is that a trigger is activated or called when an event happens in a database table, a stored procedure must be called explicitly. For example you can have
some business logic to do before or after inserting a new record in a database table.

INSERT trigger
When an INSERT trigger statement is executed ,new rows are added to the trigger table and to the inserted table at the same time. The inserted table is a logical table that holds a copy of rows that have been inserted.
DELETE trigger
When a DELETE trigger statement is executed ,rows are deleted from the table and are placed in a special table called deleted table.
UPDATE trigger
When an UPDATE statement is executed on a table that has an UPDATE trigger,the original rows are moved into deleted table,While the update row is inserted into inserted table and the table is being updated.

Trigger Example......
We have main table [fig] and we want to create trigger [trgr_logdetails].In this query when insert data in main table [fig] at this movement also fire trigger [trgr_logdetails] and insert data in logical table [trgr_logdetails].

CREATE TRIGGER [trgr_logdetails]
 on [fig]
   AFTER insert
AS
BEGIN
 declare @figID int
 declare @roleID  int
 declare @menuID int
 declare @figcreatedBy nvarchar(100)
 declare @figupdatedBy nvarchar(100)
 declare @rigthscreatedate datetime
 declare @figupdateddate datetime
 declare @rigthsstatus  bit
 declare @tabid   int

 set @figID = (select figID  from inserted)
 set @roleID =(select roleID from inserted)
 set @menuID=(select menuID from inserted)
 set @figcreatedBy=(select  figcreatedBy from inserted)
 set @figupdatedBy=(select  figupdatedBy from inserted)
 set @rigthscreatedate=(select  rigthscreatedate from inserted)
 set @figupdateddate=(select  figupdateddate from inserted)
 set @rigthsstatus=(select  rigthsstatus from inserted)
 set @tabid=(select tabid from inserted)

    -- Insert statements for trigger here
    begin
  INSERT INTO [trgr_logdetails]
           ([figID]
           ,[roleID]
           ,[menuID]
           ,[figcreatedBy]
           ,[figupdatedBy]
           ,[rigthscreatedate]
           ,[figupdateddate]
           ,[rigthsstatus]
           ,[tabid])
     VALUES
           (@figID
           ,@roleID
           ,@menuID
           ,@figcreatedBy
           ,@figupdatedBy
           ,@rigthscreatedate
           ,@figupdateddate
           ,@rigthsstatus
           ,@tabid)
 end
END