There are two tables, one contains data about the experiment, information about the selected keyboard is added to another (the Nklav field of type number is either 1 or 2). In the form with the results, each experiment has a textbox in which I enter the data on the keyboard selected in this experiment, this is how I do it

SqlConnection ThisConnection = new SqlConnection(@"Data Source=STAS-ПК \SQLSERVER2014;Initial Catalog= SMSensomotorica2db; User ID=sa;Password= novell"); ThisConnection.Open(); SqlCommand thisCommand = ThisConnection.CreateCommand(); thisCommand.CommandText = "SELECT top 1 Nklav FROM nomerKlava ORDER BY ind desc"; SqlDataReader thisReader = thisCommand.ExecuteReader(); string res = string.Empty; while (thisReader.Read()) { res += thisReader["Nklav"]; } thisReader.Close(); ThisConnection.Close(); textBox1.Text = res; 

Question

however, every time I perform a new experiment, it is the last selected keyboard that is displayed in all the results. How to add a parameter with an experiment ID to the query?

 (id_exp = @curr_id_exp) 
  • Of course, the last keyboard is displayed, because that is what you are asking in the request. For example, try this: thisCommand.Parameters. AddWithValue ("id_exp", idExp) and add the condition "where id_exp = @id_exp" to the request text - Aleksandr Zharinov
  • Thank you very much, everything turned out! - Stan
  • @AleksandrZharinov Please post your comment as a response. - Nicolas Chabanovsky
  • @NicolasChabanovsky I'm not sure that this was a complete question. But if you insist. - Aleksandr Zharinov
  • @AleksandrZharinov Thank you very much! - Nicolas Chabanovsky

1 answer 1

To add a parameter to a query, you must change the query text by adding the condition "where id_exp = @id_exp" to it. In addition, you need to add a parameter to the command:

 thisCommand.Parameters.AddWithValue("id_exp", idExp) 

Do not forget to pre-declare and fill in the idExp variable with the necessary data.