文章目录
C#新手学习笔记
由于工作需求,需要做C#项目,因算是新手入门吧
代码环境
快捷键
ctrl + F5 运行
table 生产代码 如cw table --> 生成console.writeLine();
Alt + Enter 修补代码
Shift”+“Alt”+“=”或“Shift”+“Alt”+“-” 快速展开或合拢选定文本内容
shift alt enter 全屏展示
shit enter 补代码之后换行
ctrl +H 替换
注释 ctrl k c 解除注释ctrl k u
Ctrl+K,P: 参数信息
Ctrl+E,D(ctrl+k,d) —-格式化全部代码 ;让你的代码瞬间整洁起来。
Ctrl+U: 全部变为小写 (的sql语句全部转换成大写以提高性能 )
Ctrl+Shift+U: 全部变为大写 (U表示Upper )
Ctrl+J /Ctrl+K,L: 智能提示 列出成员 (kernel核心内容 list列表 如果我们想查看一个对象具有的成员具体信息的时候试下这个快捷键吧)
ctrl+shift+f10:自动添加using命名空间(在实例化对象的时候,使用的非常多)
内存相关
- 引用类型与值类型
Dog dog; 此时将地址存入栈中;
dog =new Dog(); 当实例化时,将在为其在堆上分配空间;并把地址保存到栈上那片小内存中;
C#基础
与Java不同点
-
与 Java 不同的是,文件名可以不同于类的名称。
-
注释:文档注释、代码片段注释
- /// 文档注释
- region endregion 代码片段注释
-
方法名首字母也大写
1、第一个C#程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_one //引用的就是namespace
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("你好");
Console.Read(); //按键
Console.ReadKey(); //等待按键动作
}
}
}
2、注释
-
文档注释 ///
-
region endregion 代码片段注释
#region 测试方法集合
/// <summary>
/// 测试一
/// </summary>
public static void Test1()
{
}
/// <summary>
/// 测试二
/// </summary>
public static void Test2()
{
}
#endregion
}
3、变量
-
占位符 {0} 与 $
/// <summary> /// 变量 /// </summary> /// <param name="args"></param> static void Main(string[] args) { char a = 'a'; int price = 100; String room = "总统套房"; Console.WriteLine("helloworld"); Console.WriteLine("字符变量char : {0}",a); //占位符{} Console.WriteLine("房间类型{0},价格{1}",room,price); //占位符$ Console.WriteLine($"房间类型{room},价格{price}"); }
-
转义符 {{}}
string msg = $"今天早上碰见了:{{{name}}}";
4、修饰符
5、类
继承: “:”
属性
6、调试
7、引用
关键字为:ref
8、字符串操作
-
IndexOf():查找某个字符在字符串中的位置(下标0开始),找不到的话是-1
-
SubString(): 从字符串中截图字符串
-
ToLower():将字符串转换为小写形式
-
ToUpper():将字符转换为大写形式
-
Equals():比较两个字符串的值是否相等
-
Join():连接字符串
-
Trim():去字符串两边的空格
9、c#数据结构
- 集合: ArrayList Hashtable
- 泛型集合: List Dictionary<T,K>
操作数据库
- 使用ADO.net 技术完成c#对数据库的访问操作(添加,删除,修改,查询)
using System.Data.SqlClient; //操作数据库
/// <summary>
/// 用户登录事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//获取界面信息
string qq=this.textQQ.Text;
string pwd = this.textPwd.Text;
// select count(1) from UserInfos where qq = 10001 and pwd = '123456'
//第一步,创建数据库连接对象 ADO.NET
//访问地址 server=.;uid= zhangsan;database=QQDB
SqlConnection conn = new SqlConnection("server=.;uid=zhangsan;pwd=123456;database=QQDB");
//第二步,打开连接
conn.Open();
//第三部,创建执行脚本对象
string sql = $"select count(1) from UserInfos where qq = {qq} and pwd = '{pwd}'";
SqlCommand command = new SqlCommand(sql, conn);
//第四步,执行脚本
//command.ExecuteNonQuery();//添加删除修改
//command.ExecuteReader();//查询返回多行多列
int result= (int)command.ExecuteScalar();//查询返回第一行第一列
if (result > 0)
{
//MessageBox.Show("登录成功");
FriendForm friendForm = new FriendForm();
friendForm.Show();
this.Hide();//隐藏
this.Visible = false;
}
else
{
MessageBox.Show("登录失败");
}
//第五步,关闭连接
conn.Close();
}
总结一下
C#操作数据库流程
- 首先将导入操作数据库包 using System.Data.SqlClient; //操作数据库
- 然后创建数据库connection对象,并打开连接
- 创建执行脚本对象
- 执行脚本
- 通用方法: command.ExecuteReader();//查询返回多行多列
- int result= (int)command.ExecuteScalar();//查询返回第一行第一列
- 关闭连接
优化:防止sql注入
- 通过@占位符来防止sql注入问题
SqlParameter paral = new SqlParameter("@qq", qq);
SqlParameter para2 = new SqlParameter("@pwd", pwd);
string sql = $"select count(1) from UserInfos where qq = @qq and pwd = @pwd";
//第三部,创建执行脚本对象
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.Add(paral);
command.Parameters.Add(para2);
ew SqlParameter("@pwd", pwd);
string sql = $"select count(1) from UserInfos where qq = @qq and pwd = @pwd";
//第三部,创建执行脚本对象
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.Add(paral);
command.Parameters.Add(para2);
序列化
[XmlRootAttribute("MySchool")] // 当该类为Xml根节点时,以此为根节点名称。
public class School
[XmlAttribute("AreaName")] // 表现为Xml节点属性。<... AreaName="..."/>
public string Name
[XmlElementAttribute("Num", IsNullable = false)] // 表现为Xml节点。<Num>...</Num>
public int StudentNum
[XmlArrayAttribute("Students")] // 表现为Xml层次结构,根为Areas,其所属的每个该集合节点元素名为类名。<Students><Student ... /><Student ... /></Students>
List<Student> listStudent
[XmlArrayAttribute("Students")]
Student[] Students
[XmlElementAttribute("Area", IsNullable = false)] // 表现为水平结构的Xml节点。<Student ... /><Student ... />...
List<Student> listStudent
XmlElementAttribute("Area", IsNullable = false)]
Student[] Students
[XmlIgnoreAttribute] // 忽略该元素的序列化。
public class Classes
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public int StudentNum { get; set; }
[XmlElement("Student")]
public List<Student> listStudent { get; set; }
[XmlElement("GoodStudent")]
public List<Student> listGoodStudent { get; set; }
}
隐藏标签
//员工
public class Employee
{
public string EmpName { get; set; }
public string EmpSalary { get; set; }
}
//部门
[XmlRoot("Depertment")]
public class Department
{
public string DeptName { get; set; }
public List<Employee> Employees { get; set; }
}
不隐藏节点
<?xml version=\"1.0\" encoding=\"utf - 8\"?>
<Depertment>
<DeptName>研发部</DeptName>
<!-- Employees 节点-->
<Employees>
<Employee>
<EmpName>张三</EmpName>
<EmpSalary>10000</EmpSalary>
</Employee>
<Employee>
<EmpName>李四</EmpName>
<EmpSalary>10000</EmpSalary>
</Employee>
</Employees>
</Depertment>
如果需要隐藏节点如图所示
<?xml version=\"1.0\" encoding=\"utf - 8\"?>
<Depertment>
<DeptName>研发部</DeptName>
<!-- 隐藏了Employees节点-->
<Employee>
<EmpName>张三</EmpName>
<EmpSalary>10000</EmpSalary>
</Employee>
<Employee>
<EmpName>李四</EmpName>
<EmpSalary>10000</EmpSalary>
</Employee>
</Depertment>
做如下处理即可
//员工
public class Employee
{
public string EmpName { get; set; }
public string EmpSalary { get; set; }
}
//部门
[XmlRoot("Depertment")]
public class Department
{
public string DeptName { get; set; }
//将Employees节点名设成子节点名
[XmlElement("Employee")]
public List<Employee> Employees { get; set; }
}