Here is a simple pattern, using a dataset. I am including the entire class,
to make things easier. This is a bit of a strange pattern in some ways, but
it matches the other methodology of other classes (using table adapters), so
it will make things easier for the Junior devs who might take over the code.
The table mappings here are a bit heavy, but the basic model should work for
you if you use strongly typed datasets. I have more info on why this one is
patterned this way at:
http://gregorybeamer.spaces.live.com/blog/cns!B036196EAF9B34A8!974.entry
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microtrak.UnitWarehouse.Data.DataSets;
namespace Microtrak.UnitWarehouse.Data.Repositories
{
public class UnitInfoRepository
{
private string _connectionString;
public UnitInfoRepository(string connectionString)
{
_connectionString = connectionString;
}
public UnitInfoDS GetUnitInformationByImei(string imei)
{
var ds = new UnitInfoDS();
var connection = new SqlConnection(_connectionString);
var command = new SqlCommand("FillUnitInfoDataSetByIMEI",
connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@IMEI", imei);
//Set table mappings
var da = new SqlDataAdapter(command);
da.TableMappings.Add("Table", "Unit");
da.TableMappings.Add("Table1", "UnitModelType");
da.TableMappings.Add("Table2", "UnitManufacturerType");
da.TableMappings.Add("Table3", "Product");
da.TableMappings.Add("Table4", "ProductLine");
da.TableMappings.Add("Table5", "Project");
da.TableMappings.Add("Table6", "PID");
da.TableMappings.Add("Table7", "FirmwareVersionType");
da.TableMappings.Add("Table8", "APN");
da.TableMappings.Add("Table9", "SIM");
da.TableMappings.Add("Table10", "ShippingCarton");
da.TableMappings.Add("Table11", "Script");
da.TableMappings.Add("Table12", "FriendIpAddressGroup");
da.TableMappings.Add("Table13", "FriendIpAddress");
try
{
connection.Open();
da.Fill(ds);
}
catch (Exception)
{
throw;
}
return ds;
}
}
}
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#
or just read it:
http://feeds.feedburner.com/GregoryBeamer
********************************************
| Think outside the box! |
********************************************