Torna al Thread

List<string> elPersone = new List<string>(); elPersone.Add("Marco"); elPersone.Add("Cristina"); List<string> elencoRicette = new List<string>(); elencoRicertte = recuperaRecord(elPersone); ----------------------------------------------------------------- public List<string> recuperaRecord(List<string> elencoPersone){ List<string> listaRicette = new List<string>(); string strSQL = creaSQL(elencoPersone); if(strSQL == "errore"){ //Se non è inserita neanche una persona non vale la pena fare la ricerca e restituisce la lista vuota return new List<string>(); } SqlConnection sqlConnection1 = new SqlConnection(@"Data Source=(LocalDB)\v12.0;AttachDbFilename= C:\Users\Giovanni Camera\Documents\Visual Studio 2015\Projects\WindowsFormsApplication7\WindowsFormsApplication7\prova.MDF; Integrated Security = True"); SqlCommand comm = new SqlCommand(strSQL, sqlConnection1); SqlDataAdapter da = new SqlDataAdapter(comm); DataTable dt = new DataTable(); try { sqlConnection1.Open(); da.Fill(dt); foreach (DataRow row in dt.Rows) { listaRicette.Add(Convert.ToString(row["NomeRicette"])); } sqlConnection1.Close(); return listaRicette; } catch (Exception ex) { textBox2.Text = ex.Message; MessageBox.Show(ex.Message); return new List<string>(); } } public string creaSQL(List<string> elencoPersone){ string strSQL = "SELECT NomeRicette FROM Prova WHERE "; if(elencoPersone == null){ return "errore"; } //Se è solo una persona if(elencoPersone.Count == 1){ return strSQL + elencoPersone.First() + " = 'Y' "; } //altrimenti sono più di una persona //Inserisco la prima persona strSQL = strSQL + elencoPersone.First() + " = 'Y'"; //ciclo dalla seconda in poi for (int i = 1; i < elencoPersone.Count-1; i++){ strSQL = addPersonSQL(strSQL , elencoPersone[i]); } return strSQL; } public string addPersonSQL (string sqlOld, string strNomePersona){ return sqlOld + " AND " + strNomePersona + " = 'Y' "; }
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5