I’ve got a SqlConnection with a sqlquery that may not bring any results. On the rare occasion this happens, I have another sqlquery and another connection to the database. There has to be a better way to do this, no?
On a side note, is this best to close the connection after .Fill? I assume so, but haven’t seen it used anywhere.
I’m still getting started in C#/.Net — Thanks!
SqlConnection dbSqlConnection = new SqlConnection(--);
SqlDataAdapter dbSqlDataAdapter = new SqlDataAdapter(sqlquery, dbSqlConnection);
DataSet dbDataSet = new DataSet();
dbSqlDataAdapter.Fill(dbDataSet, "popGrid");
dbSqlConnection.Close();
if (dbDataSet.Tables["popGrid"].Rows.Count == 0)
{
SqlDataAdapter newSqlDataAdapter = new SqlDataAdapter(sqlquery2, dbSqlConnection);
newSqlDataAdapter.Fill(dbDataSet, "popGrid");
dbSqlConnection.Close();
For the first question, I think it’s also ok if you connect to the DB twice after check the first result is empty. You don’t have to make the two into one single query, unless this action(fetch data twice) is super frequent, or connecting to DB costs much time(e.g. you are in US and the DB is in UK,it’s possible,right?)
And for the second question: