-2

how should i return this command in c# ?

SELECT COUNT(id_pac) AS Expr1, data
FROM     programare
GROUP BY data
private void button1_Click(object sender, EventArgs e)
{
      programareTableAdapter.Cerinta_3(_BD_AtestatDataSet.programare);
      DataTable dt = _BD_AtestatDataSet.programare;
}
  • return this command? Do you mean to execute the command and return the result? This [Read SQL Table into C# DataTable](https://stackoverflow.com/q/6073382/8017690) may helps you. – Yong Shun May 07 '22 at 11:15
  • Try referring to this post for some help: https://stackoverflow.com/questions/3735531/sql-c-best-method-for-executing-a-query – Ibrennan208 May 07 '22 at 11:16
  • @YongShun yes, i mean i want to add in a listboc what it would return when i execute the query in the data set – criss_cross May 07 '22 at 11:21
  • What is database are you trying to execute the SQL in (MSSQL, Oracle, etc.). – Paul Sinnema May 07 '22 at 15:19

1 Answers1

0

Perhaps an introduction to Microsoft's ADO.NET code, the System.Data.SqlClient in particular if you are dealing with SQL Server. A quick example:

 using (SqlConnection conn = new SqlConnection("YourConnectionStringToDatabaseHere"))
            {
                conn.Open();

                // using a command to retrieve active orders
                using (SqlCommand cmd = new SqlCommand("SELECT COUNT(id_pac) AS Expr1, data
FROM     programare
GROUP BY data", conn))
                {
                   
                    // get a sqlreader with the results from our query
                    using (var reader = cmd.ExecuteReader())
                    {
                        // while there is a record to be read
                        while (reader.Read())
                        {
                            int index = 1;
                            var count = reader.GetInt32(index++);
                            var data = reader.GetString(index++);
                         }

                    }
                }
            }

I didn't get to test my code in particular, but it should give you an idea where to start.

Ibrennan208
  • 1,345
  • 3
  • 14
  • 31