Efficient Syntax for Filling DataTable from MS Access

Hi All,

Just looking for advice on whether the following code could be made more efficient/better?..

Basically it connects to Access DB fills a DataTable and then returns it to the calling code. I haven't included error trapping within this method but rather wrppaing the error tradding around the calling code.


public static DataTable GetUserDetails(string strFirstName, string strLastName)
        string ConnString = GetConnString();
        string SqlString = "Select * From Contacts Where FirstName = ? And LastName = ?";
        OleDbDataAdapter adptUserData = new OleDbDataAdapter();
        DataTable dtUserData = new DataTable();

        using (OleDbConnection conn = new OleDbConnection(ConnString))
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("FirstName", strFirstName);
                cmd.Parameters.AddWithValue("LastName", strLastName);

                adptUserData.SelectCommand = cmd;



	return dtUserData;

Open in new window

Who is Participating?
radcaesarConnect With a Mentor Commented:
Just return your datareader object

and use while <datareaderObject>.read() methos to navigate it (To get values)

while (reader.Read())
                        reader[0], reader[1]);
If you are going to only read the data, then use DataReader instead of dataset since it will be faster and use memory.

IF you are going to do some validations on data in the dataset the yours is good to go.
rito1Author Commented:
thanks radcaesar,

The DataReader would be good in this scenario... My only question then is how would I pass the DataReader as a return value?

Many thanks,

rito1Author Commented:
Excellent thank you.

I have been using SubSonic Project for the past 2 years to build my DAL so my ADO has become very rusty!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.