区别与Parameters.Add和Parameters.AddWithValue [英] Difference with Parameters.Add and Parameters.AddWithValue

查看:525
本文介绍了区别与Parameters.Add和Parameters.AddWithValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上命令参数和参数有一个像添加 AddWithValue ,等在我见过的所有教程,我通常会发现,他们使用的是添加而不是 AddWithValue

Basically Commands has Parameters and parameters has functions like Add, AddWithValue, and etc. In all tutorials i've seen, i usually noticed that they are using Add instead of AddWithValue.

.Parameters.Add("@ID", SqlDbType.Int)

VS

.Parameters.AddWithValue("@ID", 1)

有没有理由不使用 AddWithValue ?我想preFER使用,超过

Is there a reason NOT to use AddWithValue? I'd prefer to use that over

Parameters.Add("@ID", SqlDbType.Int, 4).Value = 1

,因为它节省了我的编码时间。因此,这是更好地使用?这是安全使用?它是否可以提高性能?​​

since it saves my coding time. So which is better to use? Which is safe to use? Does it improves performance?

推荐答案

通过添加()方法,你可以限制用户输入指定数据的类型和长度 - 尤其是对 VARCHAR 列。

With Add() method you may restrict user input by specifying type and length of data - especially for varchar columns.

.Parameters.Add("@name",SqlDbType.VarChar,30).Value=varName;

在案件的<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue%28v=vs.90%29.aspx"相对=nofollow> AddWithValue()的(隐式的值变换)方法,它发送nvarchar的值到数据库中。

In case of AddWithValue() (implicit conversion of value) method, it sends nvarchar value to the database.

这篇关于区别与Parameters.Add和Parameters.AddWithValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆