ExecuteNonQuery returning -1 when using sql COUNT despite the query string

后端 未结 2 1358
粉色の甜心
粉色の甜心 2020-11-29 12:55

For some reason, ExecuteNonQuery() in C# returns -1, though when I run a query separately, the value returns the actual value needed.

For E

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 13:25

    You can use Ef core with Ado.net like this example

    var context = new SampleDbContext();
    using (var connection = context.Database.GetDbConnection())
    {
        connection.Open();
    
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT COUNT(*) FROM SomeTable";
            var result = command.ExecuteScalar().ToString();
        }
    }
    

提交回复
热议问题