Comment puis-je changer cela:
using (SqlCommand myCommand = myConnection.CreateCommand()) { myConnection.Open(); myCommand.CommandText = "SELECT FormID FROM tbl_Form"; using (SqlDataReader reader = myCommand.ExecuteReader()) { while (reader.Read()) { int FormID = reader.GetInt32(reader.GetOrdinal("FormID")); MessageBox.Show(FormID.ToSsortingng()); } } }
obtenir MAX (FormID) ?
Ma tendance naturelle est de lancer un MAX autour de FormID mais je reçois une exception IndexOutOfRange.
Lorsque vous sélectionnez l’identifiant maximal, vous ne devez pas utiliser SqlDataReader
– la requête ne renvoie qu’un élément, qui par défaut n’a pas de nom. Par conséquent, votre requête existante se rompt car elle attend un résultat nommé “FormID” – bien que vous ayez “corrigé” votre requête en utilisant "SELECT MAX(FormID) as FormId FROM tbl_Form"
. Utilisez plutôt ExecuteScalar()
:
myCommand.CommandText = "SELECT MAX(FormID) FROM tbl_Form"; int maxId = Convert.ToInt32(myCommand.ExecuteScalar());
Cela fait longtemps que je n’utilise pas ce type d’access à la firebase database, mais je pense que vous avez juste besoin de
select max(FormID) from tbl_Form
avec un appel à ExecuteScalar