Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi All,

How can we executing Stored procedure from coding.

I have used two ways .

1. By directly executing like in sqlserver

eg:
C#
exec spname '+ txt1.Text+','+ txt2.Text+'

2. by using sql parameters

eg:
C#
SqlParameter[] param = new SqlParameter[1];
        param[0] = new SqlParameter("@value1", txt1.Text);
        param[1] = new SqlParameter("@value2", txt2.Text);
        DataTable objDataTable = new DataTable();
        SqlConnection objConnection = new SqlConnection(sConnString);
            objConnection.Open();
            SqlCommand objCommand = new SqlCommand("spname", objConnection) { CommandType = CommandType.StoredProcedure };
            objCommand.Parameters.AddRange(param);
            objCommand.CommandTimeout = 0;
            SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
            objDataAdapter.Fill(objDataTable);




which is the best way ? is it have any performance issue and why?



Regards,
Pal.
Posted

1 solution

They aren't comparable.
The first way can only be executed on the SQL server, the second can only be executed on your application host pc. Unless the two are the same machine, the performance comparison is irrelevant.

And the second example effectively causes the first to be executed on the SQL server anyway.
 
Share this answer
 
Comments
AmitGajjar 20-Jun-12 2:36am    
5+ correct

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900