Insert, Delete, UpDate records in SQLServer using Enterprise LIbrary in ASP.NET with C#

Insert, Delete, UpDate records in SQLServer using Enterprise LIbrary in ASP.NET with C#

Using Microsoft Enterprise library also we can able to do database operations in C#.net. Please follow below steps for executing sqlcommand using enterprise library concept. This example shows how to execute stored procedures using Microsoft Enterprise Library.

See more Interview Questions for Dotnet People.

STEP 1:
create a stored procedure for inserting employee records. 


create procedure spInsertEmployeeDetails
@empId int, @empName varchar(100),@salary float, @department varchar(50),@location varchar(50)
as
begin
insert into EmployTable values(@empId,@empName,@salary,@department,@location);
end

STEP 2: 

Add reference tot he solution as per image.
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.data.dll


STEP 3: 
use below namespaces.
using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using Microsoft.Practices.EnterpriseLibrary.Data;

using System.Data.Common;

using System.Configuration;

using Microsoft.Practices.EnterpriseLibrary.Data.Sql;





STEP 4: 

give connection string in the web.config file
<connectionstrings>
    <add connectionstring="Data Source=serverName;Initial Catalog=dbname; user id=sa; password=" name="ConnString" providername="System.Data.SqlClient">
  </add></connectionstrings>

STEP 5: 

copy and paste below code into your solution. 
    protected void Page_Load(object sender, EventArgs e)

    {

        UpdateEmployeeRecord(2, "Swati", "200000", "IT", "Bangalore");

    }


    public void UpdateEmployeeRecord(int empId, string empName, string empSalary, string empDept, string empLocation)

    {

        try

        {

            string connectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;

            SqlDatabase db = new SqlDatabase(connectionString);

            string sqlCommand = "spInsertEmployeeDetails";

            DbCommand SaveCommand = db.GetStoredProcCommand(sqlCommand);

            db.AddInParameter(SaveCommand, "@empId", DbType.Int32, empId);

            db.AddInParameter(SaveCommand, "@empName", DbType.String, empName);

            db.AddInParameter(SaveCommand, "@salary", DbType.String, empSalary);

            db.AddInParameter(SaveCommand, "@department", DbType.String, empDept);

            db.AddInParameter(SaveCommand, "@location", DbType.String, empLocation);

            int status = db.ExecuteNonQuery(SaveCommand);

        }

        catch (Exception ex)

        {

            throw ex;

        }

    }

See more ASP.NET, C#, SQL Server Interview Questions


If you have any queries or suggestions, please fell free to ask in comments section.
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar