C #: Summary of office supplies management software project
This semester. the net course is designed as a software for managing office supplies. Only five courses are given for ten hours in the class, and the requirement documents provided by the teacher are not comprehensive, basically, it is written under the course. If you have any questions, ask the teacher in the class. If the function is not correct, try again. The functionality of this software is not complex. After all, it is the work of the school rather than the company's products. The rest of the interface is the operation of the database. The tables in the database are created by the teacher, and there are only four tables in total. However, when the teacher creates the table, the associated table does not have a foreign key set, and the primary key is not set to auto-increment, therefore, when adding and deleting operations, it is always awkward. When deleting a table, you cannot cascade the deletion operation. It takes a lot of time and effort to delete the table one by one. Fortunately, there are only four tables. This is certainly not the case for the company.
In this article, I want to record some of the problems and knowledge I learned in the process of writing this small software, so that you can easily view them later.
Interface
Main Interface:
Subdirectories in the menu bar:
This is a French version of the interface, the introduction:
Fichier: File Article: item Marque: Brand Famille: Category Sous-Famille: sub-category S-lectionner: Select Afficher: Display Ajouter: add Modifier: Modify Supprimer: Delete
Click "select" to bring up the "Data Integration dialog box", as shown below:
Click the button in the upper-left corner to bring up a file selection dialog box. You can select an XML file. All data is saved as an XML file. The single region in the upper-right corner is the method for determining data integration. the above option is to delete all the original data in the database and store the data in the New XML file into the database. The lower option is to update the database. Click the "Integration" button to start reading data. If any exceptions or errors occur, the data is displayed in the Exception window. After reading the data, the window automatically disappears.
Read data
To read data from the database, I first created an object class Article_Model for an item. It has six attributes, corresponding to the six attributes in the XML file, which are the data format in the XML file:
Next, create the class XML_Read, which specifically reads XML file data. This class has only one method:
public List Read_XML(string FileName) { XmlDocument Doc = new XmlDocument(); Doc.Load(FileName); XmlElement RootElem = Doc.DocumentElement;//root element XmlNodeList ArticleNodes = RootElem.GetElementsByTagName("article");//all articles List Atl_Model_List = new List(); //get datas foreach (XmlNode Node in ArticleNodes) { XmlNodeList Childs = Node.ChildNodes; string Description = Childs.Item(0).InnerText; string RefArticle = Childs.Item(1).InnerText; string Marque = Childs.Item(2).InnerText; string Famille = Childs.Item(3).InnerText; string SousFamille = Childs.Item(4).InnerText; string StrPrixHT = Childs.Item(5).InnerText; string[] Str = StrPrixHT.Split(new char[] { ','}); string TempPrix = Str[0] + "." + Str[1]; float PrixHT = float.Parse(TempPrix); Article_Model AM = new Article_Model(Description, RefArticle, Marque, Famille, SousFamille, PrixHT); Atl_Model_List.Add(AM); } return Atl_Model_List; }
The decimal point in the item price column in the data is a comma (why do you need to use a comma !!!!), All are converted once again to convert the value to the float type.
When the file selection dialog box is opened, because only files in XML format can be selected, I added a filter:
OpenFileDialog Ofd = new OpenFileDialog();Ofd.Filter = "xml files(*.xml)|*.xml";Ofd.ShowDialog();
When you store data from an XML file to the List and then to the data inventory, there is a problem that the data may be duplicated. For example, the XML file contains 50 product directories, but they may belong to 30 different brands, that is, the brands, types, and sub-categories may be duplicated, in addition, the primary keys of these tables are not auto-incrementing. Therefore, you need to check whether the data exists in the database before inserting data into the database. If the data does not exist, insert the data. Because the MVC mode is required, I encapsulate all the database operation functions into a class: DB_Controller. Take the Marques table as an example. solution:
DB_Controller DataBase = new DB_Controller();//insert data to table Marquesif (DataBase.SelectMarquesTable(ArtModelList[i].marque) == -1){ DataBase.InsertMarquesTable(MarqueId++, ArtModelList[i].marque);}
Query functions and insert functions are as follows:
public void InsertMarquesTable(int RefMarque,string Nom) { SQLiteConnection DB_Conn = new SQLiteConnection("Data Source=Mercure.SQLite;"); DB_Conn.Open(); string Sql = "INSERT INTO Marques (RefMarque,Nom) VALUES (" + RefMarque + ",'" + Nom + "')"; SQLiteCommand Command = new SQLiteCommand(Sql, DB_Conn); Command.ExecuteNonQuery(); DB_Conn.Close(); } public int SelectMarquesTable(string Nom) { SQLiteConnection DB_Conn = new SQLiteConnection("Data Source=Mercure.SQLite;"); DB_Conn.Open(); string Sql = "SELECT RefMarque FROM Marques WHERE Nom = '" + Nom +"'"; SQLiteCommand Command = new SQLiteCommand(Sql, DB_Conn); SQLiteDataReader Date = Command.ExecuteReader(); int I = -1; if (Date.Read()) { I = int.Parse(Date["RefMarque"].ToString()); } else { I = -1; } Date.Close(); DB_Conn.Close(); return I; }
There are four tables in the database, Articles, Marques, Familles, and SousFamilles. Example figure:
After reading the data, click the "show" button to display the information of all items. The four tables need to be joined for query and the information is displayed using the ListView control. The effect is as follows:
Add, delete, and modify data
It is easy to add three small tables: Marques, Familles, and SousFamilles. Click the "add" button to bring up a dialog box. Enter a name. When adding SousFamille, You need to select an existing Famille. Therefore, in the Add window, Famille is displayed in the drop-down list ComboBox for users to choose from. The add operation on Articles is also a window. The Marque in the window and SousFamille are displayed in the ComboBox drop-down list for users to choose from.
When you modify and delete data in three small tables: Marques, Familles, and SousFamilles, the content in the corresponding Articles table must also be updated, and the data in the display window must also be updated in real time.
Take the Marques table as an example to modify and delete functions:
public void UpdateMarquesTable(string DeNom,string Nom){ SQLiteConnection DB_Conn = new SQLiteConnection("Data Source=Mercure.SQLite;"); DB_Conn.Open(); string Sql = "UPDATE Marques SET Nom = '" + Nom + "' WHERE Nom = '" + DeNom + "'"; SQLiteCommand Command = new SQLiteCommand(Sql, DB_Conn); Command.ExecuteNonQuery(); DB_Conn.Close(); } public void DeleteMarqueByName(string Nom) { SQLiteConnection DB_Conn = new SQLiteConnection("Data Source=Mercure.SQLite;"); DB_Conn.Open(); string Sql = "DELETE FROM Marques WHERE Nom = '" + Nom + "'"; SQLiteCommand Command = new SQLiteCommand(Sql, DB_Conn); Command.ExecuteNonQuery(); DB_Conn.Close(); }
Summary
In general, this course design is not difficult. If you drag and drop the interface in VS2015, you can draw a picture. If you want to operate the database, you only need to control the logic of each function, naturally, everything becomes. This is my first time using C #. I feel that it is not very different from C ++ and java. It may be that I am not very familiar with it. However, the computer language is the same. If you have mastered one language carefully, other languages will be easy to use.