数据类型转换的问题...... [英] Problems with data type conversions...

查看:71
本文介绍了数据类型转换的问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个名为CreateSQLParam的函数。它将SQL参数添加到

集合中。


该函数如下所示...我使用
$为集合添加参数b $ b以下行代码...


----------------------------- -------------------------------------------------- -----

dim vcContractNo as varchar

dim colParms as collection

vcContractNo =" 07-00001"


''为集合添加一个参数

colParms.Add(CreateSQLParam(" @ vcContractNo",ContractNo,SqlDbType.VarChar,

ParameterDirection 。输入)

--------------------------------------- ---------------------------------------------

我在上面一行的ContractNo字段中收到错误说明

" option strict on

不允许缩小类型''object' '在复制ByRef参数sValue的

值时键入''string''返回垫子ching argument


这是函数...

-------------------- -------------------------------------------------- -------------------

函数CreateSQLParam(ByVal sName As String,ByRef sValue As Object,ByVal

varType As System.Data.SqlDbType,ByVal varDir As ParameterDirection)As

SqlClient.SqlParameter


Dim objParam As SqlClient.SqlParameter

objParam = New SqlClient.SqlParameter()

objParam.ParameterName = sName


If IsNothing(sValue)then sValue = System.DBNull.Value


objParam.Value = sValue

objParam.SqlDbType = varType

objParam.Direction = varDir

CreateSQLParam = objParam


结束功能

---------------------------- -------------------------------------------------- -------------


所以在查看函数时,我将varchar值(ContractNo)传递给

sValue whi ch已被定义为一个对象,因此被定义为错误消息。

没有转动选项严格关闭,保持我的

泛型函数的最佳方法是什么我可以传递

函数sValue参数所需的任何数据类型吗?它必须以某种方式表示我需要明确地说明

定义变量的类型而不是使用对象但是如何和

我会在哪里这样做?


帮助!!


谢谢,布拉德

解决方案

Brad,


首先,由于你没有得到答案,你可以确定没有人知道答案,所以

请不要重复。


你有两个问题,为什么你用ref,我知道你是b / b
解释那个错误其次为什么你认为当你告诉那个

你想传递一个它将传递其他所有内容的对象,因为

实例值?


Cor

" Brad Pears" < br *** @ truenorthloghomes.comschreef在bericht

新闻:%2 ****************** @ TK2MSFTNGP03.phx.gbl .. 。


>我正在使用一个名为CreateSQLParam的函数。它将SQL参数添加到一个集合中。


该函数如下所示...我使用

跟随一个参数添加到集合中行代码......


--------------------------------- -------------------------------------------------- -

dim vcContractNo as varchar

dim colParms as collection

vcContractNo =" 07-00001"


''为集合添加一个参数

colParms.Add(CreateSQLParam(" @vcContractNo",ContractNo,

SqlDbType.VarChar,ParameterDirection.Input)

------------------------------------------- -----------------------------------------

我我在上面一行的ContractNo字段中收到错误

表示选项严格

不允许从类型''对象'缩小到'类型' 'string''将ByRef参数" sValue"的

值复制回匹配的arg ument"


这是函数......

--------------------- -------------------------------------------------- ------------------

函数CreateSQLParam(ByVal sName As String,ByRef sValue As Object,

ByVal varType As System.Data.SqlDbType,ByVal varDir As

ParameterDirection)As SqlClient.SqlParameter


Dim objParam As SqlClient.SqlParameter

objParam =新的SqlClient.SqlParameter()

objParam.ParameterName = sName


如果IsNothing(sValue)则sValue = System.DBNull.Value


objParam.Value = sValue

objParam.SqlDbType = varType

objParam.Direction = varDir

CreateSQLParam = objParam


结束功能

----------------------------- -------------------------------------------------- ------------


所以在查看函数时,我将一个varchar值(ContractNo)

传递给sValue这是def作为一个对象,因此错误信息。

没有转动选项严格关闭,保持我的

通用功能的最佳方法是什么,以便我可以传递

函数sValue参数所需的任何数据类型?它必须以某种方式表示我需要明确地说明

定义变量的类型而不是使用对象但是如何和

我会在哪里这样做?


帮助!!


谢谢,布拉德



Brad Pears写道:


我正在使用一个名为CreateSQLParam的函数。它将SQL

参数添加到集合中。


函数如下所示...我在集合中添加一个参数

使用以下行代码...


----------------------------- -------------------------------------------------- -----

dim vcContractNo as varchar

dim colParms as collection

vcContractNo =" 07-00001"



但它是字符串在VB.NET中,不是varchar...


Andrew


Brad Pears写道:
< blockquote class =post_quotes>
我正在使用一个名为CreateSQLParam的函数。它将SQL参数添加到

集合中。


该函数如下所示...我使用
$为集合添加参数b $ b以下行代码...


----------------------------- -------------------------------------------------- -----

dim vcContractNo as varchar

dim colParms as collection

vcContractNo =" 07-00001"


''为集合添加一个参数

colParms.Add(CreateSQLParam(" @ vcContractNo",ContractNo,SqlDbType.VarChar,

ParameterDirection 。输入)

--------------------------------------- ---------------------------------------------

我在上面一行的ContractNo字段中收到错误说明

" option strict on

不允许缩小类型''object' '在复制ByRef参数" sValue"的

值时,键入''string''回到matchi ng参数


这是函数...

-------------------- -------------------------------------------------- -------------------

函数CreateSQLParam(ByVal sName As String,ByRef sValue As Object,ByVal

varType As System.Data.SqlDbType,ByVal varDir As ParameterDirection)As

SqlClient.SqlParameter


Dim objParam As SqlClient.SqlParameter

objParam = New SqlClient.SqlParameter()

objParam.ParameterName = sName


If IsNothing(sValue)then sValue = System.DBNull.Value


objParam.Value = sValue

objParam.SqlDbType = varType

objParam.Direction = varDir

CreateSQLParam = objParam


结束功能

---------------------------- -------------------------------------------------- -------------


所以在查看函数时,我将varchar值(ContractNo)传递给

sValue是en定义为一个对象,因此定义了错误消息。

没有转动选项严格关闭,保持我的

泛型函数的最佳方法是什么,以便我可以传递

函数sValue参数所需的任何数据类型吗?它必须以某种方式表示我需要明确地说明

定义变量的类型而不是使用对象但是如何和

我会在哪里这样做?


帮助!!


谢谢,Brad



当您通过引用传递引用时,

引用的数据类型必须完全匹配。但是,如果您不打算在方法中更改

引用,则没有理由通过引用传递

引用。只需删除ByRef关键字。


您也可以删除ByVal关键字,这是传递

参数的默认方式。坚持默认,除非你确实需要

来通过引用传递一个参数。


-

G?ran Andersson

_____
http://www.guffa.com


I am using a function called "CreateSQLParam" which adds SQL parameters to a
collection.

The function is shown below... I add a parameter to a collection using the
following line code...

------------------------------------------------------------------------------------
dim vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"

'' Add a paramter to the collection
colParms.Add(CreateSQLParam("@vcContractNo", ContractNo, SqlDbType.VarChar,
ParameterDirection.Input)
------------------------------------------------------------------------------------
I am getting an error on the "ContractNo" field in the above line that says
"option strict on
disallows narrowing from type ''object'' to type ''string'' in copying the
value of ByRef parameter "sValue" back to the matching argument"

Here is the function...
-----------------------------------------------------------------------------------------
Function CreateSQLParam(ByVal sName As String, ByRef sValue As Object, ByVal
varType As System.Data.SqlDbType, ByVal varDir As ParameterDirection) As
SqlClient.SqlParameter

Dim objParam As SqlClient.SqlParameter
objParam = New SqlClient.SqlParameter()
objParam.ParameterName = sName

If IsNothing(sValue) Then sValue = System.DBNull.Value

objParam.Value = sValue
objParam.SqlDbType = varType
objParam.Direction = varDir
CreateSQLParam = objParam

End Function
-------------------------------------------------------------------------------------------

So in looking at the function, I am passing a varchar value (ContractNo) to
sValue which has been defined as an object and hence the error message.
Short of turning "option strict OFF", what is the best way to keep my
generic function so that I can pass whatever data type is required to the
functions sValue parameter? It must somehow mean I need to explicitely
define the type of variable coming in isntead of using object but how and
where would I do this?

Help!!

Thanks, Brad


解决方案

Brad,

First as you get no answers, you can be sure nobody knows the answer, so
please don''t repeat.

Two questions to you, why you are using by ref, I have the idea that you are
interpreting that wrong secondly why do you think that when you tell that
you want to pass an object that it will pass everything else too as by
instance values?

Cor
"Brad Pears" <br***@truenorthloghomes.comschreef in bericht
news:%2******************@TK2MSFTNGP03.phx.gbl...

>I am using a function called "CreateSQLParam" which adds SQL parameters to
a collection.

The function is shown below... I add a parameter to a collection using the
following line code...

------------------------------------------------------------------------------------
dim vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"

'' Add a paramter to the collection
colParms.Add(CreateSQLParam("@vcContractNo", ContractNo,
SqlDbType.VarChar, ParameterDirection.Input)
------------------------------------------------------------------------------------
I am getting an error on the "ContractNo" field in the above line that
says "option strict on
disallows narrowing from type ''object'' to type ''string'' in copying the
value of ByRef parameter "sValue" back to the matching argument"

Here is the function...
-----------------------------------------------------------------------------------------
Function CreateSQLParam(ByVal sName As String, ByRef sValue As Object,
ByVal varType As System.Data.SqlDbType, ByVal varDir As
ParameterDirection) As SqlClient.SqlParameter

Dim objParam As SqlClient.SqlParameter
objParam = New SqlClient.SqlParameter()
objParam.ParameterName = sName

If IsNothing(sValue) Then sValue = System.DBNull.Value

objParam.Value = sValue
objParam.SqlDbType = varType
objParam.Direction = varDir
CreateSQLParam = objParam

End Function
-------------------------------------------------------------------------------------------

So in looking at the function, I am passing a varchar value (ContractNo)
to sValue which has been defined as an object and hence the error message.
Short of turning "option strict OFF", what is the best way to keep my
generic function so that I can pass whatever data type is required to the
functions sValue parameter? It must somehow mean I need to explicitely
define the type of variable coming in isntead of using object but how and
where would I do this?

Help!!

Thanks, Brad



Brad Pears wrote:

I am using a function called "CreateSQLParam" which adds SQL
parameters to a collection.

The function is shown below... I add a parameter to a collection
using the following line code...

------------------------------------------------------------------------------------
dim vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"

But it''s "String" in VB.NET, not "varchar"...

Andrew


Brad Pears wrote:

I am using a function called "CreateSQLParam" which adds SQL parameters to a
collection.

The function is shown below... I add a parameter to a collection using the
following line code...

------------------------------------------------------------------------------------
dim vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"

'' Add a paramter to the collection
colParms.Add(CreateSQLParam("@vcContractNo", ContractNo, SqlDbType.VarChar,
ParameterDirection.Input)
------------------------------------------------------------------------------------
I am getting an error on the "ContractNo" field in the above line that says
"option strict on
disallows narrowing from type ''object'' to type ''string'' in copying the
value of ByRef parameter "sValue" back to the matching argument"

Here is the function...
-----------------------------------------------------------------------------------------
Function CreateSQLParam(ByVal sName As String, ByRef sValue As Object, ByVal
varType As System.Data.SqlDbType, ByVal varDir As ParameterDirection) As
SqlClient.SqlParameter

Dim objParam As SqlClient.SqlParameter
objParam = New SqlClient.SqlParameter()
objParam.ParameterName = sName

If IsNothing(sValue) Then sValue = System.DBNull.Value

objParam.Value = sValue
objParam.SqlDbType = varType
objParam.Direction = varDir
CreateSQLParam = objParam

End Function
-------------------------------------------------------------------------------------------

So in looking at the function, I am passing a varchar value (ContractNo) to
sValue which has been defined as an object and hence the error message.
Short of turning "option strict OFF", what is the best way to keep my
generic function so that I can pass whatever data type is required to the
functions sValue parameter? It must somehow mean I need to explicitely
define the type of variable coming in isntead of using object but how and
where would I do this?

Help!!

Thanks, Brad

As you are passing the reference by reference, the data type of the
reference has to match exactly. However, if you don''t plan to change the
reference from within the method, there is no reason to pass the
reference by reference. Just remove the ByRef keyword.

You can remove the ByVal keywords also, that is the default way that
parameters are passed. Stick with the default, unless you actually need
to pass a parameter by reference.

--
G?ran Andersson
_____
http://www.guffa.com


这篇关于数据类型转换的问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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