Pro*_*ear 1 c# sql asp.net coding-style naming-conventions
我很擅长使用C#和ASP.NET,我很好奇是否有一个约定在SQL数据库上运行多个查询时命名SqlCommands.例如,我创建了我的SqlConnection,我希望调用一个函数,两个存储过程,并创建一个常规的简单查询.目前,我正在使用:
SqlCommand function = new SqlCommand();
SqlCommand command = new SqlCommand();
SqlCommand proc1 = new SqlCommand();
SqlCommand proc2 = new SqlCommand();
Run Code Online (Sandbox Code Playgroud)
对于这些不同的命令是否有更多的接受命名约定,或者我应该使用单个命令,因为我在后面的代码块中使用CommandText和CommandType调用?
如果在同一范围内有很多命令,则可以使用personCommand或等名称productCommand.从MSDN常规命名约定中,您可以:
请选择易读的标识符名称.例如,名为HorizontalAlignment的属性比AlignmentHorizontal更具英文可读性.
在简洁方面倾向于可读性.属性名称CanScrollHorizontally优于ScrollableX(对X轴的模糊引用).
请勿使用下划线,连字符或任何其他非字母数字字符.X请勿使用匈牙利表示法.
使用与广泛使用的编程语言的关键字冲突的标识符进行避免.
了解有关C#编码约定的更多信息.在其他情况下,我更喜欢使用只是command以保持简单,因为范围会告诉我.
另一个很好的提示,当你用它实现的工作类型IDisposable接口一样SqlCommand,SqlConnection你可以使用using() { }结构这个范围后处置的对象.样品:
public IEnumerable<Person> GetPersons()
{
var result = new List<Person>();
using (var connection = new SqlConnection("connection string"))
{
// I know it is a person command, because it is in a method for it.
// Keep it simple!
using (var command = new SqlCommand("select id, name from persons", connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add(new Person() {
Id = (int) reader["id"];
Name = reader["name"] as string;
});
}
} // reader is disposed here
} // command is disposed here
} // connection is disposed here
return result;
}
Run Code Online (Sandbox Code Playgroud)
编码惯例还有很多.请参阅参考文献中的链接.
| 归档时间: |
|
| 查看次数: |
800 次 |
| 最近记录: |