Before we proceed, we need to make the previous code as thin as possible (which is good for the rest of the world, and the reader should be careful to improve the programming level by carefully compared to where the code and the upper part of this section are actually optimized).
First of all, the operation class of the database is what can be done more common, variable name is also common, at least to connect the target database name, Target workbook, password should be universal, the connection method is also universal, So it should be connected to the database of this method independent (I want to write a read and write Questionuser class as long as the copy and paste repair modification can be)
?
Secondly, it is necessary to make more general use of several methods of pruning. These methods either input type to specify the struct body, or the output type is the specified structure, if the copy and paste rewrite a similar class, the structure must be changed, each read and write data must have changed, so this certainly can not be done very common (perhaps you could use the function name is very common, such as Databaseadd , is to increase the method, A.databaseadd is the record structure according to a specified increase, B.databaseadd is the record structure specified by B increases, not necessarily questionadd, may be answeradd, it is also beneficial, Readers can try it themselves)
?
There is a lot of code that can be perfected in the interface section. In fact, whether it is a new question or modify the test, each point of interaction with the database only two, either from the database to update the interface, or to take data from the interface to update the database. Therefore, whether it is the "previous question", "next question", or "update this problem", most of the code can be reused, what method you use to filter out specific records I don't care, but the last update to the interface method can be unified, thus there can be a Questionupdatesettoui method.
?
The same from the interface to update the data can be called Questionupdategetfromui, and the test entry and examination points, the question input is not from the database in turn update the interface method (or you can have, recorded a topic, automatically empty text box, ready to update the next question, Otherwise, it's not easy to tell if it's in the record.
?
On the test questions, I read a lot of "authoritative" test questions online, feeling very misleading people. One is that these topics are not suitable for beginners, and there is no need to master them. It's like you're learning to drive. There is no need to master the engine principle, or even understand it. There is another even to understand the engine principle is not so rote method (not your back down on behalf of you will be, what overload, encapsulation, entrust, you like the back horse zhe so sure exam can pass, but can't determine whether you really understand)
Http://blog.sina.com.cn/s/blog_485502810101dtp5.html
Http://3y.uu456.com/bp_41rym11l6p8xzko047mj_3.html
?
I think of some of my own test questions, basically all of the requirements of their own programming can be achieved, no matter what method you use to do it, you can make it is qualified, the front of the short answer such as 5 minutes A, give 1 minute time, the back of the simple answer 10 minutes, to 10 minutes (the actual test time randomly pumped 12 points, 4 10 points), asked all should be will (can't ask too tricky, such as ask int type of numerical range, you know does not mean that you are bad, you do not know also does not mean that you do not, that mention this question is more silly force. The main thing is to tell the reader not to be fooled by the exam questions, the exam will only send you a certificate, the ability to pass to you to bring real money, all day playing test routines of those goods do not need the ability to solve problems in the exercise capacity)
?
For a half-day nonsense, we went on to refine the program. If you click Start Test, you will start a random spot check. Because there are already 1-20 of the title, randomly take 12 (60 points), 101-120 of the number, randomly take 4 (40 points), then we first want to write a small algorithm, is given a starting value, the terminating value, and then can generate a random number (more complex, to generate a number of random numbers, and cannot be repeated), then the first is to implement a random number, in order to quickly verify, you can first use a label and button to verify whether the resulting number is within range.
?
After a single data validation, we look at how to produce a set of random numbers, and do not repeat (or from small to large), produce a set of random numbers, you can directly call the method of generating a random number, note that the wording is not unique. It is also recommended that users try to separate a complex function into a combination of several small methods (such as generating a set of random numbers = generating a random number + judging whether random numbers are duplicates + whether they need to be sorted , etc.), these small methods are also defined in Functionmath, But do not need the main program calls, so all the private method can be made)
?
With this feature, we have been able to design the module for the exam. Suppose the user clicks to start the exam, to draw a few in the 5-point question, and then draw a few in the 10-point question. This feature has been better implemented. We've already had the experience of writing calculators and Super hotkeys, and instead of putting the variables in the Form1, we'll just create a new class to take over the entire exam. Regardless of how the entire process is implemented, we can plan to implement the test this method, some of the necessary properties and methods (we already know is to extract a small collection from a large collection, of course, you can also do a set think of all the extracted questions list, convenient program processing), in the method we need at least
Initialize Method-Generate a new random question sequence
A test method-the number of the corresponding question to find the data corresponding to the content and display to the interface
Complete test Method-automate the entire cycle from the beginning of the first question to the end of the final question
?
The initialization method has been verified, the core is to extract the question, we put in the variable Questionall variable (this variable can also be used as a double array to do, with a list of better extensibility, if you follow up and added the judgment, the choice of the question, So the topic put together into a set, the title of the score, type and quantity have changed, with the array do not very good change), in order to step through the verification, each initialization can be output to see whether it is according to our requirements 12 small topics + 4 large topics (serial numbers are 1-20 and 101-120), Finally set an index of 0, this we'll tell you later why this index
?
The premise of each test test is that a certain question can be prepared to read from the database and reflected to the interface, so we first want to write a separate question read, note that the interface should be read-only set to read-only, especially the text box, and in order to verify that you can put some textbox to enter the numbers manually, When you click to re-draw the question can not accurately reflect the corresponding question (in fact, the re-draw should be reflected in the random question sequence of the first question, rather than the database number 1). The response here is the button clicked on the Form1, depending on whether the result is true or not updating the interface, actually the more scientific way is the delegate and the event (which will be described later), now the entire program flow.
?
From the beginning of the exam to the end of the exam, we use an index to decide (the previous question is index--, the next question is index++, confirm the answer is also index++, but confirm the answers will save the current reply, whether immediately deposited in the database reader can decide, The general topic quantity can be saved up rarely, wait for the final completion to unify deposit again. With this index, we can have some additional list variables for each question (whether the current question has been answered, the time spent, the corresponding specific answer text, etc.)
?
In order to give each question the time limit effect, each question starts the time we want to synchronously start a stopwatch and the timer (stopwatch is to count the current time, the timer is in order to display the current time-consuming in the main interface, and to judge if the time-out force answers and enters the next question), first to see this timer, We put a timer on the main interface, is the simplest way (the control is always simpler than writing code), because it does not need to be particularly accurate, the default time interval of 100ms, every 100ms detection is timed out, if the time-out alarm and enter the next question. We have defined a global boolean currenttimeoverlap variable in Examhandler, and if we do not make this variable, it will be a more important programming technique for the reader to learn if the time-out popup is constantly bouncing out.
?
Then look at the stopwatch function, stopwatch in fact, a few methods, Reset,start,stop,gettime (Reset/start/stop/Get Time), as long as the entry into a new topic, then reset First, then start (Reset+start), Run time can be someone else to get the current time (GetTime), more than the time to stop timing and get the current time (Stop), test if the timeout after entering the next question, and then go back to the previous question, the display is not the time limit, but the answer time (more user experience optimization can also be done, For example, if you have answered this question, confirm that the answer button is not available, giving the user a chance to make a mistake.
?
Complete the answer, put all the information into the new database table, for this we need to create a new table (note that the new table does not fill in the duplicate information, we can find the first table according to question_no the score, time limit, specific topics and so on, there is no need to write again, Questionuser only need to retain the user's answer to the relevant information), in fact, there are redundant information, such as the need for an exam ID on the line, do not need the name of the candidate and the company, the company name and the name of the person is not unique, we can do a table to save the candidate's information, At this point, the data records for each test are repeated. The name of the test and the company in which it was written
?
Just make a simple signal here, if you make a data table, the table stores the user's information (name, gender, age, company, work experience, take the test time, total score, other notes, etc.), in order to connect with other tables, For each candidate to have a similar to the ID number of the same number to find the user at any time the test of which questions, how many times, the score of each question (so the database is not the beginning to be able to design a very reasonable, need continuous improvement, and is finished a version after the discovery of unreasonable, and then improve, Do similar projects later will be more experienced, less detours).
?
With the previous entry questions we copy a copy, a little modification can be, define a similar structure, and then join methods and add methods are similar
?
Specific I want to hand in the time is to traverse the list, the structure to add to the database can (temporarily regardless of illegal values, also first set the score to 0 points, in the scoring column can be changed)
?
The same is the Add method, only need to modify the corresponding field (it does not seem to need to delete or update, query method, in the scoring column can do query and update method)
?
Finally think about the function of the start rating (time limit is not specific, anyway, there are a few simple questions to do with this process is also not very reasonable), in fact, the starting score should be followed by the beginning of the test into the same database (start the exam we have a record of the database of most of the information completed, the remaining scores, The final score, or more of the additional information, such as the Test level is at the beginning of the score is completed, start scoring is to read the completed part of the data record, the examination of all the records are searched, according to the number from small to large sorting, and then scoring and update the database to the end, The whole process is the essence is update, interested readers can improve their entire function)
?
More versatility can be explored:
If the user finishes a question in advance, should he be rewarded with time?
If the user constantly re-draw the question, whether it should be given a warning or even forbidden, should I cancel the previous/Next Question view function?
The exam is over, how to upload the title to a place (especially the next 10 points of the superior experimental questions, can not do automatic scoring, only manual review)?
?
Summary: This course briefly introduces the database read and write, this depends on your specific application, if you do Internet-related development, will often encounter, the traditional industrial industry seems to have little demand, but the future of the Internet of things, artificial intelligence, big data and other more trendy areas must also be indispensable, The so-called big data first have a large number of data, certainly with TXT or XML such things hold. So at least there is a basic understanding of the database, it is necessary to simply implement the function.
1 understand the basic operation of the database, and search and revise
2 Understanding how database reads and writes improve, why define these variables, and whether there is more refinement in the code possible
3 constantly improve the quality of the front and back of the code, the same kind of functionality to merge together, as much as possible to reduce duplication of code, think clearly how to design a database, design a function, a class, and then go to a small piece of the implementation and validation of a small piece
?
?
For more instructional videos and downloads, please pay attention to the following information:
My Youku Space:
Http://i.youku.com/acetaohai123
?
My online forum:
http://csrobot.gz01.bdysite.com/
?
Problem Exchange:
qq:910358960
Email:[email protected]
?
?
C # Basic Video tutorial 6.3 How to read and write databases easily