Followers

Friday, September 17, 2010

Using Store Procedure How to Insert/store data into database table

**Using Store Procedure How to Insert/store data into database table.
** In Database section create the following store procedure.
    
   -- =============================================
-- Author: Amin Uddin
-- Create date: 05/09/2010
-- Description: This store procedure used to save/store/ Insert all data of EmployeeInformation Table
-- =============================================
Create PROC InsertEmployeeInformation
        (
           @EmployeeId varchar(50),
           @EmployeeName varchar(50),
           @Salary decimal(18, 0),
           @JoiningDate datetime
        )
     asBegin 
      if not exists(select employeeId from EmployeeInformation where employeeId = @EmployeeId)        
          Begin
               Insert Into EmployeeInformation values(@EmployeeId,@EmployeeName,@Salary,@JoiningDate)
              return 1     

         End 
    else    
        Begin
         return 0  

        End
End

*********************************************************************************

** In C#.net Portion


public int InsetEmployeeInformation(Employee employee)
{
SqlConnection con = new SqlConnection();
SqlDataAdapter da = new SqlDataAdapter();

CommandType.StoredProcedure;
da.SelectCommand.CommandText = "InsertEmployeeInformation";
da.SelectCommand.CommandType = System.Data.
da.SelectCommand.Connection = con;
da.SelectCommand.Parameters.Add("@EmployeeId",System.Data.SqlDbType.VarChar, 50).Value = employee.EId;
da.SelectCommand.Parameters.Add("@EmployeeName", System.Data.SqlDbType.VarChar, 50).Value = employee.EName;
da.SelectCommand.Parameters.Add("@Salary", System.Data.SqlDbType.Decimal).Value = employee.Salary;
da.SelectCommand.Parameters.Add("@JoiningDate", System.Data.SqlDbType.Date).Value = employee.JoiningDate;
if (con.State == ConnectionState.Closed)
{
con.Open();
}

{

}
if (da.SelectCommand.ExecuteNonQuery() == 1)
{
return 1;
}
else
{
return 0;
}
if (con.State == ConnectionState.Open)
{con.Close();

}
}

No comments:

Post a Comment