programmernovice
programmernovice

Reputation: 3941

How to return single sql result in a string without using datatable (C#)?

I know how to fill a datatable with dAdapter.Fill(dTable) using System.Data.OleDb

But it's heavy if I just want to retrieve a single string value like "select name from table where idperson = 1"

Can't I bypass creating a DataTable ?

I want to create an equivalent of dlookup function in MS Access.

Upvotes: 3

Views: 966

Answers (1)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421998

using (var conn = new OleDbConnection(...))
using (var cmd = new OleDBCommand("select ...", conn)) {
   conn.Open();
   object result = cmd.ExecuteScalar(); // cast to appropriate type
   conn.Close();
}

Upvotes: 14

Related Questions