官术网_书友最值得收藏!

How to do it...

  1. Create a new class called StarShip:
        public class Starship 
{

}
  1. To your class, add a new method that will set the maximum troop capacity of the StarShip class:
        public void SetMaximumTroopCapacity(int capacity) 
{

}
  1. Inside this method, add a trycatch clause that will attempt to set the maximum troop capacity, but for some reason, it will fail. Upon failure, it will write the error to the log table inside the database:
        try 
{
// Read current capacity and try to add more
}
catch (Exception ex)
{
string connectionString = "connection string goes here";
string sql = $"INSERT INTO tblLog (error, date) VALUES
({ex.Message}, GetDate())";
using (SqlConnection con = new
SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(sql);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
throw ex;
}
主站蜘蛛池模板: 岗巴县| 新竹县| 泸定县| 木兰县| 伊金霍洛旗| 革吉县| 磐安县| 南木林县| 巴林左旗| 威远县| 黄龙县| 屏边| 峨边| 和平区| 湘西| 景泰县| 探索| 石棉县| 安宁市| 临沧市| 大渡口区| 赣州市| 浙江省| 内江市| 巍山| 湘乡市| 宜昌市| 桂东县| 井陉县| 遂宁市| 昌乐县| 阿坝县| 武义县| 云和县| 太保市| 密云县| 湄潭县| 光泽县| 宁化县| 招远市| 彩票|