Xcode usage record-use xcode to automatically insert test data to the database (No entity class is required) [after modification]

Source: Internet
Author: User
ArticleDirectory
    • 1. Why should we insert data randomly into the database?
    • 2. Functional Requirements
    • 3. Development Process
    • 4. Improvement
-

The previous article introduced the knowledge of reverse engineering of xcode databases. Today, we use xcode to insert random data into the database and write down the data to help people who will learn it later.

For the basic usage knowledge of xcode, please go to the open-source website and Forum on your own. You can also ask questions on the forum. Enter the subject below:

1. Why should we insert data randomly into the database?

Write todayProgramThe reverse engineering of yesterday's database was used to generate a database, but there was no data in it, so I was too lazy to manually insert it. I want to write a program to insert test data randomly. Of course, I shouted in the group before writing the program to see if anyone has done it. But a group of friends said, I don't remember the original words. It means: "If it's so simple, just ask yourself for one ". Of course, this group of friends is right, but to tell the truth, this idea is too naive. Every time a programmer or developer thinks of a problem, they do not abstract it, but do not do everything they can, every time I repeat for, it is too sad. Although I am not a very professional developer, I still want to write a general point program to facilitate the insertion of random data, rather than every time, each table goes.

2. Functional Requirements

1. xcode is powerful. It can completely block database differences and easily operate database architecture information;

2. you only need to "use a database connection string" and the number of inserted data records to insert data. No entity class is required. Of course, your database already exists. (if it does not exist, design it first, or reverse engineering );

3. More field types and lengths are required. Data of the corresponding type is inserted randomly.

3. Development Process

In fact, the above requirements are quite simple, and you may think it is difficult to meet 2nd requirements, because you only need to connect the database to strings and databases. Of course, you need xcode.CodeThere are comments in the Code:

According to the suggestions put forward by the big stone, the program was modified:

 

View code 1 ///   <Summary>
2 /// Randomly fill all tables in the specified database connection string
3 ///   </Summary>
4 ///   <Param name = "connstr"> Database connection string </Param>
5 ///   <Param name = "needcount"> Number of filled records </Param>
6 Public Static Void Filldatafordb ( String Connstr, Int Needcount = 50 )
7 {
8 Dal = Dal. Create (connstr ); // Create a data access object based on the database connection string
9 List <idatatable> tablelist = Dal. tables; // Obtains information about all tables and architectures of a database.
10 Tablelist. removeall (t => T. isview ); // Filter out views
11 Foreach ( VaR Item In Tablelist)
12 {
13 // First, obtain the object operation interface of the current table based on the table name.
14 Ientityoperate entity = Dal. createoperate (item. Name );
15 For ( Int I = 0 ; I <needcount; I ++)
16 {
17 Ientity model = entity. Create (); // Create data entity Interface
18 Fielditem [] filds = entity. fields; // Obtain all field information
19 Foreach ( VaR Fild In Entity. fields)
20 {
21 If (! Fild. isidentity)
22 Model. setitem (fild. Name, getrandomvalue (fild ));
23 }
24 Model. Save (); // Save data
25 }
26 }
27 }

 

If you have any questions, go to the Forum to ask questions. The Forum address is later. The main note is that there is a getrandomvalue (fielditem fild)

Function, which obtains the random value of the corresponding type based on the field information. This code is relatively simple. paste it first and feel that there should be a simpler way to operate it, remind anyone you want to know. [Sure enough, the switch was used, but the type is not supported. I used to make a mistake. I proposed it by the big stone. I updated it by the way]

 

View code 1 ///   <Summary>
2 /// Obtain random data of the corresponding type based on field type and length
3 ///   </Summary>
4 ///   <Param name = "fild"> Field object </Param>
5 ///   <Returns> Corresponding random data </Returns>
6 Public Static Object Getrandomvalue (fielditem fild)
7 {
8 Switch (Type. gettypecode (fild. Field. datatype ))
9 {
10 Case Typecode. boolean: Return Randomhelper. getrandombool ();
11 Case Typecode. byte: Return Randomhelper. getrandombyte ();
12 Case Typecode. Char: Return Randomhelper. getrandomchar ();
13 Case Typecode. datetime: Return Randomhelper. getrandomdatetime ();
14 Case Typecode. Decimal: Return Randomhelper. getrandomdouble ( 0 , Needcount * 10.1 );
15 Case Typecode. Double: Return Randomhelper. getrandomdouble ( 0 , Needcount * 10.1 );
16 Case Typecode. int16: Return Randomhelper. getrandomint ( 1 , Uint16.maxvalue );
17 Case Typecode. int32: Return Randomhelper. getrandomint ( 1 , Needcount * 50 );
18 Case Typecode. int64: Return Randomhelper. getrandomint ( 1 , Needcount * 100 );
19 Case Typecode. sbyte: Return Randomhelper. getrandomint ( 1 , 127 );
20 Case Typecode. Single: Return Randomhelper. getrandomdouble ( 0 , Needcount * 10.1 );
21 Case Typecode. String: Return Randomhelper. getrandomstring (( Int ) (Fild. length * randomhelper. getrandomdouble ( 0.2 , 0.7 )));
22 Case Typecode. uint16: Return Randomhelper. getrandomint ( 1 , Uint16.maxvalue );
23 Case Typecode. uint32: Return Randomhelper. getrandomint ( 1 , Needcount * 50 );
24 Case Typecode. uint64: Return Randomhelper. getrandomint ( 1 , Needcount * 100 );
25 Default :
26 Return String . Empty;
27 }

Randomhelper is a common class that everyone has. It is also relatively simple and you will not post it. This is the idea.

4. Improvement

If improvements are made, it would be better to insert more user-friendly data. It is easy to write. Of course, xcode is also required. The following are the related URLs. Let's take a look:

Add a big stone blog and xcode open source URL: http://www.cnblogs.com/nnhyhttp://xcode.codeplex.com/

Forum: http://www.53wb.com/

Technical support QQ: 1600800

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.