CodeFirst combat: Configure a text database archiving software

background:

Previous write the software, in the preparation of user profiles this one, do not fly because the archive database for small or large problem, it is generally exist in the text.
Began as a file to save a configuration (Figure File.Read operate a simple and File.Write)
due to a software upgrade regularly, the user's profile will constantly increases, it's also compatible with the previous, but also newly added, after configuration and separated by a newline come to save multiple configurations.
After repeated more, feel how much trouble ~ ~ ~

 

solve:

So, for DBImport, you see early versions of database links for users, did not save the configuration, it means lazy ah.
Later, the user response to the increase, combined with CYQ.Data text database functions relatively more perfect, and also easy to use, so increases go up.

 

So today at this as an example to explain is how easy method:

 

For DBImport, there are two input boxes, in order to facilitate archiving and next time you start, you need to save and restore in case next time you start:

 

 

Combat is as follows:

 

1: Use the CYQ.Data .dll V5 version.

2: CodeFirst write entities:

  class Config : CYQ.Data.Orm.OrmBase
    {
         public Config()
        {
             base.SetInit( this" Config "" txt path={0};ts=0 ");
        }
         private  int ID{get;set;}
         private  string Key{get;set;}
         public  string Value{get;set;}
    }

Description:

Table name: Config;

{0}: the root of the code, i.e., software running directory; 

ts = 0: identifying the new parameters, does not create the file table structure of Config.ts. 

 

3: Then there is the button click event is successful, the data is saved link:

  // 保存链接字符串。
                     using (Config config =  new Config())
                    {
                         if (config.Fill( " key='ConnFrom' "))
                        {
                            config.Value = connFrom;
                            config.Update();
                        }
                         else
                        {
                            config.Key =  " ConnFrom ";
                            config.Value = connFrom;
                            config.Insert(InsertOp.None);
                        }
                    }

If the link already exists, it is updated, and if not, is inserted.

 

4: When the detection data at the beginning of running software exists and set the initial value reduction:

    using (Config config =  new Config())
            {
                 if (config.Fill( " Key='ConnFrom' "))
                {
                    txtConnFrom.Text = config.Value.ToString();
                }
                 if (config.Fill( " Key='ConnTo' "))
                {
                    txtConnTo.Text = config.Value.ToString();
                }
            }

 

5: Everything is ready, after F5 to run under software on more than a catalog file (text data stored archive json or xml format data):

 

 

Here the way, if you do not specify text database link ts = 0, the database will be created in the same directory structure:

 

 

Examples on here is over.


For CodeFirst, it supports all databases, but there are little differences:

If for sqlite, mssql, oracle, mysql, etc., because the data is sure to be saved to the database, so after CodeFirst coding run certainly will create the table structure in the database to store data.

It means that, if there are changes in database fields, you may need to modify the corresponding database field;

The text database, if it is increasing or decreasing property field, is completely without incident, if it is to delete or modify field names, data if the field is not, then no matter if the original field data needs, their own to open the Notepad, batch replace it , it is quite convenient.

 


Overall, CodeFirst personal sleep mode of operation, or text database to reality with the convenience ~ ~ ~ and other databases, although if intermediate changes, or to change it back to the CodeLast. . .


Reproduced in: https: //my.oschina.net/secyaher/blog/274347

Guess you like

Origin blog.csdn.net/weixin_33909059/article/details/91967086
Recommended