}//WriteItemListToDatabase //--------------------------------------------------------------------------------------------------------------- ///<summary> /// WriteItemToDatabase - read all records from this.itemList and write to "theTable" ///</summary> ///<output>strength r - output one strength object to the "theTable" in the database </output> public void WriteItemToDatabase(strength r) { SqlConnection myConnection = DBUtils.GetNewSqlConnection(); if (myConnection == null) { LogManager.writeToLog("New connection failed to open; strength_Table.cs:WriteItemToDatabase.GetNewSqlConnection()"); return; } //WARNING: A field, like "ID", defined with "IDENTITY" semantics, cannot be assigned a value since it Auto-Increments string strQuery = "INSERT INTO " + theTable + " (notes, processArea, specificGoal, specificPractice, genericGoal, genericPractice, projectId ) " + "VALUES ( @notes, @processArea, @specificGoal, @specificPractice, @genericGoal, @genericPractice, @projectId);"; SqlCommand myCommand = new SqlCommand(strQuery, myConnection); //DEPRECATED: myCommand.Parameters.Add(...), INSTEAD USE myCommand.Parameters.AddWithValue(...) // myCommand.Parameters.AddWithValue("@ID", r.ID); //cannot asign to a field having IDENTITY semantics myCommand.Parameters.AddWithValue("@notes", r.notes); myCommand.Parameters.AddWithValue("@processArea", r.processArea); myCommand.Parameters.AddWithValue("@specificGoal", r.specificGoal); myCommand.Parameters.AddWithValue("@specificPractice", r.specificPractice); myCommand.Parameters.AddWithValue("@genericGoal", r.genericGoal); myCommand.Parameters.AddWithValue("@genericPractice", r.genericPractice); myCommand.Parameters.AddWithValue("@projectId", r.projectId); myCommand.ExecuteNonQuery(); myConnection.Close(); }//WriteItemToDatabase
}//TestDBI_T_strength_Read_from_DB /// <summary> /// TestDBI_T_strength_T3 - clear the SQLServer strength table, write some demo data to SQLServer DB, /// query the affirmatin table by Project ID, /// </summary> static void TestDBI_T_strength_T3() { Console.WriteLine(" --START: TestDBI_T_strength_T3"); //Construct myTable in RAM SQLServerDB.strength_Table myTable = new SQLServerDB.strength_Table(); //put demo records into myTable for (int i = 1; i < 10; i++) { SQLServerDB.strength sItem = new SQLServerDB.strength(); sItem.ID = i; sItem.notes = "aff_Name_" + i.ToString(); sItem.processArea = "aff_Type_" + i.ToString(); sItem.specificGoal = "aff_specificGoal_" + i.ToString(); sItem.specificPractice = "aff_specificPractice_" + i.ToString(); sItem.genericGoal = "aff_genericGoal_" + i.ToString(); sItem.genericPractice = "aff_genericPractice_" + i.ToString(); sItem.projectId = i; myTable.itemList.Add(sItem); } //Count SQLServerDB strength table rows before clearing int iRows = myTable.CountRows(); Console.WriteLine("myTable.CountRows = " + iRows.ToString()); Console.WriteLine(" --before clear SQLServer database table"); Util.pause(); myTable.Clear_Database_Table(); int iRows2 = myTable.CountRows(); Console.WriteLine("myTable.CountRows = " + iRows2.ToString()); Util.pause(); myTable.WriteItemListToDatabase(); Console.WriteLine("after writing to SQLServerDB"); Util.pause(); int iRows3 = myTable.CountRows(); Console.WriteLine("myTable.CountRows = " + iRows3.ToString()); Util.pause(); int iSeek_ProjectID = 3; Console.WriteLine("seek item: iSeek_ProjectID= " + iSeek_ProjectID); myTable.ReadItemListFromDatabase_ByProjectID(iSeek_ProjectID); Console.WriteLine("SEEK items found: myTable.itemList.Count =" + myTable.itemList.Count.ToString()); SQLServerDB.strength strengthItem = myTable.itemList[0]; strengthItem.Show(); Util.pause(); Console.WriteLine(" --DONE: TestDBI_T_strength_T3"); }
// <summary> /// Equals - compare for equivalence of two objects, comparing each field individually, except for the autonumbered ID field /// true = identical content /// false = NOT identical content /// </summary> /// <param name="other"></param> /// <returns>bool</returns> public bool Equals(strength other) { return( (this.notes == other.notes) && (this.processArea == other.processArea) && (this.specificGoal == other.specificGoal) && (this.specificPractice == other.specificPractice) && (this.genericGoal == other.genericGoal) && (this.genericPractice == other.genericPractice) && (this.projectId == other.projectId) ); }//Equals
} //ReadItemListFromDatabase //--------------------------------------------------------------------------------------------------------------- ///<summary> /// ReadItemListFromDatabase_ByProjectID - read all records from "theTable" insert them into this.itemList, filter by projectID /// 1) Erase the current itemList in memory first /// 2) Read records from SQLServer, filling the itemList ///</summary> ///<input>List<strength> itemList - an ordinary List<> of type strength, will be cleared if not already empty </input> ///<output>List<strength> itemList- an ordinary List<> of type strength, extracted from the database </output> ///<param name="projectId"></param> public void ReadItemListFromDatabase_ByProjectID(int projectId) { itemList.Clear(); //First, empty the existing list contents string sQuery = "SELECT * FROM " + theTable + " WHERE projectId=" + projectId.ToString(); DataSet dsObj = DBUtils.ExecuteSqlQuery(sQuery); if (dsObj != null && dsObj.Tables[0].Rows.Count > 0) { DataTable dtObj = dsObj.Tables[0]; //get the DataTable reference once foreach (DataRow dr in dtObj.Rows) { //extract data int intID = Convert.ToInt32(dr["ID"]); String strnotes = dr["notes"].ToString(); String strprocessArea = dr["processArea"].ToString(); String strspecificGoal = dr["specificGoal"].ToString(); String strspecificPractice = dr["specificPractice"].ToString(); String strgenericGoal = dr["genericGoal"].ToString(); String strgenericPractice = dr["genericPractice"].ToString(); //fill the itemList strength newRec = new strength(); newRec.ID = intID; newRec.notes = strnotes; newRec.processArea = strprocessArea; newRec.specificGoal = strspecificGoal; newRec.specificPractice = strspecificPractice; newRec.genericGoal = strgenericGoal; newRec.genericPractice = strgenericPractice; newRec.projectId = projectId; itemList.Add(newRec); } //for } } //ReadItemListFromDatabase_ByProjectID
}//UpdateItemListToDatabase //--------------------------------------------------------------------------------------------------------------- ///<summary> /// UpdateItemToDatabase - update one record within "theTable" in the database ///</summary> ///<input>strength r - one item to be updated within the "theTable" in the database </input> ///<input> r.currentProject - the projectId to match with one database record</input> public void UpdateItemToDatabase(strength r) { SqlConnection myConnection = DBUtils.GetNewSqlConnection(); if (myConnection == null) { LogManager.writeToLog("New connection failed to open; in strength_Table.cs: UpdateItemToDatabase.GetNewSqlConnection()"); return; } //WARNING: A field, like "ID", defined with "IDENTITY" semantics, cannot be assigned a value since it Auto-Increments string strQuery = "UPDATE " + theTable + " SET " + " notes=@notes," + " processArea=@processArea," + " specificGoal=@specificGoal," + " specificPractice=@specificPractice," + " genericGoal=@genericGoal," + " genericPractice=@genericPractice" + " WHERE " + " projectId=@projectId"; // <<<---- match on the Primary Key !!! TBD: This is probably NOT going to work! may keys will match on projectId???? SqlCommand myCommand = new SqlCommand(strQuery, myConnection); //WARNING: myCommand.Parameters.AddWithValue("@ID", r.ID); //cannot assign/modify a field having IDENTITY semantics myCommand.Parameters.AddWithValue("@notes", r.notes); myCommand.Parameters.AddWithValue("@processArea", r.processArea); myCommand.Parameters.AddWithValue("@specificGoal", r.specificGoal); myCommand.Parameters.AddWithValue("@specificPractice", r.specificPractice); myCommand.Parameters.AddWithValue("@genericGoal", r.genericGoal); myCommand.Parameters.AddWithValue("@genericPractice", r.genericPractice); myCommand.Parameters.AddWithValue("@projectId", r.projectId); myCommand.ExecuteNonQuery(); myConnection.Close(); }//UpdateItemToDatabase