ahmedsalah
Active member
- Joined
- Sep 26, 2018
- Messages
- 32
- Programming Experience
- 3-5
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 .
so I do function above for make insert or update or delete
what is remaining or wrong to be best practice ?
or update or delete but I don't know what must added or remove from function below to make function work as best practice .
C#:
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[i]);
}
}
try
{
await con.OpenAsync();
RecordsCount = (await cmd.ExecuteNonQueryAsync());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
});
return RecordsCount;
}
what is remaining or wrong to be best practice ?
Last edited by a moderator: