我正在处理IBM i服务器上的数据。我用来连接.NET的驱动程序无法执行LINQ或Entity Framework。我知道还有另一个驱动程序,但这是昂贵的,我还没有弄清楚这是否真的可以根据我在网上看到的一切工作。
为了“解决”这个限制,我将数据从服务器拉入数据表,然后立即将数据放入更友好的LINQ模型中。
到目前为止,我使用过的大多数表都没有太多列,因此复制粘贴参数会让它变得烦人。现在我有一个TON列的文件。
SELECT DDATE, DPERNO, DPIN, DCODE, DCODE2, DESTCS, DBLDBP, DPERMP, DADFEE, DPRFEE,
DSURCR, DTTFEE, DSTNU, DSTEX, DSTNA, DSTSU, DDIR, DAPTS, DBORI, DSUBDV, DDIREC,
DSITED, DLEGAL, DAPPCD, DOWNER, DOWNAD, DONAD2, DOWNCT, DOWNST, DOWNZP, DOWPH1,
DOWCM1, DOWPH2, DOWCM2, DCMPNY, DCONTR, DCONAD, DCNAD2, DCONCT, DCONST, DCONZP,
DCNPH1, DCNCM1, DCNPH2, DCNCM2, DCNCPH, DCTRNO, DSPRCD, DTYCON, DTYCN2, DDEMO,
DZONE, DCOMDT, DSTRWD, DSTRDP, DSTYNO, DPROPT, DSETFR, DSETRR, DSETLF, DSETRT,
DATTGR, DUNAGR, DTYOCC, DSQFOT, DLOTCO, DPLMNM, DCONCD, DDIVIS, DARCHT, DENGIR,
DINSPC, DVARCE, DDTPD, DDTCOM, DUNITS, DVOID, DINSFL, DBLDCD, DCAUKY, DCAPIN
FROM MYTABLE
我正在编写我的方法来进行插入,并认为必须有更好的方法。
public bool AddBuildingPermit(BuildingPermit bp)
{
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO QMFILES.PBMASTP ");
sb.Append("VALUES(@DDATE, @DPERNO, @DPIN, @DCODE, @DCODE2, @DESTCS, @DBLDBP, @DPERMP, @DADFEE, @DPRFEE, @DSURCR, @DTTFEE, @DSTNU, ");
sb.Append("@DSTEX, @DSTNA, @DSTSU, @DDIR, @DAPTS, @DBORI, @DSUBDV, @DDIREC, @DSITED, @DLEGAL, @DAPPCD, @DOWNER, @DOWNAD, ");
sb.Append("@DONAD2, @DOWNCT, @DOWNST, @DOWNZP, @DOWPH1, @DOWCM1, @DOWPH2, @DOWCM2, @DCMPNY, @DCONTR, @DCONAD, @DCNAD2, @DCONCT, ");
sb.Append("@DCONST, @DCONZP, @DCNPH1, @DCNCM1, @DCNPH2, @DCNCM2, @DCNCPH, @DCTRNO, @DSPRCD, @DTYCON, @DTYCN2, @DDEMO, @DZONE, ");
sb.Append("@DCOMDT, @DSTRWD, @DSTRDP, @DSTYNO, @DPROPT, @DSETFR, @DSETRR, @DSETLF, @DSETRT, @DATTGR, @DUNAGR, @DTYOCC, @DSQFOT, ");
sb.Append("@DLOTCO, @DPLMNM, @DCONCD, @DDIVIS, @DARCHT, @DENGIR, @DINSPC, @DVARCE, @DDTPD, @DDTCOM, @DUNITS, @DVOID, @DINSFL, ");
sb.Append("@DBLDCD, @DCAUKY, @DCAPIN)");
using (iDB2Connection conn = new iDB2Connection(_connString))
{
using (iDB2Command cmd = new iDB2Command(sb.ToString(), conn))
{
cmd.Parameters.Add("@DDATE", iDB2DbType.iDB2Decimal).Value = IbmIDateTime.ConvertToNumericDate(bp.DateApplied);
cmd.Parameters.Add("@DPERNO", iDB2DbType.iDB2Decimal).Value = GetNextBuildingPermitNumber();
cmd.Parameters.Add("@DPIN", iDB2DbType.iDB2Char).Value = bp.CommonAddressPin;
/// add all of the fields here
}
}
return true;
}
除了每个字段的复制粘贴/修改。有没有办法让我更轻松。也许是一个循环或什么?
答案 0 :(得分:1)
使用DataAnnotations属性TableAttribute和ColumnAttribute然后使用反射使用单个方法构建查询。
[Table("QMFILES.PBMASTP")]
public class QMFILESPBMASTP
{
[Column("DDATE")]
public DateTime DueDate { get; set; }
}
public bool AddRecord<T>(T model)
{
var tlbInfo = System.Attribute
.GetCustomAttribute(typeof(T),
typeof(CustomTableAttribute))
as CustomTableAttribute;
var cols = typeof(T).GetProperties()
.Where(p => p.GetCustomAttributes(typeof(ColumnAttribute),
true)
.Count() == 1)
.Select(p => p.GetCustomAttributes(typeof(ColumnAttribute),
true)
.First() as ColumnAttribute);
var colNames = cols.Select(c => c.Name);
if (tblInfo != null && cols.Count() > 0)
{
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO ");
sb.Append(tlbInfo.Name);
sb.Append("VALUES(@");
sb.Append(string.Join(", @", colNames.ToArray()));
// etc
}
}