I have a SQL
query which supposed
to return only ONE
row from the business database. Based on this, I have written following sql script
to get the data from the result set.
string query = @"select
ProdMaster.data_Id Id,
ProdMaster.data_name Name,
ProdMaster.data_countryname CountryName
from RM.Db
order by ProdMaster.data.FromDate desc"
SqlCommand command = new SqlCommand(query, conn);
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
countryname = reader["CountryName"].ToString();
}
}
But, there is some data issue in the database, sometimes it returns multiple rows.
How do we check the row count
? If rows more than one we want to return a custom exception.
Note:
- I do not want to use COUNT(*) in the query.
- We don't have control on
RM.Db
database - it might have data issues (3rd party)