// Class:applicationFrmClass // A method to get the program list public static List <applicationFrmClass> getList() { List <applicationFrmClass> recordList = new List <applicationFrmClass>(); // Connection string String connStr = "data source=localhost;initial catalog=NoTreal;integrated security=true"; SqlConnection dbConn = new SqlConnection(connStr); SqlCommand sqlStmt = new SqlCommand("SELECT name,programCode FROM program", dbConn); SqlDataReader reader = null; try { dbConn.Open(); // Attempt to connect to database reader = sqlStmt.ExecuteReader(); if (reader.HasRows) { // Need a loop to process all the rows inside reader while (reader.Read()) { // Now process one row String name = reader.GetString(0); String programCode = reader.GetString(1); // Create an object applicationFrmClass record = new applicationFrmClass(name, programCode); recordList.Add(record); } } } // Try-catch, avoid program crash. // When program crash happened, a message box shows the error information and roll back to the last step. catch (SqlException error) { MessageBox.Show(error.Message, "Error"); } finally { if (reader != null) { reader.Close(); } if (dbConn != null) { dbConn.Close(); } } return(recordList); }
// A method to search the entry requirements public static applicationFrmClass getRequirement(String name) { // Link to the database applicationFrmClass record = null; String connStr = "data source=localhost;initial catalog=NoTreal;integrated security=true"; SqlConnection dbConn = new SqlConnection(connStr); SqlCommand sqlStmt = new SqlCommand("SELECT name,description FROM Program WHERE name = @pName", dbConn); SqlParameter param = new SqlParameter("@pName", name); sqlStmt.Parameters.Add(param); SqlDataReader reader = null; try { dbConn.Open(); reader = sqlStmt.ExecuteReader(); if (reader.HasRows) { reader.Read(); String programDescription = reader.GetString(1); record = new applicationFrmClass(name, programDescription); } } catch (SqlException error) { MessageBox.Show(error.Message, "Error"); } finally { if (dbConn != null) { dbConn.Close(); } } Console.WriteLine(record); return(record); }