如何使我的getdatatable方法更动态
本文关键字:动态 方法 getdatatable 何使 我的 | 更新日期: 2023-09-27 18:17:16
我已经创建了一个类,使它更容易从sql数据库访问我的数据。
因此,从你可以看到,我有两个重载的DataTable方法,它们决定根据哪个列从数据库中提取哪个表。
的问题,我似乎不能得到我的头,但它似乎是如此基本的是,"值"变量并不总是将是一个整数。它可以是日期、字符串或浮点变量。
我想知道在这种情况下最好的编码实践是什么,而不仅仅是使它成为一个动态变量。我是不是要不断重载这个方法?然而,如果有两个以上的表和多个场景,这似乎是不可行的。
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());
}
}
}
}
你可以很好地利用泛型:
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
中为每个值创建一个方法,然后再调用泛型方法呢?