[英]C# SqlCommand naming convention
I'm fairly new to working with C# and ASP.NET and I'm curious if there is a convention to naming the SqlCommands when more than one query is being run on a SQL database.我对使用 C# 和 ASP.NET 还很陌生,我很好奇在 SQL 数据库上运行多个查询时是否有命名 SqlCommands 的约定。 For example, I have created my SqlConnection and I wish to call a function, two stored procedures, and just create a regular simple query.
例如,我已经创建了我的 SqlConnection 并且我希望调用一个函数、两个存储过程,并且只创建一个常规的简单查询。 Currently, I am using:
目前,我正在使用:
SqlCommand function = new SqlCommand();
SqlCommand command = new SqlCommand();
SqlCommand proc1 = new SqlCommand();
SqlCommand proc2 = new SqlCommand();
Is there a more accepted naming convention for these different commands or should I just use a single command as I am using the CommandText and CommandType calls in the later code blocks?对于这些不同的命令是否有更可接受的命名约定,或者我应该只使用单个命令,因为我在后面的代码块中使用 CommandText 和 CommandType 调用?
If you have a lot of commands in the same scope, you could have names like personCommand
or productCommand
.如果您在同一范围内有很多命令,您可以使用类似
personCommand
或productCommand
名称。 From the MSDN General Naming Conventions you could:从MSDN 通用命名约定,您可以:
DO choose easily readable identifier names.
请选择易于阅读的标识符名称。 For example, a property named HorizontalAlignment is more English-readable than AlignmentHorizontal.
例如,名为 HorizontalAlignment 的属性比 AlignmentHorizontal 更易读英文。
DO favor readability over brevity.
一定要重视可读性而不是简洁。 The property name CanScrollHorizontally is better than ScrollableX (an obscure reference to the X-axis).
属性名称 CanScrollHorizontally 优于 ScrollableX(对 X 轴的模糊引用)。
DO NOT use underscores, hyphens, or any other nonalphanumeric characters.
请勿使用下划线、连字符或任何其他非字母数字字符。 X DO NOT use Hungarian notation.
X 不要使用匈牙利符号。
AVOID using identifiers that conflict with keywords of widely used programming languages.
避免使用与广泛使用的编程语言的关键字冲突的标识符。
See more about C# Coding Conventions .请参阅有关C# 编码约定的更多信息。 In other cases, I prefer using just
command
to Keep It Simple , because the scope will tell me.在其他情况下,我更喜欢只使用
command
来保持简单,因为范围会告诉我。
Another good tip, when you work with types which implements the IDisposable
interface like SqlCommand
, SqlConnection
, you can use the using() { }
structure to dispose the object after this scope.另一个很好的提示,当您使用实现
IDisposable
接口(如SqlCommand
、 SqlConnection
,您可以使用using() { }
结构在此范围之后处理对象。 For sample:样品:
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;
}
There is much more about coding convention.还有更多关于编码约定的内容。 See the links at references.
请参阅参考资料中的链接。
A good and more readable convention would be to express what the command will do.一个好的和更易读的约定是表达命令将做什么。 If you read updateProductCommand or queryCategoryCommand everybody instantly knows the purpose of the command.
如果您阅读updateProductCommand或queryCategoryCommand,每个人都会立即知道该命令的用途。 When you target a stored procedure it´s always good to use the sproc name as the command prefix like sp_UpdateProductCommand .
当您以存储过程为目标时,最好使用 sproc 名称作为命令前缀,如sp_UpdateProductCommand 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.