Small program----. NET Learning notes for implementing multi-database support

Source: Internet
Author: User
Tags sqlite database

Some of the things that I've summed up

This applet supports MySQL and SQLite databases and can use different databases depending on the requirements

Show the admin class as below, for simple only two fields

1  1  classAdmin2  2     {3  3          PublicAdmin () {}4  4 5  5          PublicAdmin (DbDataReader DR)6  6         { 7  7              This. Id = dr["ID"]. ToString ();8  8              This. Password = dr["Password"]. ToString ();9  9         }Ten Ten  One  OneString ID; A  A  -  -          PublicString Id -  -         { the  the             Get{returnID;} -  -             Set{id =value;} -  -         } -  -String password; +  +  -  -          PublicString Password +  +         { A  A             Get{returnpassword;} at  at             Set{Password =value;} -  -}
Admin

Down is the Adminbasemgr class, which is an abstract class

1   Abstract classAdminbasemgr2     {3 4          Public Abstract BOOLInsert (admin admin);5          Public Abstract BOOLDelete (admin admin);6          Public Abstract BOOLDelete (String ID);7          Public Abstract BOOLUpdate (admin admin);8          Public AbstractAdmin getadminbyid (admin admin);9          Public AbstractAdmin Getadminbyid (String ID);Ten  One}
Adminbasemgr

Down is the implementation of the Adminsqlitemgr class, corresponding to the SQLite database

1 classAdminsqlitemgr:adminbasemgr2     {3         Privateadminsqlitemgr () {}4 5         Private StaticAdminsqlitemgr adminmgr =NULL;6 7          Public Staticadminbasemgr getadminmgr ()8         {9             if(Adminmgr! =NULL)Ten             { One                 returnadminmgr; A             } -             Else -             { the  -                 return(Adminmgr =Newadminsqlitemgr ()); -             } -         } +          Public Override BOOLInsert (admin admin) -         { +            /*String sql = "INSERT into admin values (@id, @password)"; A sqliteparameter[] p = {new Sqliteparameter ("@id", "root"), at new Sqliteparameter ("@password", "root") -                                 }; -             */ -String sql ="INSERT INTO admin values (' root ', ' root ')"; -  -             returnSqlitehelper.executenonquary (sql) = =1; in         } -  to          Public Override BOOLDelete (admin admin) +         { -            returnDelete (admin. ID); the         } *  $          Public Override BOOLDeletestringID)Panax Notoginseng         { -String sql ="Delete from admin where [email protected]"; theSqliteparameter p =NewSqliteparameter ("@id", id); +             returnSqlitehelper.executenonquary (sql,p) = =1; A         } the  +          Public Override BOOLUpdate (admin admin) -         { $             Throw Newnotimplementedexception (); $         } -  -          Public OverrideAdmin getadminbyid (admin admin) the         { -             returnGetadminbyid (admin. ID);Wuyi         } the  -          Public OverrideAdmin Getadminbyid (stringID) Wu         { -String sql ="SELECT * from admin where [email protected]"; AboutSqliteparameter[] p = {NewSqliteparameter ("@id", id)}; $Sqlitedatareader dr =sqlitehelper.executereader (SQL, p);; -  -             return NewAdmin (DR); -         } A}
Adminsqlitemgr

It's not like MySQL.

Again, it's the Adminmgrfactory class, which returns different adminmgr based on different databases.

1 classadminmgrfactory2     {3          Public Staticadminbasemgr getadminmgr () {4             Switch(Sysconfig.db_type)5             {6                  Case "MySQL":7                     returnadminmysqlmgr.getadminmgr ();8 9                  Case "SQLite":Ten                     returnadminsqlitemgr.getadminmgr (); One             } A  -             return NULL; -}
View Code

  

Call the following

1    Static void Main (string[] args)2        {3             adminmgrfactory.getadminmgr (). Delete ("root"); 4         }
View Code

This allows you to use a different database only if you need to change the configuration information.

---April 22, 2015 23:22:57

  

Small program----. NET Learning notes for implementing multi-database support

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.