This is the class
Persons
to accompany the class Person
.
Persons.cs:
public class Persons: IEnumerable{ private List Items = new List (); public string sError = ""; public Person Add(Person oItem) { Items.Add(oItem); return oItem; } public Persons Clear() { Items.Clear(); sError = ""; return this; } public int Count() { return Items.Count; } IEnumerator IEnumerable.GetEnumerator() { return null; } IEnumerator IEnumerable .GetEnumerator() { foreach (Person oItem in Items) { yield return oItem; } } public Page oPage = new Page(); #region SQL private static string sTable = "Persons"; private static string SQL0 = "SELECT CAST(COUNT(*) AS INT) nc FROM " + sTable; private static string sFr1 = ",Partys.*" + ",sfPerOrg=COALESCE((SELECT TOP 1 sOrgNm FROM Organizations WHERE nfOrgPar=nfPerOrg),'')" + ",sfPerUnt=COALESCE((SELECT TOP 1 sUntNm FROM Units WHERE nfUntPar=nfPerUnt),'')" + ",sfParPerOwn=dbo.GetObjNm('Person',nfParPerOwn)" + ",sComLND=COALESCE((SELECT TOP 1 sCom FROM Communicators WHERE nfComPar=nfPerPar AND eComUse='CON' AND eComType='LND' ORDER BY nkCom),'')" + ",sComMOB=COALESCE((SELECT TOP 1 sCom FROM Communicators WHERE nfComPar=nfPerPar AND eComUse='CON' AND eComType='MOB' ORDER BY nkCom),'')" + ",sComEML=COALESCE((SELECT TOP 1 sCom FROM Communicators WHERE nfComPar=nfPerPar AND eComUse='CON' AND eComType='EML' ORDER BY nkCom),'')" + ",sLoc=COALESCE((SELECT TOP 1 sLocAdr+NCHAR(9)+sLocZip+NCHAR(9)+sLocCty FROM Locators WHERE nfLocPar=nfPerPar AND eLocUse='CON' ORDER BY nkLoc),'')"; private static string sJoin = " INNER JOIN Partys ON nfPerPar=nkPar"; public int LoadPage() { SqlConnection cn = DB.GetCn(); SqlCommand cmd = cn.CreateCommand(); string sOrder = ParseOrder(); string sWhere = ParseWhere(cmd); string SQL = SQL0 + (sWhere == "" ? "" : " WHERE " + sWhere) + ";"; if (oPage.bPage) { if (oPage.nPage == 0) { SQL += "SELECT TOP " + oPage.nPageSize.ToString() + " " + sTable + ".*" + sFr1 + " FROM " + sTable + " " + sJoin + (sWhere == "" ? "" : " WHERE " + sWhere + " ") + (sOrder == "" ? "" : " ORDER BY " + sOrder + " "); } else { SQL += "WITH t AS (SELECT ROW_NUMBER() OVER (ORDER BY " + sOrder + ") rn," + sTable + ".* FROM " + sTable + " " + sJoin + " " + (sWhere == "" ? "" : " WHERE " + sWhere + " ") + ") " + "SELECT t.*" + sFr1 + " FROM t " + sJoin + " WHERE rn BETWEEN " + (oPage.nPageSize * oPage.nPage + 1).ToString() + " AND " + (oPage.nPageSize * (oPage.nPage + 1)).ToString() + " " + (sWhere == "" ? "" : " AND " + sWhere + " ") + (sOrder == "" ? "" : " ORDER BY " + sOrder + " "); } } else { SQL += "SELECT " + sTable + ".*" + sFr1 + " FROM " + sTable + " " + sJoin + (sWhere == "" ? "" : " WHERE " + sWhere + " ") + (sOrder == "" ? "" : " ORDER BY " + sOrder + " "); } cmd.CommandText = SQL; LoadCmd(cmd); cmd.Dispose(); cn.Dispose(); return Count(); } private string ParseOrder() { string sOrder = ""; int nPos; string[] aOrder = oPage.sOrder.Split('\r'); for (nPos = 0; nPos <= aOrder.Length - 1; nPos++) { if (nPos > 0) sOrder += ","; switch (aOrder[nPos]) { case "sPerNm ASC": sOrder += "sPerLNm,sPerFNm,nfPerPar"; break; case "sPerNm DESC": sOrder += "sPerLNm DESC,sPerFNm DESC,nfPerPar DESC"; break; case "nfPerUnt ASC": sOrder += "dbo.GetTreeUnt(nfPerUnt)"; break; case "nfPerUnt DESC": sOrder += "dbo.GetTreeUnt(nfPerUnt) DESC"; break; case "nfPerOrg ASC": case "nfPerOrg DESC": case "sPerTel ASC": case "sPerTel DESC": case "sPerEml ASC": case "sPerEml DESC": sOrder += aOrder[nPos]; break; default: sOrder = ""; break; } } return sOrder; } private string ParseWhere(SqlCommand cmd) { StringBuilder sb = new StringBuilder(); int nFld, nPos; try { if (oPage.sWhere != string.Empty) { string[] aFlds = oPage.sWhere.Split('\n'); Dictionary aDic = new Dictionary (); for (nFld = 0; nFld < aFlds.Length; nFld++) { nPos = aFlds[nFld].IndexOf(':'); aDic.Add(aFlds[nFld].Substring(0, nPos), aFlds[nFld].Substring(nPos + 1)); } if (aDic["sPerNm"] != "") { sb.Append("sPerLNm LIKE @sNm+'%'"); cmd.Parameters.Add("@sNm", SqlDbType.NVarChar, 100).Value = aDic["sPerNm"]; } } switch (oPage.sWhat) { case "nfPerUnt": case "nfPerOrg": int nf = 0; int.TryParse(oPage.sData, out nf); //if (nf > 0) { if (sb.Length > 0) sb.Append(" AND "); sb.Append(oPage.sWhat + "=@nf "); cmd.Parameters.Add("@nf", SqlDbType.Int).Value = nf; //} break; } } catch (Exception ex) { System.Diagnostics.Debug.Fail(ex.Message); } return sb.ToString(); } private void LoadCmd(SqlCommand cmd) { SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); oPage.nTotal = (int)dr["nc"]; dr.NextResult(); Person oItem; Clear(); while (dr.Read()) { oItem = new Person(); oItem.ParseDr(dr); Add(oItem); } dr.Dispose(); } #endregion #region Client-server private enum enFldJs { bPage = 0, nPage, nPageSize, sOrderBy, LENGTH }; public void ParseFromJs(string sVals) { string[] aVals = sVals.Split('\r'); oPage.ParseFromJs(aVals[0]); Items.Clear(); Person oItem; for (int nPer = 0; nPer <= aVals.Length - 2; nPer++) { oItem = new Person(); oItem.ParseFromJs(aVals[nPer + 1]); Items.Add(oItem); } } public string ToJson() { StringBuilder sb = new StringBuilder("["); sb.Append(oPage.ToJson()); sb.Append(",["); bool bFirst = true; foreach (Person oItem in Items) { if (bFirst) bFirst = false; else sb.Append(","); sb.Append(oItem.ToJson()); } sb.Append("]]"); return sb.ToString(); } #endregion Client-server }
No comments:
Post a Comment