- 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;
}
推薦閱讀
- OpenStack Cloud Computing Cookbook(Third Edition)
- Visual C++程序設計教程
- Python爬蟲開發:從入門到實戰(微課版)
- Python從入門到精通(精粹版)
- Web開發的貴族:ASP.NET 3.5+SQL Server 2008
- Java從入門到精通(第5版)
- 數據結構與算法JavaScript描述
- 基于免疫進化的算法及應用研究
- Oracle JDeveloper 11gR2 Cookbook
- Mastering Linux Network Administration
- Visual Basic程序設計上機實驗教程
- Mastering ArcGIS Enterprise Administration
- Unity 2017 Game AI Programming(Third Edition)
- Oracle 12c從入門到精通(視頻教學超值版)
- SAS編程演義