编程思想之一

  编程思想:编程时要对“不可能发生的情况” 做处理!这是一个编程的好习惯。

  如:

    在编写登陆时  

   

 1 public void LogOn(string userName)
 2         {
 3             DataSet dataSet = SqlHelper.ExecuteDataSet(
 4                 "select * from users where username=@UserName", new SqlParameter("@UserName", userName));
 5             DataTable table = dataSet.Tables[0];
 6             if (table.Rows.Count <= 0)
 7             {
 8                 throw new Exception("用户不存在!");
 9             }
10 
11             if (table.Rows.Count > 1)
12             {
13                 throw new Exception("用户名重复");
14             }
15         }

  对Rows.Count < 0 以及 Rows.Count > 0 的情况都做处理,可以避免很多麻烦。

    

分享每天的收获之一种快乐。
原文地址:https://www.cnblogs.com/TXZkuaizi/p/3449695.html