如何使我的Get DataTable方法更具动态性

时间:2014-08-08 07:08:54

标签: c# sql

我创建了一个类,以便更轻松地从sql数据库访问我的数据。

所以从你可以看到我有两个重载的DataTable方法,它们根据哪个列确定从数据库中提取哪些表。

这个问题我似乎无法理解,但似乎很基本的是,"价值"变量并不总是一个整数。它可以是Date,String或Float Variable。

我想知道在这种情况下最好的编码实践是什么,而不仅仅是将它变成动态变量。 我会一遍又一遍地重载方法吗?如果有两个以上的表和多个场景,这似乎不太可行。

public abstract class dbData
{



    ///
    public enum AssmbliesColumn 
    {
        /// <summary>
        /// Returns Entire Datatable.
        /// </summary>
        Fulltable = 0,

        /// <summary>
        /// Returns Single Assembly Datatable.
        /// </summary>
        AssemblyID = 1,

        /// <summary>
        /// Returns Single Assembly Datatable.
        /// </summary>
        AssemblyNo = 2,

        /// <summary>
        /// Returns multiple assemblies datatable based on JobID.
        /// </summary>
        JobID = 3,

        /// <summary>
        /// Returns single assembly datatable based on RFID No.
        /// </summary>
        RFID = 4,               


    };
    public enum JobsColumn
    {

        /// <summary>
        /// Returns entire DataTable.
        /// </summary>
        Fulltable = 0,

        /// <summary>
        ///Returns DataTable based on JobID.
        /// </summary>
        JobId = 1,

        /// <summary>
        /// Returns DataTable based on JobNumber.
        /// </summary>
        JobNumber = 2,

        /// <summary>
        /// Returns DataTable based on JobClient.
        /// </summary>
        JobClient = 3,

        /// <summary>
        /// Returns DataTable based on BudgetHours
        /// </summary>

        BudgetHours = 4,
        /// <summary>
        /// Returns DataTable based on BudgetTonnage.
        /// </summary>

        BudgetTonnage = 5,

        /// <summary>
        /// Returns DataTable based on PurchaseOrder.
        /// </summary>
        PurchaseOrder = 6,
    }



/// <summary>
/// Returns Jobs Datatable
/// </summary>
/// <param name="dataValue"></param>
/// <param name="Column"></param>
/// <returns></returns>
    public static DataTable getDatatable(string Value, JobsColumn Column)
    {
         dataControls dbControl = new dataControls();

        string cmdString;
        SqlCommand sqlCmd = new SqlCommand();




                if ((int)Column == 0)
                {
                    cmdString = "SELECT * FROM dbo.Jobs";
                    sqlCmd.Connection = dbControl.connection;
                    sqlCmd.CommandText = cmdString;
                }

                if ((int)Column == 1)
                {
                    cmdString = "SELECT * FROM dbo.Jobs WHERE jobId = @jobid";
                    sqlCmd.Connection = dbControl.connection;
                    sqlCmd.CommandText = cmdString;
                    sqlCmd.Parameters.AddWithValue("@jobid", Value);
                }
                if ((int)Column == 2)
                {
                    cmdString = "SELECT * FROM dbo.Jobs WHERE jobNumber = @jobnumber";
                    sqlCmd.Connection = dbControl.connection;
                    sqlCmd.CommandText = cmdString;
                    sqlCmd.Parameters.AddWithValue("@jobnumber", Value);
                }
                if ((int)Column == 3)
                {
                    cmdString = "SELECT * FROM dbo.Jobs WHERE jobName = @jobname";
                    sqlCmd.Connection = dbControl.connection;
                    sqlCmd.CommandText = cmdString;
                    sqlCmd.Parameters.AddWithValue("@jobname", Value);
                }

                if ((int)Column == 4)
                {
                    cmdString = "SELECT * FROM dbo.Jobs WHERE jobClient = @jobclient";
                    sqlCmd.Connection = dbControl.connection;
                    sqlCmd.CommandText = cmdString;
                    sqlCmd.Parameters.AddWithValue("@jobclient", Value);
                }

                    dbControl.dbConnect();

        using (SqlDataAdapter adap = new SqlDataAdapter(sqlCmd))
        {

            DataTable dt = new DataTable();
            adap.Fill(dt);

            dbControl.dbDisconnect();
            return dt;

        }
    }

    /// <summary>
    /// Returns Assemblies Datatable
    /// </summary>
    /// <param name="Value"></param>
    /// <param name="Column"></param>
    /// <returns></returns>
    public static DataTable getDatatable(int Value, AssmbliesColumn Column)
    {
        dataControls dbControl = new dataControls();

        string cmdString;
        SqlCommand sqlCmd = new SqlCommand();


        if (Column == 0)
        {
            cmdString = "SELECT * FROM dbo.Assemblies";
            sqlCmd.Connection = dbControl.connection;
            sqlCmd.CommandText = cmdString;
        }
        ///By AssemblyID
        if ((int)Column == 1)
        {
            cmdString = "SELECT * FROM dbo.Assemblies WHERE assemblyID = @assemblyid";
            sqlCmd.Connection = dbControl.connection;
            sqlCmd.CommandText = cmdString;
            sqlCmd.Parameters.AddWithValue("@assemblyid", Value);
        }
        ///By AssemblyNo
        if ((int)Column == 2)
        {
            cmdString = "SELECT * FROM dbo.Assemblies WHERE assemblyNo = @assemblyno";
            sqlCmd.Connection = dbControl.connection;
            sqlCmd.CommandText = cmdString;
            sqlCmd.Parameters.AddWithValue("@assemblyno", Value);
        }
        ///By JobID
        if ((int)Column == 3)
        {
            cmdString = "SELECT * FROM dbo.Assemblies WHERE jobID = @jobid";
            sqlCmd.Connection = dbControl.connection;
            sqlCmd.CommandText = cmdString;
            sqlCmd.Parameters.AddWithValue("@jobid", Value);
        }
        ///By RFID
        if ((int)Column == 4)
        {
            cmdString = "SELECT * FROM dbo.Assemblies WHERE RFID = @rfid";
            sqlCmd.Connection = dbControl.connection;
            sqlCmd.CommandText = cmdString;
            sqlCmd.Parameters.AddWithValue("@rfid", Value);
        }

        dbControl.dbConnect();

        using (SqlDataAdapter adap = new SqlDataAdapter(sqlCmd))
        {

            DataTable dt = new DataTable();
            adap.Fill(dt);

            dbControl.dbDisconnect();
            return dt;

        }
    }

@patrickhofman

我最好创建一个特定于我想要访问的表的类吗?

   class Assemblies
{

    protected int? StoredID = null;
    protected string? StoredStringID = null;
    protected AssembliesColumn StoredColumn;

    public enum AssembliesColumn
    {

        /// <summary>
        /// Returns Entire Datatable.
        /// </summary>
        Fulltable,
        /// <summary>
        /// Returns Single Assembly Datatable.
        /// </summary>
        AssemblyID,
        /// <summary>
        /// Returns Single Assembly Datatable.
        /// </summary>
        AssemblyString,
        /// <summary>
        /// Returns multiple assemblies datatable based on JobID.
        /// </summary>
        JobID,
        /// <summary>
        /// Returns single assembly datatable based on RFID No.
        /// </summary>
        RFID,


    }

    /// <summary>
    /// New Class Based on Integer ID.
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="Column"></param>
    public Assemblies(int ID, AssembliesColumn Column)
    {
        StoredColumn = Column;
        StoredID = ID;
                }

    public Assemblies(string ID, AssembliesColumn Column)
    {
        StoredColumn = Column;
        StoredStringID = ID;


    }


    public Assemblies(AssembliesColumn Column)
    {
        StoredColumn = Column;

    }

    public DataTable GetDataTable()
    {
        dataControls dbControl = new dataControls();
        string cmdString;
        SqlCommand sqlCmd = new SqlCommand();

        try
        {
            switch (StoredColumn)
            {
                case AssembliesColumn.Fulltable:
                    cmdString = "SELECT * FROM dbo.Assemblies";
                sqlCmd.Connection = dbControl.connection;
                sqlCmd.CommandText = cmdString;
                    break;
                case AssembliesColumn.AssemblyID:
                    if (StoredID == null) throw new Exception("Assemblies ID Must be Integer Value");

                    cmdString = "SELECT * FROM dbo.Assemblies WHERE assemblyID = @assemblyid";
                    sqlCmd.Connection = dbControl.connection;
                    sqlCmd.CommandText = cmdString;
                    sqlCmd.Parameters.AddWithValue("@assemblyid", StoredID);
                    break;

                case AssembliesColumn.AssemblyString:
                    if (StoredStringID == null) throw new Exception("Assemblies ID Must be String Value");

                    cmdString = "SELECT * FROM dbo.Assemblies WHERE assemblyNo = @assemblyno";
                    sqlCmd.Connection = dbControl.connection;
                    sqlCmd.CommandText = cmdString;
                    sqlCmd.Parameters.AddWithValue("@assemblyno", StoredStringID);
                    break;

                case AssembliesColumn.JobID:
                    if (StoredStringID == null) throw new Exception("Assemblies ID Must be Integer Value");

                    cmdString = "SELECT * FROM dbo.Assemblies WHERE jobID = @jobid";
                    sqlCmd.Connection = dbControl.connection;
                    sqlCmd.CommandText = cmdString;
                    sqlCmd.Parameters.AddWithValue("@jobid", StoredID);
                    break;
                case AssembliesColumn.RFID:
                    if (StoredStringID == null) throw new Exception("Assemblies ID Must be Integer Value");

                    cmdString = "SELECT * FROM dbo.Assemblies WHERE RFID = @rfid";
                    sqlCmd.Connection = dbControl.connection;
                    sqlCmd.CommandText = cmdString;
                    sqlCmd.Parameters.AddWithValue("@rfid", StoredID);
                    break;

                default:
                    break;

                    dbControl.dbConnect();

        using (SqlDataAdapter adap = new SqlDataAdapter(sqlCmd))
        {
            DataTable dt = new DataTable();

            adap.Fill(dt);

            dbControl.dbDisconnect();
            return dt;

        }
            }
        }

        catch(Exception e)
        {

            MessageBox.Show(e.ToString());


        }


    }
}
}

1 个答案:

答案 0 :(得分:0)

你可以很好地利用泛型:

public static DataTable getDatatable<T>(T Value, AssmbliesColumn Column)

该函数将使用您输入的变量Value的类型,如下所示:

DataTable d = getDatatable(1 /*int*/, AssmbliesColumn.Fulltable);

或者:

DataTable d = getDatatable("s" /*string*/, AssmbliesColumn.Fulltable);

必须注意的是,您的代码最终会失控。如果您需要多个参数,则需要额外的功能。为什么不在enum中为每个值创建一个方法并稍后调用泛型方法?