如何编写从 ide 中删除注释的方法?
How to write a method to delete notes from a ide?
提议的代码旨在使用笔记应用程序的界面来删除所有笔记。但是,在编写此解决方案时,方法 DeleteAllNotes() 不会 return Note Repository DeleteAllNotes() 方法的代码值。
public Note DeleteAllNotes(int Id)
{
DataTable dt = new DataTable();
string sql = $@"DELETE all Notes where id = {Id}";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
它应该 运行 一段代码,执行时可以在行或列中删除整个 table 数据。
参考文献:
1)CS1061错误的解释:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1061
2) C# 中的方法
https://docs.microsoft.com/en-us/dotnet/csharp/methods
3) C# 中的静态 Class 成员
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members
4) 摘要和密封 Class 成员:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members
参考资料:微软官方文档。
如果您想从 Notes
table
中删除 Id
给出的 记录
//DONE: We delete, that why we return bool (has items actually been deleted), note Note
//TODO: if connStr is static, declare DeleteAllNotes as static as well
public bool DeleteAllNotes(int Id) {
//DONE: Syntax: correct one is "Delete From table Where condition(s)"
//DONE: SQL Parametrized; do not hardcode parameters - "{Id}"
string sql =
@"delete
from Notes
where id = @prm_id";
using (SqlConnection conn = new SqlConnection(connStr)) {
conn.Open();
//DONE: wrap IDisposable into using
using (SqlCommand cmd = new SqlCommand(sql, conn)) {
cmd.Parameters.Add("@prm_id", SqlDbType.Int).Value = Id;
return cmd.ExecuteNonQuery() > 0;
}
}
}
提议的代码旨在使用笔记应用程序的界面来删除所有笔记。但是,在编写此解决方案时,方法 DeleteAllNotes() 不会 return Note Repository DeleteAllNotes() 方法的代码值。
public Note DeleteAllNotes(int Id)
{
DataTable dt = new DataTable();
string sql = $@"DELETE all Notes where id = {Id}";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
它应该 运行 一段代码,执行时可以在行或列中删除整个 table 数据。
参考文献:
1)CS1061错误的解释: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1061
2) C# 中的方法 https://docs.microsoft.com/en-us/dotnet/csharp/methods
3) C# 中的静态 Class 成员 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members
4) 摘要和密封 Class 成员:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members
参考资料:微软官方文档。
如果您想从 Notes
table
Id
给出的 记录
//DONE: We delete, that why we return bool (has items actually been deleted), note Note
//TODO: if connStr is static, declare DeleteAllNotes as static as well
public bool DeleteAllNotes(int Id) {
//DONE: Syntax: correct one is "Delete From table Where condition(s)"
//DONE: SQL Parametrized; do not hardcode parameters - "{Id}"
string sql =
@"delete
from Notes
where id = @prm_id";
using (SqlConnection conn = new SqlConnection(connStr)) {
conn.Open();
//DONE: wrap IDisposable into using
using (SqlCommand cmd = new SqlCommand(sql, conn)) {
cmd.Parameters.Add("@prm_id", SqlDbType.Int).Value = Id;
return cmd.ExecuteNonQuery() > 0;
}
}
}