.NET learning notes

xiaoxiao2021-03-06  17

The public language runtime uses the managed code service (such as automatic reference counting and garbage recovery) to simplify development.

-------------------------------------------------- ------------------- Language supports C #, VB, JScript (here: C #) (JScript syntax Similar to Delphi, Java?) -------- -------------------------------------------------- ----------- Accessing the index properties string s = request.queryString ["name"]; string value = request.cookies ["key"]; ------------ -------------------------------------------------- ------ Declaring the indexed property // default indexed propertypublic string this [string name] {get {return (string) lookuptable [name];} --------------- -------------------------------------------------- ---- Declaration Simple Attribute Public String Name {Get {... Return ...} set {... = value;}} ------------------------------------------------------------------------------------------------------ -------------------------------------------------- - declare and use enumeration // declare the Enumerationpublic enum MessageSize {Small = 0, Medium = 1, Large = 2} // Create a Field or Propertypublic MessageSize msgsize; // Assign to the property using the enumeration valuesmsgsize = Small; - -------------------------------------------------- ------------------ Enumerate Collection Foreach (String S in Coll) {...} ----------------- -------------------------------------------------- - Custom Properties // Stand-Alone Attribute [Stathread] // Attribute with Parameters [Obsolete ("Obsole with"] // attribute with named parameters [Obsolete ("Obsolete Message Here", True)] --- -------------------------------------------------- ---------------- Case Statement Switch (Firstname) {Case "John": ... Break; Case "Paul": ... Break; Case "ringo":. DEFAULT: ... BREAK;

} ------------------------------------- -------------------- Exception Try {// Code That Throws Exceptions} catch (overflowexception e) {// catch a specific exception} catch (exception e) { // catch the generic exceptions} finally {// Excection Some Cleanup code} ---------------------------------- ---------------------------------- String S1; STRING S2 = "Hello"; S2 = "world"; S1 = S2 "!!!"; // useing stringbuilder class for performance // Performance StringBuilder S3 = new stringbuilder (); s3.append ("hello"); S3.Append ("World "); S3.Append (" !!! "); ------------------------------------ -------------------------------- Event handler entrust void mybutton_click (Object sender, eventargs e) {...} -------------------------------------------------- ------------------ Declaration Event // Create a public evening auevent; // create a method for firing the eventprotaced void for firing the eventprotected void ONMYVENT (Eventargs E) {MyEvent (this, e);} ---------------------------------------------- --------------------- - Add an event handler to the event or from the event removal event handler Control.Change = New EventHandler (this.changeeventhandler); control.change - = new eventhandler (this.changeeventhandler); --------- -------------------------------------------------- --------- Conversion INT I = 3; string s = i.toString (); double d = double.parse (s);

-------------------------------------------------- ------------------ Implement the interface ---- C # public class myclass: ienumerable {... ienumerator {...}} --- --Jscriptpublic class myclass ustements →umerable {... function ienumerable.GeteNumerator (): ienumerator {...}} ---------------------------------------------------------------------------------------------------------------------- ----------------------------------------- // Class with the MAIN method defined using System; public class ConsoleCS {public ConsoleCS () {Console.WriteLine ( "Object Created");} public static void Main (String [] args) {Console.WriteLine ( "Hello World"); ConsoleCS ccs = new ConsoleCS ();}} // csc /out:consolecs.exe / T: EXE console.cs ----------------------------- --------------------------------------- // Standard module USING SYSTEM; public class module { Public static void main (string [] args) {console.writeLine ("Hello World");}

} // csc /out:consolecs.exe / T: EXE console.cs --------------------------------- ----------------------------------- / / Prevent URLs from garbled, httputility.urlencode (String) // Response .REDIRECT ("Controls_navigationTARGET.ASPX? Name =" System.Web.httputility.urlencode (name.text)); ------------------------ ------------------------------------------ - SqlDataReader pair from SQL Database Retrieved Data Provides only forward read-only pointers - When using SQLCOMMAND, you must explicitly open and close SQLConnection; - SqlDataAdapter automatically handles how to open the connection. Sqlcommand mycommand = new sqlcommand ("Select * from authors", myconnection; myconnection.open (); sqldataareader dr = mycommand.executeReader (); ----------------- -------------------------------------------------when SQLCOMMAND is also used when performing commands that do not return data (such as insert, update, and delete). This command is sent by calling the ExecuteNonQuery method, and the method returns the number of rows affected. -------------------------------------------------- ------------------ Note that the EnableViewState property of DataGrid is set to false. If the data is to be populated in each request, the DataGrid storage will not be advantageous to send the status information sent by the round trip from the form. Because DataGrid stores all its data when maintaining the status, it is important to close it, so that page performance can be improved. -------------------------------------------------- ------------------ Regular expression validationExpression = "[0-9] {3} - [0-9] {2} - [0-9] {4} "// must be digits: XXX-XX-XXXX ValidationExpression =" ^ [01] $ "// should be either 0 or 1ValidationExpression = '<% # InputValidator.AnsiNameExpressionString%>' InputValidator.AnsiTwoCharacterStateErrorString

ValidationExpression = "[/ w / s - '] " // only alphapleric characters and limited puncture ----------------------------- -------------------------------------- ASP.NET page Using the stored procedure is just the so far Sqlcommand objects have been expanded. CommandText is just the name of the stored procedure, not special query text. By setting the CommandType property, pointing to SqlCommand to CommandText is a stored procedure. Mycommand.selectcommand.commandtype = commandType.StoredProcedure; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ---------------------------- Access XML-based data begins with this section, DataSet is in a way independent of the actual data source Designed to abstract data. This can be understood by changing the example of the exemplary from SQL to XML. DataSet supports the READXML method that uses the FileStream object as a parameter. In this case the file read must simultaneously contain the architecture and data you want to read. DataSet expects data in the form, as shown in the example below. column value column value column value column value < ColumnName1> column value column value column value column value each section of the table TableName One line corresponds to. The following example shows how to read architectures and data from an XML file using the READXML method of DataSet. Note that after the data is read into DataSet, it cannot distinguish that - DataGrid is also bound to it. You can also read data and architecture, using Dataset's READXMLDATA and READXMLSCHEMA methods. Just as DataSet supports reader methods for XML data. It also supports writing data. -------------------------------------------------- --------------------------------- When performing commands that do not return data (such as insert, update, and delete) When using SQLCommand instead of SqlDataAdapter. This command is sent by calling the ExecuteNonQuery method, and the method returns the number of rows affected.

-------------------------------------------------- ---------------------------------- Simple two-layer web form page External components typical use is to implement data access. It simplifies the code in the page, enhances readability and separates the user interface (UI) logic to the system function. The following example illustrates a simple two-layer web form page that uses the data access component to retrieve product information. Data Access Components use a single parameter of its constructor to specify the connection string of the product database. Web Form Page Use the GetCategories method of this component to fill the drop-down list, call the GetProductSforcategory method of the component to display the product of the user selected.

Simple Three-layer Web Forms page Three-layer application model extends two-layer scheme, including business rules between user interface and data access logic. This model allows user interface developers to handle higher abstract levels, not directly to direct data by low-level data access component API. Intermediate business components typically enforce business rules and ensure compliance with the database relationship and primary key constraints. The following example calculates the discount using the intermediate component based client-entered two vendor IDs. -------------------------------------------------- ---------------------------------- If the A class has an attribute of type B, then B Attributes (if any) is called a subtribracle. The following example defines custom controls SimpleSubproperty, which has the properties of the Format type. Format is a class with two primitive properties - Color and Size, which in turn becomes SIMPLESUBPROPERTY sub-properties. Note that ASP.NET has special syntax for setting sub-properties. The following code example shows how to set the format.color and format.size subtribracuits on SimpleSubproperty declaration. "-" syntax indication subtribracle. --------------------- --------------------------------------- Use the HTMLINPUTFILE HTMLINPUTFILE control from the client browser to The server's binary or text file is uploaded. The file is uploaded to all HTML 3.2 and a higher version of the web client. Note that the EncType property on the

mark must be set to "Multipart / Form-Data". -------------------------------------------------- ---------- Important Description: Different from ASP (in ASP, the function can be declared in <%> block), all functions and global page variables must be in the