- 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;
}
推薦閱讀
- Node.js Design Patterns
- Java程序設計
- UML 基礎與 Rose 建模案例(第3版)
- 深度學習:Java語言實現
- Python爬蟲、數據分析與可視化:工具詳解與案例實戰
- Creating Data Stories with Tableau Public
- Node.js開發指南
- Red Hat Enterprise Linux Troubleshooting Guide
- 一步一步跟我學Scratch3.0案例
- 交互式程序設計(第2版)
- SQL Server 2012 數據庫應用教程(第3版)
- Java EE項目應用開發
- Java程序設計入門(第2版)
- Mobile Test Automation with Appium
- 像程序員一樣使用MySQL