什么是参数传递给SqlCommand的最佳方法是什么? [英] What's the best method to pass parameters to SQLCommand?
本文介绍了什么是参数传递给SqlCommand的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是参数传递给SqlCommand的最佳方法是什么?你可以这样做:
What's the best method to pass parameters to SQLCommand? You can do:
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = "Bob";
或
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = "Bob";
或
cmd.Parameters.Add("@Name").Value = "Bob";
这似乎是第一个可能是某种方式更好无论是性能明智或错误检查,明智的。不过,我想知道更明确。
It seems like the first one might be somehow "better" either performance-wise or error checking-wise. But I would like to know more definitively.
推荐答案
您也可以使用 AddWithValue()
,但要注意的错误隐式类型转换的可能性
You can also use AddWithValue()
, but be aware of the possibility of the wrong implicit type conversion.
cmd.Parameters.AddWithValue("@Name", "Bob");
这篇关于什么是参数传递给SqlCommand的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文