The example is the "Party" from the "Party-Person-Organization" pattern.
Party.cs
public int nkPar { get; set; } public byte[] rvPar { get; set; } public int nfParPerOwn { get; set; } public string sfParPerOwn { get; set; } public string sParTag { get; set; } public string ePar {get;set;} public string sParAFM { get; set; } public string sParDOY { get; set; } public string sParTxt { get; set; } public int nAudUser { get; set; } public string sAudIp { get; set; } public DateTime dAud { get; set; } public string sError { get; set; } public Party() { Clear(); } public Party(int nkPar) { LoadNk(nkPar); } public Party Clear() { nkPar = 0; rvPar = Constants.rv0; nfParPerOwn = 0; sfParPerOwn = ""; sParTag = ""; ePar = ""; sParAFM = ""; sParDOY = ""; sParTxt = ""; nAudUser = UserLib.nkUsr; sAudIp = ""; dAud = Constants.d0; return this; } #region SQL public Party Save() { SqlConnection cn = DB.GetCn(); SqlCommand cmd = cn.CreateCommand(); cmd.CommandText = "UPDATE Partys SET" + "nfParPerOwn=@nfParPerOwn,sParTag=@sParTag,ePar=@ePar,sParAFM=@sParAFM,sParDOY=@sParDOY,sParTxt=@sParTxt" + ",nAudUser=@nAudUser,sAudIp=@sAudIp,dAud=SYSUTCDATETIME()" + " WHERE nkPar=@nkPar AND rvLoc=@rvLoc" + ";IF @@ROWCOUNT=1 SELECT CAST(1 AS BIT); ELSE" + " IF EXISTS(SELECT * FROM Partys WHERE nkPar=@nkPar)" + " SELECT CAST(0 AS BIT)" + " ELSE " + " INSERT Partys (" + "nfParPerOwn,sParTag,ePar,sParAFM,sParDOY,sParTxt,nAudUser,sAudIP" + ") OUTPUT CAST(1 AS BIT)" + " VALUES (" + "@nfParPerOwn,@sParTag,@ePar,@sParAFM,@sParDOY,@sParTxt,@nAudUser,@sAudIP" + ")"; cmd.Parameters.Add("nkPar", SqlDbType.Int).Value = nkPar; cmd.Parameters.Add("@rvPar", SqlDbType.VarBinary).Value = rvPar; cmd.Parameters.Add("@nfParPerOwn", SqlDbType.Int).Value = nfParPerOwn; if (nfParPerOwn == 0) { cmd.Parameters["@nfParPerOwn"].Value = DBNull.Value; } cmd.Parameters.Add("@sParTag", SqlDbType.NVarChar, 100).Value = sParTag; cmd.Parameters.Add("@ePar", SqlDbType.NChar, 3).Value = ePar; cmd.Parameters.Add("@sParAFM", SqlDbType.NVarChar, 50).Value = sParAFM; cmd.Parameters.Add("@sParDOY", SqlDbType.NVarChar, 100).Value = sParDOY; cmd.Parameters.Add("@sParTxt", SqlDbType.NVarChar, -1).Value = sParTxt; cmd.Parameters.Add("@nAudUser", SqlDbType.Int).Value = UserLib.nkUsr; cmd.Parameters.Add("@sAudIp", SqlDbType.NVarChar, 50).Value = UserLib.sUsrIP; bool bOK = (bool)cmd.ExecuteScalar(); cmd.Dispose(); cn.Dispose(); if (!bOK) sError = "RV"; return this; } public ErMsgs Validate() { ErMsgs oErs = new ErMsgs(); //if (this.nfLocPar == 0) { // oErs.Add("", ErMsg.enSeverity.eError, "nfLocPar", "Enter a value"); //} return oErs; } public Party LoadNk(int nk) { SqlConnection cn = DB.GetCn(); SqlCommand cmd = cn.CreateCommand(); cmd.CommandText = "SELECT *" + ",sfParPerOwn=COALESCE(sPerLNm+' '+sPerFNm)" + " FROM Partys" + " LEFT JOIN Persons ON nfParPerOwn=fPerPar" + " WHERE nkPar=@nk"; cmd.Parameters.Add("@nk", SqlDbType.Int).Value = nk; bool bOK = Load(cmd); cmd.Dispose(); cn.Dispose(); return this; } private bool Load(SqlCommand cmd) { SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); ParseDr(dr); } else { Clear(); } dr.Close(); cmd.Connection.Close(); return true; } public void ParseDr(SqlDataReader dr) { DataView dvSchema = dr.GetSchemaTable().DefaultView; nkPar = (int)dr["nkPar"]; rvPar = (byte[])dr["rvPar"]; //nfParPerOwn = (int)(dr.IsDBNull(dr.GetOrdinal("nfParPerOwn")) ? 0 : dr["nfParPerOwn"]); nfParPerOwn = (dr["nfParPerOwn"] == DBNull.Value ? 0 : (int)dr["nfParPerOwn"]); //sfParPerOwn = (string)dr["sfParPerOwn"]; sfParPerOwn = (Ut.FieldExists(dvSchema, "sfParPerOwn") ? (string)dr["sfParPerOwn"] : ""); sParTag = (string)dr["sParTag"]; ePar = (string)dr["ePar"]; sParAFM = (string)dr["sParAFM"]; sParDOY = (string)dr["sParDOY"]; sParTxt = (string)dr["sParTxt"]; nAudUser = (int)dr["nAudUser"]; sAudIp = (string)dr["sAudIp"]; dAud = (DateTime)dr["dAud"]; return; } public static bool Delete(int nk) { SqlConnection cn = DB.GetCn(); SqlCommand cmd = cn.CreateCommand(); cmd.CommandText = "DELETE Partys WHERE nkPar=@nk"; cmd.Parameters.Add("@nk", SqlDbType.Int).Value = nk; cmd.Dispose(); int nRows = cmd.ExecuteNonQuery(); cn.Dispose(); return (nRows == 1); } #endregion #region Client-server public void ParseFromJs(string sVals) { string[] aVals = sVals.Split('\t'); int nExpected = 8; if (aVals.Length != nExpected) System.Diagnostics.Debug.Fail("Expected: " + nExpected.ToString() + " fields\nReceived: " + aVals.Length.ToString()); int nFld = 0; nkPar = int.Parse(aVals[nFld++]); rvPar = Ut.RowVerFrString(aVals[nFld++]); nfParPerOwn = int.Parse(aVals[nFld++]); sParTag = Regex.Replace(aVals[nFld++].Replace(',', ' '), @"\s+-?\s*", " "); //Remove "," "-" ePar = aVals[nFld++]; sParAFM = aVals[nFld++]; sParDOY = aVals[nFld++]; sParTxt = aVals[nFld++]; } public string ToJson() { StringBuilder sb = new StringBuilder("["); sb.Append(nkPar.ToString()); sb.Append("," + Ut.jstr(Ut.RowVerToString(rvPar))); sb.Append("," + nfParPerOwn.ToString()); sb.Append("," + Ut.jstr(sfParPerOwn)); sb.Append("," + Ut.jstr(sParTag)); sb.Append("," + Ut.jstr(ePar)); sb.Append("," + Ut.jstr(sParAFM)); sb.Append("," + Ut.jstr(sParDOY)); sb.Append("," + Ut.jstr(sParTxt)); sb.Append("]"); return sb.ToString(); } #endregion
No comments:
Post a Comment