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

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;
}
主站蜘蛛池模板: 崇信县| 镇雄县| 遂宁市| 宜川县| 云安县| 时尚| 黎平县| 怀宁县| 仁布县| 永靖县| 买车| 高安市| 资中县| 刚察县| 北川| 青田县| 蕉岭县| 浦北县| 淮滨县| 澄迈县| 常山县| 苏州市| 永春县| 襄樊市| 禄丰县| 治县。| 都兰县| 衡山县| 精河县| 普格县| 容城县| 凤城市| 城固县| 丰县| 友谊县| 平罗县| 德安县| 南充市| 新兴县| 鄂尔多斯市| 阳江市|