The way I prefer to do it is to create an method that accepts an IDataReader, and let that do the DataMapping/Serialization/Hydrating.
That allows the IDataReader to be created outside the code (Jet or Sql Server or Other), and then reused.
I also don't like "0", "1", etc.
Here is my typical IDataReader/DataMapper code. (See bigger code block later in this post)
As far as your original question, most DataTypes will match up.
However, I remember a few times between Jet and SqlServer, the datatypes were slightly off.
The workaround was use the GetValue(), and the cast it.
Instead of this:
decimal pubPrice = dataReader.GetDecimal(Layout.COLUMN0);
It ended up being like this:
decimal pubPrice = Convert.ToDecimal (accessDataReader.GetValue (Layout.COLUMN0));
Again, I was testing against an IDataReader being created against an OleDB (Jet/Access) vs Sql Server. I think I was testing with the pubs database, both the Jet(Access) version and the corresponding Sql Server version.
Most times it was fine. But once in a while I had to tweak and use the less "specific" "GetValue()" method, over the concrete getter (GetDecimal() for example).
[Serializable]
public partial class Employee
{
public int EmployeeKey { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime HireDate { get; set; }
}
[Serializable]
public class EmployeeCollection : List<Employee>
{
}
internal static class EmployeeSearchResultsLayouts
{
public static readonly int EMPLOYEE_KEY = 0;
public static readonly int LAST_NAME = 1;
public static readonly int FIRST_NAME = 2;
public static readonly int HIRE_DATE = 3;
}
public EmployeeCollection SerializeEmployeeSearchForCollection(IDataReader dataReader)
{
Employee item = new Employee();
EmployeeCollection returnCollection = new EmployeeCollection();
try
{
int fc = dataReader.FieldCount;//just an FYI value
int counter = 0;//just an fyi of the number of rows
while (dataReader.Read())
{
if (!(dataReader.IsDBNull(EmployeeSearchResultsLayouts.EMPLOYEE_KEY)))
{
item = new Employee() { EmployeeKey = dataReader.GetInt32(EmployeeSearchResultsLayouts.EMPLOYEE_KEY) };
if (!(dataReader.IsDBNull(EmployeeSearchResultsLayouts.LAST_NAME)))
{
item.LastName = dataReader.GetString(EmployeeSearchResultsLayouts.LAST_NAME);
}
if (!(dataReader.IsDBNull(EmployeeSearchResultsLayouts.FIRST_NAME)))
{
item.FirstName = dataReader.GetString(EmployeeSearchResultsLayouts.FIRST_NAME);
}
if (!(dataReader.IsDBNull(EmployeeSearchResultsLayouts.HIRE_DATE)))
{
item.HireDate = dataReader.GetDateTime(EmployeeSearchResultsLayouts.HIRE_DATE);
}
returnCollection.Add(item);
}
counter++;
}
return returnCollection;
}
//no catch here... see http://blogs.msdn.com/brada/archive/2004/12/03/274718.aspx
finally
{
if (!((dataReader == null)))
{
try
{
dataReader.Close();
}
catch
{
}
}
}
}