How is wrong on function ExecuteNonQuery to work as Best Practise ?

  • Thread starter Thread starter engahmedbarbary
  • Start date Start date
E

engahmedbarbary

Guest
I work on c# app I need to make function make insert data or update or delete dynamically so that I do function below for insert

or update or delete but I don't know what must added or remove from function below to make function work as best practice .

public static async Task<int> ExecuteNonQuery(string sql, SqlConnection sqlconnection, DbParameter[] @params = null, CommandType cmdType = CommandType.StoredProcedure)
{
int RecordsCount = 0;


if (sql == "") return 0;
await Task.Run(async () =>
{
using (var con = new SqlConnection(GlobalVariables.con))
{
using (var cmd = new SqlCommand() { Connection = con })
{


if (cmd.CommandTimeout < 360)
cmd.CommandTimeout = 360;
cmd.CommandText = sql;
cmd.CommandType = cmdType;
cmd.Parameters.Clear();
if (@params != null)
{
for (int i = 0; i < @params.Length; i++)
{
cmd.Parameters.Add(@params);
}
}
try
{
await con.OpenAsync();

RecordsCount = (await cmd.ExecuteNonQueryAsync());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

}


});
return RecordsCount;
}

so I do function above for make insert or update or delete

what is remaining or wrong to be best practice ?

Continue reading...
 
Back
Top