C#学习笔记(零碎知识点收集)

类注释用 ///


错误信息可直接 Ctrl+C


#region
#endregion 折叠


输入Cw两次按Tab可快速得到Console.WriteLine();


Console.WriteLine("{0}的年龄是{1},学历是{2}",name,age,edu);


Convert.ToString(163); Convert.ToInt32("78"); 类型转换


Console.Clear(); 清屏


out ref 用户向调用函数传值,out、ref关键词两边都得有


if(str1.Equals(str2)) if(str1.Equals(str2,StringComparison.OrdinalIgnoreCase)) 字符串比较


if(str1.ToUpper().Equals(str2.ToUpper())) str.ToLower() 字符串大小写转换


所有的对象实例都可以掉用.ToString()方法


str.SubString(6,3) str.Contains("自由") str.Split(' ') str.Split(' ','-','/') 可为字符、字符串、字符数组等


str.StartWith("kai") str.EndWith("jin") int n = "Hello,World!".IndexOf("wo",3) 3为起始位置。不包含则返回-1


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


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; 升序、降序
select * from T_Person order by age DESC,salary ASC;


select * from T_Person where salary>5000 order by age DESC; where在order by前


select * from T_Person where name Like '_kai' or name Like '%kai%' 通配符:_单个字符、%多个字符 


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
除了聚合函数外
select age,count(*) from T_Person group by age Having count(*)>1 having对分组后的数据进行过滤


select top 3 *from T_Person order by salary DESC top关键词用于选择前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;
不在前五中的前三,也就是第6-8名 不在前五 前五


alter Table T_Person add company varchar(20); 新增加一个字段


select distinct department from T_Person; 排除重复项
select distinct department,company from T_Person; distinct针对整行排除重复,并非只针对department


select name,age from T_Person
Union union联合 联合结果类 合并重复项
select name,age from T_employee; 字段数量要一致,类型要相容(相同最好)可以不同


select number,salary from T_Person
union all
select '合计',sum(salary) from T_Person; union all 补全字段已相同


ABS()求绝对值 Ceiling()舍入到最大整数 Floor()舍入到最小整数 Round()四舍五入
Len()求字符串长度 Lower() Upper() LTrim() RTrim() 去空格 没有trim()  substring(string,startposition,length)


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


enum color 枚举
{ Red,
Yellow, Blue, Green=5, White} 那么Red=0  Yellow=0  Blue=0  Green=5  White=6 赋值的后面自动递增


二维数组
int[,] a = new int[,]{{1,1},{2,2},{3,3}};
不规则数组,必须对各子数组进行初始化, 访问时用a[x][y]
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"; @符号表示忽略转义字符的分隔符,str1和str2完全相同


string str = "Hello";
Console.WriteLine(String.Copy(str)+str.SubString(3,2)+str.Insert(3,"*****"));
结果分别是对str的复制,sub,插入: Hello lo Hel****lo


int num = int.Parse(Console.ReadLine()); int num = Convert.ToInt32(Console.ReadLine());


char[] c = Console.ReadLine().ToCharArray();
字符比较 if(char[0]>='0' && char[0]<='9') if(char[0]>='a' && char[0]<='z')


switch(表达式) //可为整数类型和字符串类型
{ case 1: -----; break;
case 2: -----; break;
default: ------; break;  //break必须有!不可穿透。 default可有可无
}


foreach(string name in new string[]{"gukai","guxiaofei","luyong"}) {} 不用考虑数组下标越界


main方法中用return则程序结束


params数组型参数
public int drink(int id,params string[] name) {} 
1 调用的时候用 int kk = drink(33,"gu","fu","lu"); 或者string[] aa = {"aa","bb","cc"}drink(33,aa); 长度可变
2 一个方法中只能声明一个params参数
3 params参数必须在所有参数之后
4 必须为一位数组
5 不能同时使用out和ref关键词


索引器--------------------------有待学习


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


SqlConnection conn = new SqlConnection("Database='hrms';Data Source='192.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 = cmdText;


SqlDataReader reader = cm.ExecuteReader();
while(reader.Read()) { }


conn.Open(); conn.Close(); conn.Dispose(); 用using最方便。 出了using作用域后调用dispose,并判断是否close了,using语句自动先close再dispose


SQL 注入漏洞攻击 ------where password = 'kai' or '1'='1';
需参数化查询 cmd.CommandText = "select * from where name = @name and password = @password";
cmd.Paraneters.Add(new SqlParaneters("@name",name)); cmd.Paraneters.Add(new SqlParaneters("@password",password);
MySql中用?做参数化查询的标记


winform中
this.AcceptButton = button1; this.CancelButton = button2;


NumericUpDown 控件 numericUpDown1.Maximun = 25; numericUpDown1.DecimalPlaces = 2;


SqlServer
getDate()获得当前日期 DateAdd(datepart,number,date) 参数分别为计量单位、数值,日期 datepart 可以为year\month\day\hh\yy等
DATEADD(MONTH,-8,getDate())当前日期向前8个月 DateDiff(datepart,startdate,enddate)计算日期的差值


select DateDiff(year,FinDate,getDate()),count(*) from T_Person group by DateDiff(year,FinDate,getDate());


cast类型转换
select Cast('123' as int),Cast('200-08-08' ad datetime) from T_Person; 或者Convert(datetime,'2009-09-09') convert(varchar(50),1234)


select IsNull(FName,'佚名') as 姓名 from T_Person 如果为空则返回佚名,否则返回FName


case的使用
select name,
(case level
when 1 then '普通用户'
when 2 then '会员'
when 3 then 'VIP'
else '未知客户' 此处无then!
end) as 客户类型 from T_Person; 注意sql语句中的case的括号为小括号!


select name,
(case 若case不是固定值,则case后无字段名
when salary<2000 then '低收入'
when salary>4000 then '高收入'
else '一般收入'
end) as 收入水平 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;


自动对查询结果添加序号
"SET @row_number =0; select @row_number := @row_number+1 AS 序号,data_name as 日期 from dicdata_type_info where id>5;


对于设有外键的值进行操作时需先取消其对外键的检查
set FOREIGN_KEY_CHECKS=0;
insert into dictionary_info (pid,data_name,data_type_id,del_flg,lastupdateuser,lastupdatetime)values(0,'" + data_name + "',200,0,1,'20120202');
set FOREIGN_KEY_CHECKS=1; 


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


创建日志的函数
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();
        }

你可能感兴趣的:(C#学习笔记)