I have a program that gets data from SQL server table. The code is the following:
SqlConnection conn=new SqlConnection(...);//correct
conn.Open();
DataTable dt=new DataTable();
SqlCommand selectCMD = new SqlCommand("SELECT * FROM TABLE WHERE Condition", conn);
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
custDA.Fill(dt);
Datagridview1.DataSource=dt;
Datagridview1.DataBind();
But the problem is that when executing the same query in SQL server management studio, it takes less then second to execute. Meanwhile when using program, it takes half a minute to get the result. Using debugger I see, that the main row, where program “thinks” a lot time is when data adapter is filling DataTable. Any suggestions how to reduce the time? What’s wrong in my code?
Thank Everyone for help. I used parameters in
SqlCommandobject. Unfortunately I haven’t mentioned that so you couldn’t help me. But as James posted a link, I have found that whenSqlCommandis uded with parameters then the execution is made using stored proceduresp_execute. Because server has to compile it, that’s why it takes so long. After parameters were removed, everything is working good. The other way is that you can turn off automatic recomplilation each time stored procedure executes. Thanks everyone again. +1 for everyone.