public SqlConnection connection; public SqlCommand command; public DataSet Dataset = new DataSet();
private SqlParameter GetParameter(string varName) { FieldInfo field = typeof(Fenetre).GetField(varName, BindingFlags.NonPublic | BindingFlags.Static); // ici typeof(Program) est devenu typeof(Fenetre) où Fenetre est ma classe ou il y a les btn_click toussa if (field == null) throw new ArgumentException("La variable '" + varName + "' n'est pas définie dans la classe Program"); SqlDbType dbType; switch (field.FieldType.FullName) { case ("System.String"): dbType = SqlDbType.VarChar; break; case ("System.Int32"): dbType = SqlDbType.Int; break; case ("System.Double"): dbType = SqlDbType.Float; break; case ("System.DateTime"): dbType = SqlDbType.DateTime; break; default: throw new ArgumentException("Une variable de Type '" + field.FieldType.FullName + "' ne peut pas être converti en SqlDbType"); } SqlParameter parameter = new SqlParameter("@" + varName, dbType); parameter.Value = field.GetValue(null); nb_param++; return parameter; }
public void Execution(string XExec) { try { XExec=XExec.Replace('?', '@'); //-------------------------------TEST - solution 1 - TEST ---------------------------------- parser(XExec); // choppe toutes mes variables ? quelquechose dans un Tabstring sans le ? for (int i = 0; i < nb_string; i++) // créé les parametres { trouve = existe(Tabstring[i].ToString()); if (!trouve) { TabParam[i] = GetParameter(Tabstring[i].ToString()); // créé le param command.Parameters.Add(ChercheParam(Tabstring[i].ToString())); // ajoute le parametre a la commande TableDefini.Add(Tabstring[i].ToString()); } else { command.Parameters.Add(ChercheParam(Tabstring[i].ToString())); // ajoute le parametre a la commande } } //------------------------------ FIN TEST - solution 1 - FIN TEST -------------------------- command.CommandType = CommandType.Text; command.CommandText = XExec; SqlDataReader reader = command.ExecuteReader(); command.Parameters.Clear(); this.Found = reader.HasRows; reader.Close(); //Dataset = RecupDataSet(command); } catch (Exception e) { this.NbErrTransac++; MessageBox.Show(e.ToString()); } }
private void btn_select_Click(object sender, EventArgs e) { LANGAGE2SQL L_LANGAGE2SQL = new LANGAGE2SQL(); string wheretmp=""; if (textBox2.Text != "" && comboBox1.SelectedItem.ToString() != "") { wheretmp = comboBox1.SelectedItem.ToString() + "=?G_Etendue"; if (textBox1.Text != "" && comboBox2.SelectedItem.ToString() != "") wheretmp = wheretmp + comboBox2.SelectedItem.ToString() + "=?G_CAction"; G_Etendue = Convert.ToInt32(textBox2.Text); G_CAction = (textBox2.Text); L_LANGAGE2SQL.RAZ(); L_LANGAGE2SQL.Connect(); L_LANGAGE2SQL.CSecteur = ""; L_LANGAGE2SQL.EtatVal = 3; L_LANGAGE2SQL.Type = "SELECT"; L_LANGAGE2SQL.Select = "*"; L_LANGAGE2SQL.From = "[DBSoft].[dbo].[DateAff]"; L_LANGAGE2SQL.Where = wheretmp; L_LANGAGE2SQL.OrderBy = ""; L_LANGAGE2SQL.GroupBy = ""; L_LANGAGE2SQL.IntoDbf = "T_POIN"; L_LANGAGE2SQL.Execute(); DataSet dataset1 = new DataSet(); dataset1 = (L_LANGAGE2SQL.RecupDataSet(L_LANGAGE2SQL.command)); dataGridView1.DataSource = dataset1; dataGridView1.DataMember = "Table"; dataGridView1.Columns["PKDateAff"].ReadOnly = true; L_LANGAGE2SQL.DisConnect(); Label_test.Text = "SELECTFAIT"; } else { L_LANGAGE2SQL.RAZ(); L_LANGAGE2SQL.Connect(); L_LANGAGE2SQL.CSecteur = ""; L_LANGAGE2SQL.EtatVal = 3; L_LANGAGE2SQL.Type = "SELECT"; L_LANGAGE2SQL.Select = "*"; L_LANGAGE2SQL.From = "[DBSoft].[dbo].[DateAff]"; L_LANGAGE2SQL.Where = ""; L_LANGAGE2SQL.OrderBy = ""; L_LANGAGE2SQL.GroupBy = ""; L_LANGAGE2SQL.IntoDbf = "T_POIN"; L_LANGAGE2SQL.Execute(); DataSet dataset1 = new DataSet(); dataset1 = (L_LANGAGE2SQL.RecupDataSet(L_LANGAGE2SQL.command)); dataGridView1.DataSource = dataset1; dataGridView1.DataMember = "Table"; dataGridView1.Columns["PKDateAff"].ReadOnly = true; L_LANGAGE2SQL.DisConnect(); } }
public DataSet RecupDataSet(SqlCommand command) { SqlDataAdapter adapter = new SqlDataAdapter(); command.CommandType = CommandType.Text; // Set the SqlDataAdapter's SelectCommand. adapter.SelectCommand = command; // Fill the DataSet. DataSet DataSetmp = new DataSet(); adapter.Fill(DataSetmp,"Table"); <=== l'erreur indique qu'une exeption n'est pas gérée ici return DataSetmp; }
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication2 { static class Program { /// <summary> /// Point d'entrée principal de l'application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Fenetre()); } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question