What is the C # equivalent of the "PHP sql_fetchrow" function and the following code?

رضا جافری 1,296 Reputation points
2022-04-01T10:41:42.62+00:00

The following PHP code needs to be converted to C#.
Is there anyone who can provide me with a response in the form of output?
189123-php.jpg

Thanks

Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Nataniel Marcial Chavez 1 Reputation point
    2022-04-04T03:59:13.94+00:00

    if you want to consume some sql database you can use the different objects into System.Data name space
    for example to retrieve some data you can use a sqlcommand and call the execute reader method its similar to use sql_fetchrow

    command.ExecuteReader

    example:
    private static void ReadOrderData(string connectionString)
    {
    string queryString =
    "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
    connectionString))
    {
    SqlCommand command = new SqlCommand(
    queryString, connection);
    connection.Open();
    using(SqlDataReader reader = command.ExecuteReader())
    {
    while (reader.Read())
    {
    Console.WriteLine(String.Format("{0}, {1}",
    reader[0], reader[1]));
    }
    }
    }
    }

    please check the following documentatio for more info https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand?view=dotnet-plat-ext-6.0

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.