如何使用 ADO.NET、IDbConnection 和 IDbCommand 使 C# 代码更简洁?
无论如何,我可以使这个数据库代码更短吗?它工作得很好,但看起来非常冗长和重复。我认为将其中的一些内容包装在一种方法中会更好,但是还有其他改进可以缩短它吗?
using (IDbConnection theConn = GetDatabaseConnection())
using (IDbCommand theCmd = theConn.CreateCommand())
{
theCmd.CommandText = @"INSERT INTO table(one, two, three,four)
VALUES (@one,@two,@three,@four)";
var parameterOne = theCmd.CreateParameter();
parameterOne.ParameterName = "@one";
parameterOne.Value = "text";
theCmd.Parameters.Add(parameterOne);
var parameterTwo = theCmd.CreateParameter();
parameterTwo.ParameterName = "@two";
parameterTwo.Value = "text";
theCmd.Parameters.Add(parameterTwo);
var parameterThree = theCmd.CreateParameter();
parameterThree.ParameterName = "@three";
parameterThree.Value = "text";
theCmd.Parameters.Add(parameterThree);
var parameterFour = theCmd.CreateParameter();
parameterFour.ParameterName = "@four";
parameterFour.Value = "text";
theCmd.Parameters.Add(parameterFour);
theCmd.ExecuteNonQuery();
}
Is there anyway I can make this database code any shorter ? It works fine, but seems very verbose and repetitive. Wrapping some of it in a method would be better I suppose, but is there other improvement that could be made to shorten it ?
using (IDbConnection theConn = GetDatabaseConnection())
using (IDbCommand theCmd = theConn.CreateCommand())
{
theCmd.CommandText = @"INSERT INTO table(one, two, three,four)
VALUES (@one,@two,@three,@four)";
var parameterOne = theCmd.CreateParameter();
parameterOne.ParameterName = "@one";
parameterOne.Value = "text";
theCmd.Parameters.Add(parameterOne);
var parameterTwo = theCmd.CreateParameter();
parameterTwo.ParameterName = "@two";
parameterTwo.Value = "text";
theCmd.Parameters.Add(parameterTwo);
var parameterThree = theCmd.CreateParameter();
parameterThree.ParameterName = "@three";
parameterThree.Value = "text";
theCmd.Parameters.Add(parameterThree);
var parameterFour = theCmd.CreateParameter();
parameterFour.ParameterName = "@four";
parameterFour.Value = "text";
theCmd.Parameters.Add(parameterFour);
theCmd.ExecuteNonQuery();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不想使用完整的 OR/M,为什么不尝试 Dapper ?
If you don't want use a full OR/M why dont try Dapper?
如果您正在寻找一般性更改,我建议使用 Microsoft Entity Framework(或任何其他 OR/M)。
这将导致代码少得多,因为您不必再次编写这种低级代码。
它可以很好地与许多数据库配合使用。您可以通过相对较小的更改来更改底层数据库。
如果您只是想对此特定方法进行更改,那么我只会采用您已经提到过的解决方案。继续将每个重复段落重构为一个方法。
From:
Into (例如):
另外,在调用mm方法时,我更喜欢为参数名称定义一些常量,而不是每次都重新写入字符串。这使我避免了拼写错误。
如果您在不止此方法中使用它们(我想肯定会是这种情况),请将它们定义在适当的范围内,例如作为类成员。
If you are looking for a general change, I recommend using the Microsoft Entity Framework (or any other OR/M).
This will result in much less code as you do not have to write this kind of low level code again.
And it works well with many, many databases. You can change the underlying database with relatively small changes.
If you are just looking for a change in this particular method, well, then I'd just go with the solution you already mentioned yourself. Go ahead and refactor each repeating paragraph into a method.
From:
Into (for example):
Also, when calling the mmethod, I prefer defining some constants for the parameter names instead of writing the string each time again. This spares me of spelling mistakes.
Define them in an appropriate scope, like as class members, if you use them in more than this method (which I guess will sure be the case).