mvc crud operation without entity framework using ajax code example
Example 1: crud operation without entity framework in mvc
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Configuration; namespace CRUDinMVC.Models{ public class StudentDBHandle { private SqlConnection con; private void connection() { string constring = ConfigurationManager.ConnectionStrings["studentconn"].ToString(); con = new SqlConnection(constring); } // **************** ADD NEW STUDENT ********************* public bool AddStudent(StudentModel smodel) { connection(); SqlCommand cmd = new SqlCommand("AddNewStudent", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Name", smodel.Name); cmd.Parameters.AddWithValue("@City", smodel.City); cmd.Parameters.AddWithValue("@Address", smodel.Address); con.Open(); int i = cmd.ExecuteNonQuery(); con.Close(); if (i >= 1) return true; else return false; } // ********** VIEW STUDENT DETAILS ******************** public List<StudentModel> GetStudent() { connection(); List<StudentModel> studentlist = new List<StudentModel>(); SqlCommand cmd = new SqlCommand("GetStudentDetails", con); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter sd = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); con.Open(); sd.Fill(dt); con.Close(); foreach(DataRow dr in dt.Rows) { studentlist.Add( new StudentModel { Id = Convert.ToInt32(dr["Id"]), Name = Convert.ToString(dr["Name"]), City = Convert.ToString(dr["City"]), Address = Convert.ToString(dr["Address"]) }); } return studentlist; } // ***************** UPDATE STUDENT DETAILS ********************* public bool UpdateDetails(StudentModel smodel) { connection(); SqlCommand cmd = new SqlCommand("UpdateStudentDetails", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@StdId", smodel.Id); cmd.Parameters.AddWithValue("@Name", smodel.Name); cmd.Parameters.AddWithValue("@City", smodel.City); cmd.Parameters.AddWithValue("@Address", smodel.Address); con.Open(); int i = cmd.ExecuteNonQuery(); con.Close(); if (i >= 1) return true; else return false; } // ********************** DELETE STUDENT DETAILS ******************* public bool DeleteStudent(int id) { connection(); SqlCommand cmd = new SqlCommand("DeleteStudent", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@StdId", id); con.Open(); int i = cmd.ExecuteNonQuery(); con.Close(); if (i >= 1) return true; else return false; } }}
Example 2: crud operation without entity framework in mvc
Create procedure [dbo].[UpdateStudentDetails] ( @StdId int, @Name nvarchar (50), @City nvarchar (50), @Address nvarchar (100) ) as begin Update StudentReg set Name=@Name, City=@City, Address=@Address where Id=@StdId End
Example 3: crud operation without entity framework in mvc
<connectionStrings> <add name="StudentConn" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True;Pooling=False"/> </connectionStrings>