public void updateMotors() { //Create a Dictionary to hold the motors saved in the database Dictionary<string, Motor> motors = new Dictionary<string, Motor>(); //Create a new connection to the database using (SQLiteConnection m_dbConnection = new SQLiteConnection(@"Data Source=database.sqlite;Version=3;")) { //Open database connection m_dbConnection.Open(); using (SQLiteCommand command = m_dbConnection.CreateCommand()) { //Select everything from the 'motors' table SQLiteCommand getMotors = new SQLiteCommand("SELECT * FROM motors", m_dbConnection); SQLiteDataReader reader = getMotors.ExecuteReader(); //Read every entry in the motors table while (reader.Read()) { string name = (string)reader["name"]; Motor motor = new Motor((string)reader["name"], (int)reader["weight"], (int)reader["kv"], (int)reader["maxCurrent"], (int)reader["maxVoltage"], (int)reader["maxPower"], (string)reader["minCell"], (string)reader["maxCell"], (int)reader["reqESC"]); //Add the battery into the dictionary using the name as the key and a new Battery object as the value motors.Add(name, motor); } } //Close the database connection m_dbConnection.Close(); } savedMotors = motors; }
//Reads through the motor config file and create a list of Battery objects from it public static void read(List<Motor> motors) { using (StreamReader sr = File.OpenText(path)) { List<String> motor = new List<string>(); string s = ""; //This section only runs when there is more content in the config file to read while (sr.Peek() > 1) { //read the current line and split the string into separate parameters s = sr.ReadLine(); foreach (string parameter in s.Split(',')) { motor.Add(parameter); } //Assign each parameter to a helper variable for readability try { string name = motor[0]; int weight = Int32.Parse(motor[1]); int kV = Int32.Parse(motor[2]); int maxCurrent = Int32.Parse(motor[3]); int maxVoltage = Int32.Parse(motor[4]); int maxPower = Int32.Parse(motor[5]); string minCell = motor[6]; string maxCell = motor[7]; int requiredESC = Int32.Parse(motor[8]); //Create a new motor object from the above parameteres Motor newMotor = new Motor(name, weight, kV, maxCurrent, maxVoltage, maxPower, minCell, maxCell, requiredESC); //Add the new motor to the List<Motor> taken as a method parameter motors.Add(newMotor); //Clear the reader string s = ""; //Clear the saved motor parameters motor.Clear(); } catch { //If the reader pulled invalid data from the config file, close the StreamReader and exit the method sr.Close(); return; } } //If there is no more content to read from the config file, close the StreamReader sr.Close(); } }