Esempio n. 1
0
        /// <summary>
        /// Occurs when a search is requested from the preset view.
        /// </summary>
        private void Adapter_Search(object sender, EventArgs e)
        {
            // For this example, we're just getting results of the first subject in our configuration
            var target = (ISqlSubject)_config[0];

            // Instantiate an SqlGenerator which will be used to create our SQL
            // (the following fluent calls can be called in any order before the UpdateCommand function is invoked)
            var generator = new SqlGenerator(_config)

                            // Call fluent method to add a collection of columns to get
                            .Column(new FieldPathFactory().GetFields(target))

                            // Set what subject we're primarily searching for
                            .ForTarget(target)

                            // The most important part: adding the user provided parameters.
                            // Since we initialised the PresetAdapter with a ParameterBuilderFactory from the dbqf.Sql
                            // namespace, we know the generated IParameter returned from _adapter.GetParameter() will
                            // be of the correct ISqlParameter types
                            .WithWhere((ISqlParameter)_adapter.GetParameter());

            // Since we're creating our application using MSSQL, we'll use the SqlClient objects
            using (var conn = new SqlConnection())
            {
                using (var cmd = conn.CreateCommand())
                {
                    // Finally the generator will update our command object with the correct parameterized SQL
                    generator.UpdateCommand(cmd);
                    Console.WriteLine(cmd.CommandText);

                    using (var sda = new SqlDataAdapter(cmd))
                    {
                        // Use a vanilla DataTable bound to the DataGridView
                        var table = new DataTable();
                        dataGridView1.DataSource = table;

                        // This call would finally get our results
                        // (but since this is an example we have no database to query)
                        //sda.Fill(table);

                        // Pretend to get results
                        Fill(table, target);
                    }
                }
            }
        }