• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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.

Thanks,

Rit
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);
                conn.Open();

                adptUserData.SelectCommand = cmd;
                adptUserData.Fill(dtUserData);

            }

        }

	return dtUserData;
    }

Open in new window

0
rito1
Asked:
rito1
  • 2
  • 2
1 Solution
 
radcaesarCommented:
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.
0
 
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,

Rit
0
 
radcaesarCommented:
Just return your datareader object

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

while (reader.Read())
                {
                    Console.WriteLine("\t{0}\t{1}",
                        reader[0], reader[1]);
                }
0
 
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!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now