- 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;
}
推薦閱讀
- Advanced Splunk
- AngularJS Web Application Development Blueprints
- ASP.NET Core Essentials
- Visual Basic程序設計習題解答與上機指導
- Reactive Programming With Java 9
- Functional Kotlin
- C#程序設計
- Unity 2017 Mobile Game Development
- 持續(xù)輕量級Java EE開發(fā):編寫可測試的代碼
- 編寫高質量代碼:改善Objective-C程序的61個建議
- Mastering Python Design Patterns
- C語言程序設計教程
- Microsoft Windows Identity Foundation Cookbook
- Python程序設計教程
- Learn C Programming