c# - 添加参数的两种情况有什么区别?

标签 c# asp.net sql performance informix

我想知道有什么区别:

 DBCmd.Parameters.Add("user_name", IfxType.VarChar);
 DBCmd.Parameters["user_name"].Value = p_u; 

DBCmd.Parameters.Add("user_name", p_u);

这里的最佳实践是什么,哪个更安全,一个比另一个表现更好?

最佳答案

在所示示例中,主要 区别在于它明确知道要使用的类型是 IfxType.VarChar - 这可能很重要,取决于具体情况,以及 IFX 是否将字符串默认为 CharVarCharLongVarChar。老实说,我不知道默认情况下它会选择哪一个。

显式通常是个好主意,但不需要通过索引器重新获取,因为新参数是从 Add 返回的;我可能会建议:

DBCmd.Parameters.Add("user_name", IfxType.VarChar).Value = p_u;

或者也许:

DBCmd.Parameters.Add("user_name", IfxType.VarChar, 20).Value = p_u;

其中 20 是参数的大小

关于c# - 添加参数的两种情况有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11966942/

相关文章:

mysql:select * where first and last name starts with the same letter

c# - .NET 如何通过套接字获取 int C-Sharp

css - 以 % 表示的 divs 高度在 ie 中不起作用

c# - 使用 angular-ui-router 避免 .NET MVC Controller 调用

asp.net - Response.Write() 和 Response.Output.Write() 有什么区别?

c# - 从 Javascript 调用 C# 方法

python - 在 python 中模拟类似文件的行为

sql - Sql Server 中的 XML 专用

c# - 比较从 NHibernate 加载的对象时,== 是否保证有效?

c# - 如何从 c# 中的 dob 计算年龄(以年为单位)