50个提高C#编程水平的要诀
1.总是用属性 (Property) 来代替可访问的数据成员
2.在 readonly 和 const 之间,优先使用 readonly
3.在 as 和 强制类型转换之间,优先使用 as 操作符
4.使用条件属性 (Conditional Attributes) 来代替条件编译语句 #if
5.总是为自定义类重载 ToString 方法
6.区别值类型和引用类型
7.使用不可变的值类型(Immutable Atomic Value Types)
8.在值类型中,确保0是一个合法的数据
9.理解 ReferenceEquals, static Equals, instance Equals 和 比较运算符(==)之间的关系
10.理解 GetHashCode方法的缺陷
11.在编写循环时,优先使用 foreach.
12.在定义变量的时候就将其初始化
13.使用静态构造函数来初始化静态成员变量
14.用多个构造函数时,利用构造函数链
15.使用using和try/finally来处理资源的释放
16.尽量避免产生资源垃圾
17.尽量避免使用装箱(boxing)和拆箱(unboxing)
18.实现类的 Dispose 方法
19.在接口和继承(Inheritance)之间,优先使用接口(interface)
20.区分接口和重载(overrides)
21.用委托(delegate)来实现回调(callback)
22.用事件(event)来定义外部接口
23.避免返回类内部成员的引用
24.使用元数据来控制程序
25.优先使用可序列化(serilizable)类型
26.对需要排序的对象实现IComparable和IComparer接口
27.避免使用 ICloneable接口
28.避免使用类型转换操作符
29.只有当基类加入了与派生类中现有的函数名称相同的函数时,才需要使用 new 操作符
30.尽量使用 CLS-Compliant
31.尽量编写短少,简单的函数
32.尽量编写比较小的程序集(assembly)
33.限定类型的可见性(visibility)
34.编写大粒度的 web API
35.在使用事件时,优先继承基类事件,而不是重新创建一个事件
36.多使用 framework 的运行时调试 (DEBUG, TRACE, EVENTLOG等)
37.使用.net标准的配置机制
38.使用并且在类中支持.net的数据绑定功能 (Data Binding)
39.使用.net的验证机制 (Validation)
40.根据你的需求选择正确的集合类(Collection)
41.在自定义结构中使用 DataSet
42.利用属性(Attributes)
43.不要过度使用反射(Reflection)
44.创建完整的,应用程序特定的异常
45.尽可能多的考虑程序可能出现的异常,并作出处理
46.尽可能少的使用 Interop
47.尽量使用安全代码 (safe code)
48.多多学习、使用外部工具和资源
49.准备使用 C# 2.0
50.学习 ECMA 标准
ado.net实现对数据库的添加、删除和查看操作。提示:添加删除都不是查询所以我们可以使用SqlCommand 的ExecuteNonQuery完成。
程序源码:
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Data.SqlClient;
- namespace Infomation
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.TextBox txtFlightNum;
- private System.Windows.Forms.TextBox txtName;
- private System.Windows.Forms.TextBox txtCertificate;
- private System.Windows.Forms.TextBox txtSeatId;
- private System.Windows.Forms.TextBox txtAge;
- private System.Windows.Forms.Button btnAdd;
- private System.Windows.Forms.Button btnDelete;
- private System.Windows.Forms.Button btnCancel;
- private System.Windows.Forms.ComboBox cboSex;
- private System.Windows.Forms.Label lblFlightNum;
- private System.Windows.Forms.Label lblName;
- private System.Windows.Forms.Label lblSex;
- private System.Windows.Forms.Label lblCertificate;
- private System.Windows.Forms.Label lblSeatId;
- private System.Windows.Forms.Label lblAge;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- &n
bsp; components.Dispose(); - }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.lblFlightNum = new System.Windows.Forms.Label();
- this.lblName = new System.Windows.Forms.Label();
- this.lblSex = new System.Windows.Forms.Label();
- this.lblCertificate = new System.Windows.Forms.Label();
- this.lblSeatId = new System.Windows.Forms.Label();
- this.lblAge = new System.Windows.Forms.Label();
- this.txtFlightNum = new System.Windows.Forms.TextBox();
- this.txtName = new System.Windows.Forms.TextBox();
- this.txtCertificate = new System.Windows.Forms.TextBox();
- this.txtSeatId = new System.Windows.Forms.TextBox();
- this.txtAge = new System.Windows.Forms.TextBox();
- this.btnAdd = new System.Windows.Forms.Button();
- this.btnDelete = new System.Windows.Forms.Button();
- this.btnCancel = new System.Windows.Forms.Button();
- this.cboSex = new System.Windows.Forms.ComboBox();
- this.SuspendLayout();
- //
- // lblFlightNum
- //
- this.lblFlightNum.Location = new System.Drawing.Point(24, 24);
- this.lblFlightNum.Name = "lblFlightNum";
- this.lblFlightNum.Size = new System.Drawing.Size(48, 23);
- this.lblFlightNum.TabIndex = 0;
- this.lblFlightNum.Text = "航班号:";
- //
- // lblName
- //
- this.lblName.Location = new System.Drawing.Point(24, 80);
- this.lblName.Name&
nbsp;= "lblName"; - this.lblName.Size = new System.Drawing.Size(40, 23);
- this.lblName.TabIndex = 1;
- this.lblName.Text = "姓名:";
- //
- // lblSex
- //
- this.lblSex.Location = new System.Drawing.Point(24, 136);
- this.lblSex.Name = "lblSex";
- this.lblSex.Size = new System.Drawing.Size(40, 23);
- this.lblSex.TabIndex = 2;
- this.lblSex.Text = "性别:";
- //
- // lblCertificate
- //
- this.lblCertificate.Location = new System.Drawing.Point(256, 24);
- this.lblCertificate.Name = "lblCertificate";
- this.lblCertificate.Size = new System.Drawing.Size(48, 23);
- this.lblCertificate.TabIndex = 3;
- this.lblCertificate.Text = "证件号:";
- //
- // lblSeatId
- //
- this.lblSeatId.Location = new System.Drawing.Point(256, 80);
- this.lblSeatId.Name = "lblSeatId";
- this.lblSeatId.Size = new System.Drawing.Size(48, 23);
- this.lblSeatId.TabIndex = 4;
- this.lblSeatId.Text = "座位号:";
- //
- // lblAge
- //
- this.lblAge.Location = new System.Drawing.Point(256, 136);
- this.lblAge.Name = "lblAge";
- this.lblAge.Size = new System.Drawing.Size(40, 23);
- this.lblAge.TabIndex = 5;
- this.lblAge.Text = "年龄:";
- //
- // txtFlightNum
- //
- this.txtFlightNum.Location = new System.Drawing.Point(88, 24);
- this.txtFlightNum.Name =
pan class="string">"txtFlightNum"; - this.txtFlightNum.TabIndex = 6;
- this.txtFlightNum.Text = "";
- //
- // txtName
- //
- this.txtName.Location = new System.Drawing.Point(88, 80);
- this.txtName.Name = "txtName";
- this.txtName.Size = new System.Drawing.Size(136, 21);
- this.txtName.TabIndex = 7;
- this.txtName.Text = "";
- //
- // txtCertificate
- //
- this.txtCertificate.Location = new System.Drawing.Point(320, 24);
- this.txtCertificate.Name = "txtCertificate";
- this.txtCertificate.Size = new System.Drawing.Size(168, 21);
- this.txtCertificate.TabIndex = 8;
- this.txtCertificate.Text = "";
- //
- // txtSeatId
- //
- this.txtSeatId.Location = new System.Drawing.Point(320, 80);
- this.txtSeatId.Name = "txtSeatId";
- this.txtSeatId.Size = new System.Drawing.Size(168, 21);
- this.txtSeatId.TabIndex = 9;
- this.txtSeatId.Text = "";
- //
- // txtAge
- //
- this.txtAge.Location = new System.Drawing.Point(320, 136);
- this.txtAge.Name = "txtAge";
- this.txtAge.TabIndex = 10;
- this.txtAge.Text = "";
- //
- // btnAdd
- //
- this.btnAdd.Location = new System.Drawing.Point(192, 216);
- this.btnAdd.Name = "btnAdd";
- this.btnAdd.TabIndex = 11;
- this.btnAdd.Text = "添加";
-  
; this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click); - //
- // btnDelete
- //
- this.btnDelete.Location = new System.Drawing.Point(296, 216);
- this.btnDelete.Name = "btnDelete";
- this.btnDelete.TabIndex = 12;
- this.btnDelete.Text = "删除";
- this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
- //
- // btnCancel
- //
- this.btnCancel.Location = new System.Drawing.Point(400, 216);
- this.btnCancel.Name = "btnCancel";
- this.btnCancel.TabIndex = 13;
- this.btnCancel.Text = "取消";
- this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
- //
- // cboSex
- //
- this.cboSex.Items.AddRange(new object[] {
- "男",
- "女"});
- this.cboSex.Location = new System.Drawing.Point(88, 136);
- this.cboSex.Name = "cboSex";
- this.cboSex.Size = new System.Drawing.Size(121, 20);
- this.cboSex.TabIndex = 14;
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(512, 273);
- this.Controls.Add(this.cboSex);
- this.Controls.Add(this.btnCancel);
- this.Controls.Add(this.btnDelete);
- this.Controls.Add(this.btnAdd);
- this.Controls.Add(this.txtAge);
- > this.Controls.Add(this.txtSeatId);
- this.Controls.Add(this.txtCertificate);
- this.Controls.Add(this.txtName);
- this.Controls.Add(this.txtFlightNum);
- this.Controls.Add(this.lblAge);
- this.Controls.Add(this.lblSeatId);
- this.Controls.Add(this.lblCertificate);
- this.Controls.Add(this.lblSex);
- this.Controls.Add(this.lblName);
- this.Controls.Add(this.lblFlightNum);
- this.Name = "Form1";
- this.Text = "乘客详细信息";
- this.Load += new System.EventHandler(this.Form1_Load);
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- //创建数据库连接对象及命令对象
- private SqlConnection conn;
- private SqlCommand cmd;
- private void btnAdd_Click(object sender, System.EventArgs e)
- {
- string sql = "insert Flight values(‘"+this.txtFlightNum.Text+"’,'"+this.txtName.Text+"’,'"+this.cboSex.SelectedItem.ToString()+"’,'"+this.txtCertificate.Text+"’,'"+this.txtSeatId.Text+"’,"+int.Parse(this.txtAge.Text)+")";
- try
- {
- conn.Open();//打开数据库连接
- cmd = new SqlCommand(sql,conn);
- cmd.ExecuteNonQuery();//执行插入命令
- MessageBox.Show("数据添加成功");//提示执行结果
- }
- catch(SqlException ex)//捕获异常
- {
-  
; MessageBox.Show(ex.Message); - }
- finally
- {
- conn.Close();//关闭连接
- }
- }
- private void Form1_Load(object sender, System.EventArgs e)
- {
- //程序载入时创建数据库连接
- conn = new SqlConnection("server = .;uid = sa;pwd = ;database = pubs");
- }
- private void btnDelete_Click(object sender, System.EventArgs e)
- {
- string sql = "delete from Flight where FlightNum =’"+this.txtFlightNum.Text+"’";
- try
- {
- conn.Open();//打开连接
- cmd = new SqlCommand(sql,conn);
- cmd.ExecuteNonQuery();//执行删除数据操作
- MessageBox.Show("数据删除成功");//提示删除结果
- }
- catch(SqlException ex)//捕获异常
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();//关闭连接
- }
- }
- private void btnCancel_Click(object sender, System.EventArgs e)
- {
- Application.Exit();
- }
- }
- }
编程实现自制的DataSet 并在DataGird中显示提示:根据我们熟悉的 建库 建表 建字段 建约束 添加数据行 显示 这个步骤来。
程序源码:
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Data.SqlClient;
- namespace DataSetTest
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.DataGrid dataGrid1;
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.Button button2;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.dataGrid1 = new System.Windows.Forms.DataGrid();
- this.button1 = new
span> System.Windows.Forms.Button(); - this.button2 = new System.Windows.Forms.Button();
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
- this.SuspendLayout();
- //
- // dataGrid1
- //
- this.dataGrid1.DataMember = "";
- this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
- this.dataGrid1.Location = new System.Drawing.Point(8, 8);
- this.dataGrid1.Name = "dataGrid1";
- this.dataGrid1.Size = new System.Drawing.Size(384, 168);
- this.dataGrid1.TabIndex = 0;
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(82, 184);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(112, 23);
- this.button1.TabIndex = 1;
- this.button1.Text = "查看自建DataSet";
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(207, 184);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(116, 23);
- this.button2.TabIndex = 2;
- this.button2.Text = "查看数据库Jobs表";
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(400, 221);
- this.Controls.Add(this.button2);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.dataGrid1);
- this.Name = "Form1";
- this.Text = "Form1";
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void button1_Click(object sender, System.EventArgs e)
- {
- DataSet ds = new DataSet();//创建DataSet对象
- DataTable dt = new DataTable();//创建数据表
- DataColumn dc = dt.Columns.Add("编号",typeof(string));//为数据表添加字段
- dt.Columns.Add("姓名",typeof(string));//创建约束
- dt.Columns.Add("年龄",typeof(int));
- dt.Columns["编号"].AllowDBNull=false;
- dt.Columns["姓名"].Unique = true;
- dt.Columns["年龄"].DefaultValue=0;
- DataRow dr = dt.NewRow();//创建新的行
- dr["编号"] = "a100";//填充值
- dr["姓名"] = "neeke";
- dr["年龄"] = 20;
- dt.Rows.Add(dr);//加入到表的行集合中
- dr = dt.NewRow();
- dr["编号"] = "a101";
- dr["姓名"] = "jerry";
- dr["年龄"] = "19";
- dt.Rows.Add(dr);
- this.dataGrid1.DataSource = dt;//显示在dataGrid中
- }
- private void button2_Click(object sender, System.EventArgs e)
- {
- try
- {
- SqlConnection conn = new SqlConnection("server = .;uid = sa;pwd = ;database = pubs");//创建连接
- &n
bsp; SqlDataAdapter sda = new SqlDataAdapter();//创建适配器 - DataSet ds = new DataSet();//创建DataSet对象
- conn.Open();//打开连接
- sda.SelectCommand = new SqlCommand("select * from jobs",conn);//查询数据
- sda.Fill(ds,"jobs");//填充ds
- this.dataGrid1.DataSource = ds.Tables[0];//绑定并显示
- }
- catch(SqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }