Run SQL Querythat returns 1 value

  • Thread starter Thread starter sippyuconn
  • Start date Start date
S

sippyuconn

Hi

Is there an more straightforward way of getting single value from a SQL
Query besides going to table and getting first column of first row ???

This is assuming that you know that you should only expect 1 value returned

Thanks

public static string GetGDesc()
{

string strCon;
string strSql;
OleDb.OleDbConnection oleCon = new OleDbConnection();
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=d:\My
Documents\TextFiles;Extended Properties=Text;"

strSql = "Select MyKey from MyTable WHERE MyName ='Steve'";
oleCon.ConnectionString = strCon;
oleCon.Open();

OleDbDataAdapter odaXL = new OleDbDataAdapter(strSql, oleCon);
odaXL.SelectCommand.ExecuteNonQuery();

DataSet ds = new DataSet("TextFiles");


objCon.Close();

if (ds.Tables[0].Rows.Count == 1)
{
sDesc = ds.Tables[0].Rows[0][0].ToString();
}

return (sDesc);
}
 
Look at the OleDbCommand.ExecuteScalar Method (or DbCommand.ExecuteScalar
Method in gerneral).

Hope it helps

Boaz Ben-Porat
Milestone Systems A/S
 
Back
Top