- C# 7 and .NET Core Cookbook
- Dirk Strauss
- 134字
- 2021-07-03 00:12:03
How to do it...
- Create a new class called StarShip:
public class Starship
{
}
- To your class, add a new method that will set the maximum troop capacity of the StarShip class:
public void SetMaximumTroopCapacity(int capacity)
{
}
- 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;
}
推薦閱讀
- PaaS程序設計
- Java應用開發與實踐
- Python Game Programming By Example
- Python程序設計案例教程
- 概率成形編碼調制技術理論及應用
- 數據結構與算法分析(C++語言版)
- Procedural Content Generation for C++ Game Development
- Julia數據科學應用
- Web程序設計:ASP.NET(第2版)
- 交互式程序設計(第2版)
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- Mastering JavaScript
- Yii2 By Example
- 微信小程序開發邊做邊學(微課視頻版)
- Mastering XenApp?