Why are you using a string for the ExportTimeStamp parameter? using DateTime if it is a date or DateTime .
I would also replace all of your calls with AddWithValue with Add . When you call AddWithValue , it should guess what type of your parameter is. If it is mistaken, the optimizer cannot select the correct index and returns to scanning the table, and this speaks of the database performance core.
AddWithVaue can result in several requests. Since .NET does not know what the size of the database column is, it will use the size of the variable. therefore, if you have a parameterized query and pass two lines in one of lengths 10, the other length 20, you will get two plans: @text nvarchar(10) and @text nvarchar(20) . It will also assume that your field is nvarchar when it can be varchar and you will get an implicit conversion.
Therefore, always either pass the correct type to AddWithValue , or (better) use SqlParameterCollection.Add with the correct type and size. It will also check the parameter before sending it to the database.
Connected:
Also, use using -statement to make sure the connection closes as soon as you finish with it, even in the event of an error.
Here is an example:
public DataTable GetDataTable(int Year, int month, string datatype) { DataTable myDataTable = new DataTable(); String ConnString = ConfigurationManager.ConnectionStrings["IHG_MSTConnectionString"].ConnectionString; using(SqlConnection conn = new SqlConnection(ConnString)) using (SqlDataAdapter adapter = new SqlDataAdapter()) { var cmd = new SqlCommand("[Yield_Planner_With_Strategy]", conn); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add("@Holidex_Code", SqlDbType.Int).Value = int.Parse(RadComboBox_Hotels.SelectedValue); cmd.Parameters.Add("@Event_Year", SqlDbType.Int).Value = Year; cmd.Parameters.Add("@Event_Month", SqlDbType.Int).Value = month; cmd.Parameters.Add("@DataType", SqlDbType.VarChar).Value = datatype; cmd.Parameters.Add("@MktSeg", SqlDbType.NVarChar).Value = Fruitful.Get_Checked_Values_As_CSV(RadComboBox_MktSeg); DateTime exportdate = DateTime.Now; if (RadComboBox_ExportTimeStamp.Text != "" && RadComboBox_ExportTimeStamp.Text != "Create New Strategy") { exportdate = DateTime.Parse(RadComboBox_ExportTimeStamp.Text); } cmd.Parameters.Add("@ExportTimeStamp", SqlDbType.DateTime).Value = exportdate; adapter.SelectCommand = cmd;
source share