SqlCommand
-
hi where i should use SqlCommand.Parameters.Add()method and where SqlCommand.Parameter.AddWithValue()? is there performance diffrence?
-
hi where i should use SqlCommand.Parameters.Add()method and where SqlCommand.Parameter.AddWithValue()? is there performance diffrence?
Hi, Specially parameters used to pass data to ur query , as inset update delete even select
select *
from t1
where id = 12this query always returns a fixed result, now going to make it gloabal, i mean change the 12 value to needed value, so u use parameters as:
select *
from t1
where id=@idnow in command line, u must set the parameter, look at this method as a sample:
private DataTable SelectParameter(int idvalue, bool useWithvalue) { string constr = ""; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand("select \* from t1 where id=@id", con); if (useWithvalue) { cmd.Parameters.AddWithValue("@id", idvalue); } else { SqlParameter p = new SqlParameter("@id", SqlDbType.Int); p.Value = idvalue; cmd.Parameters.Add(p); } con.Open(); DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader()); return dt; }
in cases that u are going to set the value type of parameter u should use add else u could use both of them.
-
hi where i should use SqlCommand.Parameters.Add()method and where SqlCommand.Parameter.AddWithValue()? is there performance diffrence?
I hear that
AddWithValue
is kinda safer, but I rarely use it because of the way I do things. It's not a performance issue. -
I hear that
AddWithValue
is kinda safer, but I rarely use it because of the way I do things. It's not a performance issue.