It seems very Easy dear....
public int Insert(string Query, SortedList<string, object> parameter = null)
{
_cmd = new SqlCommand(Query, _con);
if (parameter != null)
{
for (int l = 0; l < parameter.Count; l++)
{
_cmd.Parameters.AddWithValue(parameter.Keys[l], parameter.Values[l]);
}
}
int _rowAffected;
try
{
if (_con.State == ConnectionState.Closed)
_con.Open();
_rowAffected = _cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (_con.State == ConnectionState.Open)
_con.Close();
}
return _rowAffected;
}
Now, Invoke this method as following...
Collapse
var _sl = new SortedList<string, object>();
_sl.Add("@firstName", firstName);
_sl.Add("@lastName", lastName);
_sl.Add("@age", age);
int _affectedRow = obj.Insert("INSERT INTO tablename VALUES('','','','')",_sl);
Thanks & Regards,
Punit
ok dear then you should pass object of SqlCommand, and in the above fuction you have catch that object like this...
public int Insert(SqlCommand cmd)
{
cmd.Connection = con;
int _rowAffected;
try
{
if (_con.State == ConnectionState.Closed)
_con.Open();
_rowAffected = _cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (_con.State == ConnectionState.Open)
_con.Close();
}
return _rowAffected;
}
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandText = "something";
cmd.AddWithValue("@firstName", firstName);
cmd.AddWithValue("@lastName", lastName);
cmd.AddWithValue("@age", age);
int _affectedRow = obj.Insert(cmd);