First of all I will create Stored procedure and I will update any of my table through procedure.
So, Basic SP will be like below, and I will run it in a Database(SQL).
CREATE PROCEDURE Set_LogRecords_TotalHours
-- Add the parameters for the stored procedure here
@timeIn datetime,
@timeOut datetime
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
UPDATE LogRecords
SET TotalHours = DATEDIFF(HOUR, @timeIn, @timeOut)
-- returns number of rows
SELECT @@ROWCOUNT
END
GO
Now, I will Move to code side.
I will Create a Generic method to call All of mine Stored procedures, see below.
public static DataSet GetRecordWithExtendedTimeOut(string SPName, params SqlParameter[] SqlPrms)
{
DataSet ds = new DataSet();
try
{
//here give reference of your connection and that is "openCon"
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(SPName, conn))
{
command.Parameters.AddRange(SqlPrms);
command.CommandTimeout = 0;
conn.Open();
command.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
{
try
{
dataAdapter.SelectCommand = command;
dataAdapter.Fill(ds);
}
catch (Exception ex)
{
return null;
}
finally
{
conn.Close();
}
}
}
}
}
catch (Exception ex)
{
//Handle Errror
}
return ds;
}
Now at last, call this method from wherever you need to access database.
Over here is the example of calling generic method.
//Add all the parameter that you want to pass to SP, here we have 2 and they are in DAteTime Formate
SqlParameter[] parameters =
{
new SqlParameter { ParameterName = "@timeIn", Value = ValueOf_TimeIN_DateTIME }
new SqlParameter { ParameterName = "@timeOut", Value = ValueOf_TimeOUT__DateTIME}
};
DataSet ds = DAL.GetRecordWithExtendedTimeOut("Set_LogRecords_TotalHours", parameters);
if (ds != null && ds.Tables.Count >= 1 && ds.Tables[0].Rows.Count >= 1)
{
//Debugg ds and you will see the number of records that affected in last update
}