I have to ask, why not just use a has-a instead of an is-a solution
for this? Now, your Customer can have all fields and methods static.
//***********************
public class DataContainer
{
protected static readonly string credentials =
"server=localhost;Initial Catalog=Northwind;uid=sa;pwd=";
protected SqlConnection con = new SqlConnection(credentials);
protected SqlDataAdapter da = new SqlDataAdapter();
protected DataSet ds = new DataSet();
public SqlDataAdapter DA
{
get
{
return da;
}
}
public DataSet DS
{
get
{
return ds;
}
}
public SqlConnection Con
{
get
{
return con;
}
}
Quote:
}
//***********************
public class Customer
{
static DataContainer dataCon;
static SqlCommand cmdSelect = null;
static Customer()
{
dataCon = new DataContainer();
cmdSelect = new SqlCommand("SELECT CustomerID, CompanyName FROM
Customers", dataCon.Con);
cmdSelect.CommandTimeout = 30;
dataCon.DA.SelectCommand = cmdSelect;
}
public static DataSet GetCustomers()
{
dataCon.Con.Open();
dataCon.DA.Fill(dataCon.DS, "Customers");
dataCon.Con.Close();
return dataCon.DS;
}
}
//***********************
public class Test
{
public static void Main()
{
DataSet ds = Customer.GetCustomers();
}
Quote:
}
Quote:
> Here is my solution -- I invite all to improve upon it.
> Thanks,
> Mark
> using System;
> using System.Data;
> using System.Data.SqlClient;
> namespace Test
> {
> public abstract class BaseSingleton
> {
> protected static BaseSingleton instance;
> protected static readonly string credentials =
"server=localhost;Initial Catalog=Northwind;uid=sa;pwd=";
Quote:
> protected SqlConnection con = new SqlConnection(credentials);
> protected SqlDataAdapter da = new SqlDataAdapter();
> protected DataSet ds = new DataSet();
> protected BaseSingleton(){}
> protected static BaseSingleton Instance
> {
> get { return instance; }
> }
> }
> public class Customer : BaseSingleton
> {
> SqlCommand cmdSelect = null;
> static Customer()
> {
> instance = new Customer();
> }
> protected Customer()
> {
> cmdSelect = new SqlCommand("SELECT CustomerID, CompanyName FROM
Customers", con);
> cmdSelect.CommandTimeout = 30;
> da.SelectCommand = cmdSelect;
> }
> public static new Customer Instance
> {
> get { return (Customer)instance; }
> }
> public DataSet GetCustomers()
> {
> con.Open();
> da.Fill(ds, "Customers");
> con.Close();
> return ds;
> }
> }
> public class Test {
> public static void Main() {
> Customer c = Customer.Instance;
> DataSet ds = c.GetCustomers();
> }
> }
> }