The usage and configuration of the SubSonic2.2 framework.
Online. there are also a lot of. net ORM frameworks, but I think the framework configuration is very simple. There seem to be a lot of people using it in the past few years, and now it seems to be using less, as the official upgrade has now reached 3.0, and the T4 template is used to generate the code, the amount of code seems to be reduced. However, I felt that 2.2 was still relatively stable. Some netizens asked me how to use the framework a few days ago and asked me to sort it out. Let's stop talking nonsense. Let's take a look at how to configure and use it.
Step 1 configure web. config:
Add the SubSonic declaration code to the configSections node as follows:
<configSections> <section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" allowDefinition="MachineToApplication" restartOnExternalChanges="true" requirePermission="false"/> </configSections> <appSettings/>
Directly connecting database files
<connectionStrings> <add name="SubSonicConn" connectionString="Data Source=127.0.0.1; Database=agebook; Integrated Security=true;User ID=sa;Password=sa;" providerName="System.Data.SqlClient"/> </connectionStrings>
Configure the SubSonic service configuration under the configuration node in <configuration> as follows:
Do not forget to add <clear/>
First, let's take a look at the name.
ConnectionStringName = "SubSonicConn" indicates the connection name.
GeneratedNamespace = "MyTest". This parameter is optional, but you can only remember the corresponding class namespace name.
<SubSonicService defaultProvider="SubSonicConn" enableTrace="false" templateDirectory=""> <providers> <clear/> <add name="SubSonicConn" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="SubSonicConn" generatedNamespace="MyTest" removeUnderscores="false"/> </providers> </SubSonicService>
OK, the configuration file is ready.
Next, enter SubSonic. dll.
Step 2: configure the code generation tool:
Click the tool option ==> select external tool ==> add ==> in the vs 2010 toolbar. The title can start at will, the name I used is MySubSonicTools ==> click-through command-based selection. Select the sonic.exe file in the subcommanderfile where subsonicis downloaded ==> parameter: generate/out ormclass. The initial directory is $ (ProjectDir ), select the "use command window" and "prompt input parameters" options, and click OK.
Out ormclass. Note that if you create a website program, you must put it in App_Code.
Click the MySubSonic button, and a pop-up window appears. Click OK to refresh the website. The generated folder ormclass will appear,
If there is no App_Code, move it to it.
In the agebook library, the related table usage classes, AllStructs. cs are the main controller files, and StoredProcedures. cs is the stored procedure-related classes. If your operation does not generate these related classes, it should be a problem with the database link string configuration. After checking, you can generate them again.
Step 3: SubSonic method call
After completing the preceding two steps, SubSonic configuration is complete. The SubSonic syntax is as follows:
Before use, you must first reference:
Using MyTest; // This is the name in our configuration file, that is, the namespace name of the generated class using SubSonic;
Load Page code
If (! IsPostBack) {// 1. add A_Admin ad = new A_Admin (); ad. name = "ttttt"; ad. password = "wwwwwwwwww"; ad. loginip = "127.0.0.1"; ad. save (); // 2. modify int updateallid = new Update (A_Admin.Schema ). set (A_Admin.Columns.Password ). failed to ("changed Password") // set the Field Password to "changed Password ". where (A_Admin.Columns.Id ). isdue to ("2 "). execute (); // Of course, there are other ways to modify it // A_Admin ad1 = new A_Admin (); // ad1.Id = 24; // ad1.Name = "ttttt "; // ad1.Password = "wwwwwwwwwwww"; // ad1.Loginip = "127.0.0.1"; // 3. delete // DB. delete (). from (A_Admin.Schema ). where (A_Admin.Columns.Id ). isvice to ("7 "). execute (); A_Admin.Delete (30); // batch Delete // Query q = A_Admin.Query (); // q. addWhere (A_Admin.Columns.Password, "wwwwwwwwww"); // q. queryType = QueryType. delete; // q. execute (); // either of the above two methods can be // 4. query this. gridView1.DataSource = DB. select (). from (A_Admin.Schema ). executeDataSet (). tables [0]; this. gridView1.DataBind ();}
The above code includes addition, deletion, modification, and query methods.
I think it is quite simple. It saves the trouble of configuring xml than other orm frameworks such as nhib.pdf.
In fact, no matter what ORM framework you use in development, you can use EF as well. And even put aside these frameworks
Download this demo: Click me