是否需要将ado.net数据提供程序数据类型转换为.net框架数据类型 [英] is it neccessary to cast ado.net data provider data types to .net framework data types

查看:58
本文介绍了是否需要将ado.net数据提供程序数据类型转换为.net框架数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我需要知道这两个代码段之间的真正区别是什么
哪个更好?


hi all
I need to know what is the real difference between these two code snippets
and which is better?


'create the connection and command objects
do while r.read = true
  memberid = convert.toint32(r.item(0))
  membername = convert.tostring(r.item(1))



而这个:



and this:

'create the connection and command objects
do while r.read = true
  memberid = r.item(0)
  membername = r.item(1)

推荐答案

Item 的返回类型SqlDataReader 的属性是Object ,如此处 http://msdn.microsoft.com/en-我们/library/y64yaah2.aspx [ ^ ]

Visual Studio中创建Visual Basic项目时,默认情况下Option Strict is off,因此允许从Object String Integer 的隐式转换,因此可以编译第二组语句. br/>
如果Option Strict已打开On (通过右键单击项目,选择属性菜单选项,并在属性页的编译"选项卡中),则将不会编译第二组语句,并且会出现以下错误将显示

Option Strict On不允许从Object到String的隐式转换,视情况可能为Integer.

在C Sharp中,必须进行显式强制转换.
因此,我认为,最好使用第一组语句.
The return type of Item property of SqlDataReader is Object as explained here http://msdn.microsoft.com/en-us/library/y64yaah2.aspx[^]

When a Visual Basic project is created in Visual Studio, by default the Option Strict is off, hence the implicit conversion from Object to String or Integer are allowed, hence, the second set of statements could be compiled.

If the Option Strict is turned on On (by right clicking on the project, selecting the properties menu option, and in the compile Tab of the property pages), then the second set of statements will not be compiled and the following error will be displayed

Option Strict On disallows implicit conversions from Object to String, Integer as case may be.

In C Sharp the Explicit cast is a must.
So, I think, it is preferable to use the first set of statements.


这篇关于是否需要将ado.net数据提供程序数据类型转换为.net框架数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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