Tuesday, July 30, 2013

CRUD Operation in MVC3 using Store Procedure's with Example

DataBase :

USE [SampleDatabse]
GO
/****** Object:  Table [dbo].[Student]    Script Date: 07/31/2013 01:34:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[City] [varchar](50) NULL,
[Address] [varchar](50) NULL,
 CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  StoredProcedure [dbo].[Student_Update]    Script Date: 07/31/2013 01:35:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[Student_Update]
@Id int,
@Name varchar(50),
@City varchar(50),
@Address varchar(50)

as

begin

update Student set
Name=@Name,
City=@City,Address=@Address

where Id=@Id
End
GO
/****** Object:  StoredProcedure [dbo].[Student_LoadByPrimaryKey]    Script Date: 07/31/2013 01:35:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[Student_LoadByPrimaryKey]
@Id int


as

begin

select *from Student where Id=@Id
End
GO
/****** Object:  StoredProcedure [dbo].[Student_LoadAll]    Script Date: 07/31/2013 01:35:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[Student_LoadAll]



as

begin

select *from Student
End
GO
/****** Object:  StoredProcedure [dbo].[Student_Insert]    Script Date: 07/31/2013 01:35:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[Student_Insert]
@Name varchar(50),
@City varchar(50),
@Address varchar(50)

as

begin

insert into Student values(@Name,@City,@Address)

End
GO
/****** Object:  StoredProcedure [dbo].[Student_Delete]    Script Date: 07/31/2013 01:35:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[Student_Delete]
@Id int


as

begin

delete from Student where Id=@Id
End
GO





Select project and click ok button. As you press ok a new window will open in this you have to select type of project. select internet application and click ok.


After creating project we will add an entity file in our project. In this entity file we will include our table and Store procedures.

For creating the model file just follow the step.

Step 1:
Right click on you project and select add->New Item. click on new item as window will open.



Now form list select ADO.NET Entity Data Model. Rename the file and click ok. As you click on a new window will open where we have to select the generate db option and click next.




Download SampleCode

No comments:

Post a Comment