C # learning notes (collecting fragmented knowledge points)

Source: Internet
Author: User

Class annotation ///

The error message can be directly Ctrl + c

# Region
# Endregion folding

Enter CW twice and press tab to quickly obtain console. writeline ();

Console. writeline ("{0} is {1}, education is {2}", name, age, Edu );

Convert. tostring (163); Convert. toint32 ("78 ");
Type conversion

Console. Clear (); clear screen

Out ref
The user transfers a value to the calling function. Both the out and ref keywords are available.

If (str1.equals (str2) if (str1.equals (str2, stringcomparison. ordinalignorecase ))
String comparison

If (str1.toupper (). Equals (str2.toupper () Str. tolower ()
String case-sensitive Conversion

All object instances can use the. tostring () method.

Str. substring (6, 3) Str. Contains ("free ")
Str. Split ('') Str. Split ('','-','/')
Can be characters, strings, character arrays, etc.

Str. startwith ("Kai") Str. endwith ("Jin ")
Int n = "Hello, world! ". Indexof (" wo ", 3)
3 is the starting position. -1 is returned if it does not contain

-----------------------------------------------------------------------

Select count (*) from t_person;
Select max (salary) from t_person;
Select min (salary) from t_person;
Select AVG/sum (salary) from t_person;

Select * From t_person order by age ASC/DESC;
Ascending and descending
Select * From t_person order by age DESC, salary ASC;

Select * From t_person where salary> 5000 order by age DESC;
Where is before order

Select * From t_person where name like '_ kai' or name like' % Kai %'
Wildcard: _ single character, % multiple characters

Where age between 20 and 30

Select age count (*) from t_person group by age;

Select age, max (salary), count (*) from t_person group by age

In addition to aggregate functions
Select age, count (*) from t_person group by age having count (*)> 1
Having filters the grouped data

Select top 3 * From t_person order by salary DESC
Top keywords: select the first n

Select top 3 * From t_person where fnumber not in (select top 5 fnumber from t_person order by salary DESC) order by salary DESC;
The first three out of the first five, that is, the sixth and eighth out of the first five
First five

Alter table t_person ADD company varchar (20 );
Add a new field

Select distinct department from t_person; Exclude duplicate items
Select distinct department, company from t_person;
Distinct removes duplicates for the entire row, not just for the department

Select name, age from t_person
Union
Merging duplicate items in the Union result class
Select name, age from t_employee; the number of fields must be consistent, and the type must be compatible (the same is the best ).

Select number, salary from t_person
Union all
Select 'Total', sum (salary) from t_person; Union all completion fields are already the same

ABS () calculates the absolute value ceiling () to round to the maximum integer
Floor () Rounding to the smallest integer round () Rounding

Len () Evaluate the string length lower ()
Upper () ltrim ()
Rtrim () removes Spaces
No Trim () substring (string, startposition, length)

-----------------------------------------------------------------------

Enum color Enumeration
{Red,
Yellow,
Blue, green = 5,
White} So Red = 0 yellow = 0 Blue = 0 Green = 5 white = 6 values are automatically incremented

Two-dimensional array
Int [,] A = new int [,] {1, 1}, {2}, {3 }};
Irregular array. Each sub-array must be initialized. A [x] [Y] is used for access.
Int [] [] A = new int [3] [];
A [0] = new int [] {1 };
A [1] = new int [] {1, 2 };
A [2] = new int [] {1, 2, 3 };

String str1 = "C: \ A \ B .txt"; string str2 = @ "C: \ A \ B .txt ";
@ Symbol indicates that escape characters are ignored. str1 and str2 are identical.

String STR = "hello ";
Console. writeline (string. Copy (STR) + Str. substring (3,2) + Str. insert (3 ,"*****"));
The result is str replication, sub, and insert:
Hello Lo
El *** Lo

Int num = int. parse (console. Readline (); int num = convert. toint32 (console. Readline ());

Char [] C = console. Readline (). tochararray ();
Character comparison if (char [0]> = '0' & char [0] <= '9 ')
If (char [0]> = 'A' & char [0] <= 'Z ')

Switch (expression) // can be integer or string type
{Case 1: -----; break;
Case 2: -----; break;
Default: ------; break; // The break must exist! Cannot penetrate. Default is optional
}

Foreach (string name in new string [] {"gukai", "guxiaofei", "luyong "})
{} The array subscript does not need to be taken into account.

If return is used in the main method, the program ends.

Params array parameters
Public int drink (int id, Params string [] Name ){}
1. Use int KK = drink (33, "Gu", "Fu", "Lu") when calling ");
Or string [] AA = {"AA", "BB", "cc"} drink (33, AA );
Variable Length
2. Only one Params parameter can be declared in a method.
3. The Params parameter must be after all parameters
4 must be an array
5. The out and ref keywords cannot be used simultaneously.

Indexer -------------------------- to be learned

-----------------------------------------------------------------------

Sqlconnection conn = new sqlconnection ("database = 'hrms '; Data Source = '2017. 168.4.160 '; user id = 'root'; Password = 'dada7020'; charset = 'utf8'; pooling = true ");
Sqlcommand cmd = conn. createcommand ()

Sqlcommand cmd = new sqlcommand ();
Cmd. Connection = conn;
Cmd. commandtext = plain text;

Sqldatareader reader = cm. executereader ();
While (reader. Read ()){
}

Conn. open (); Conn. Close ();
Conn. Dispose (); Using is the most convenient.
Call dispose after the using scope is out, and determine whether it is closed. The using statement automatically closes before dispose.

SQL Injection Vulnerability attack ------ where Password = 'Kai' or '1' = '1 ';

Cmd. commandtext = "select * From where name = @ name and password = @ password" to be parameterized ";
Cmd. paraneters. Add (New sqlparaneters ("@ name", name ));
Cmd. paraneters. Add (New sqlparaneters ("@ password", password );
In MySQL? Mark parameterized queries

Winform
This. acceptbutton = button1; this. cancelbutton = button2;

Numericupdown control numericupdown1.maximun = 25;
Numericupdown1.decimalplaces = 2;

Sqlserver
Getdate () Get the current date dateadd (datepart, number, date)
The parameters are metering units and numerical values respectively. The date datepart can be year, month, day, HH, or yy.
Dateadd (month,-8, getdate () current date datediff (datepart, startdate, enddate) for the first 8 months calculate the difference value of the date

Select datediff (year, findate, getdate (), count (*) from t_person group by datediff (year, findate, getdate ());

Cast type conversion
Select cast ('20140901' as INT), cast ('2017-08-08 'ad datetime) from t_person;
Or convert (datetime, '2017-09-09 ') convert (varchar (50), 2009)

Select isnull (fname, 'Alias name') as name from t_person if it is null, the system returns the alias name; otherwise, the system returns the fname.

Use Case
Select name,
(Case level
When 1 then 'normal users'
When 2 then 'Membership'
When 3 then 'vip'
Else 'unknown customer'
No then here!
End) as customer type from t_person;
Note that the case brackets in SQL statements are Parentheses!

Select name,
(If case is not a fixed value, there is no field name after case
When salary <2000 then 'lower'
When salary> 4000 then 'high income'
Else 'General generation'

End) as income level from t_person;

Select * from (select U. user_name, U. name, L. operation_time, L. content from user_info U, operation_log L where u. id = L. user_id) as AA where user_id = 1;

Automatically add serial numbers to the query results
"Set @ row_number = 0; select @ row_number: = @ row_number + 1 as serial number, data_name as date from dicdata_type_info where ID> 5;

When performing operations on a value with a foreign key, you must cancel the check of its foreign key first.
Set foreign_key_checks = 0;
Insert into dictionary_info (PID, data_name, data_type_id, del_flg, lastupdateuser, lastupdatetime) values (0, '"+ data_name +"', 20120202, 0, 1, '123 ');
Set foreign_key_checks = 1;

-----------------------------------------------------------------------

Log creation Function
Public void createlog (string log)
{
String time = datetime. Now. tostring ("yyyymmddhhmmss ");
Streamwriter Sw = new streamwriter ("log.txt", true, system. Text. encoding. getencoding ("UTF-8 "));
Sw. writeline (Time + ":" + Log );
Sw. Flush ();
Sw. Close ();
}

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.