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

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;
}
主站蜘蛛池模板: 绥江县| 泰安市| 象州县| 焉耆| 东台市| 茌平县| 响水县| 嘉义县| 射洪县| 弋阳县| 宝坻区| 马尔康县| 新余市| 牙克石市| 盐亭县| 浦城县| 景谷| 四会市| 尖扎县| 乐陵市| 永善县| 洛南县| 新沂市| 沙坪坝区| 庄河市| 防城港市| 达孜县| 宝清县| 汉寿县| 长岭县| 井冈山市| 黎川县| 北安市| 双城市| 穆棱市| 黔江区| 平度市| 信阳市| 武平县| 五台县| 原平市|