SQL参数:这里有什么问题? [英] SQL parameter: what is wrong here?

查看:76
本文介绍了SQL参数:这里有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我执行一些简单的示例代码:


使用的OleDbCommand作为新的OleDbCommand(Me.DBQuery.Text,

Me.OleDbConnection)<无线电通信/>

Dim OleDbParameter作为OleDbParameter =

OleDbCommand.Parameters.Add(" @Pinco",OleDbType.VarWChar)

OleDbParameter.Direction = ParameterDirection.Input

OleDbParameter.Value =" Berlin"

使用OleDbDataReader作为OleDbDataReader =

OleDbCommand.ExecuteReader()

''...


我使用此查询:


SELECT

C.Country ASCountry,

C.City ASCity

FROM

客户C

其中


C.City = @Pinco


它适用于ACCESS,但是给出了SQL Server错误(表示变量

尚未定义。)


这里有什么问题?如何修复它以使用一般的OleDB

连接?


-P

解决方案

Pamela,


当使用带有SQL Server的OleDb命名空间时,我认为你不能使用

命名参数。要使用命名参数,您需要使用SQLClient

命名空间。


如果您通过将@Pinco替换为
$来更改Select语句b $ b问号占位符,?,我认为你的代码应该有效。


Kerry Moorman

" pa ******** ***@libero.it"写道:

我执行一些简单的示例代码:


使用的OleDbCommand作为新的OleDbCommand(Me.DBQuery.Text ,

Me.OleDbConnection)


Dim OleDbParameter作为OleDbParameter =

OleDbCommand.Parameters.Add(" @ Pinco", OleDbType.VarWChar)

OleDbParameter.Direction = ParameterDirection.Input

OleDbParameter.Value =" Berlin"


使用OleDbDataReader As OleDbDataReader =

OleDbCommand.ExecuteReader()

''...


我使用此查询:


SELECT

C.Country ASCountry,

C.City ASCity

FROM

客户C

其中


C.City = @Pinco


It与ACCESS一起使用,但是给出了SQL Server的错误(表示变量

尚未定义) />

这里有什么问题?如何修复它以使用一般的OleDB

连接?


-P




Kerry Moorman写道:


Pamela,

将OleDb命名空间与SQL Server一起使用时,我认为你不能使用

命名参数。要使用命名参数,您需要使用SQLClient

命名空间。


如果您通过将@Pinco替换为
$来更改Select语句b $ b问号占位符,?,我认为你的代码应该有效。



我真的不知道OleDb和ODBC或SQL之间的区别,但

我建议添加一个:


在选择之前声明@Pinco为varchar(< maxsize>)




>

" pa *********** @ libero.it"写道:

我执行一些简单的示例代码:


使用的OleDbCommand作为新的OleDbCommand(Me.DBQuery.Text ,

Me.OleDbConnection)


Dim OleDbParameter作为OleDbParameter =

OleDbCommand.Parameters.Add(" @ Pinco", OleDbType.VarWChar)

OleDbParameter.Direction = ParameterDirection.Input

OleDbParameter.Value =" Berlin"


使用OleDbDataReader As OleDbDataReader =

OleDbCommand.ExecuteReader()

''...


我使用此查询:


SELECT

C.Country ASCountry,

C.City ASCity

FROM

客户C

其中


C.City = @Pinco


It与ACCESS一起使用,但是给出了SQL Server的错误(表示变量

尚未定义。


这里的问题是什么?如何修复它以使用一般的OleDB

连接?


-P


pa *********** @ libero.it 写道:


您好我正在执行一些简单的示例代码:


使用OleDbCommand作为新的OleDbCommand(我。 DBQuery.Text,

Me.OleDbConnection)


Dim OleDbParameter作为OleDbParameter =

OleDbCommand.Parameters.Add(" @ Pinco",OleDbType.VarWChar)



它可能需要在您创建

表时定义的字符串的最大长度,例如:

Dim OleDbParameter As OleDbParameter OleDbCommand.Parameters.Add(" @ Pinco",

OleDbType.VarWChar,24)


安德鲁


Hi I am executing some simple sample code:

Using OleDbCommand As New OleDbCommand(Me.DBQuery.Text,
Me.OleDbConnection)

Dim OleDbParameter As OleDbParameter =
OleDbCommand.Parameters.Add("@Pinco", OleDbType.VarWChar)
OleDbParameter.Direction = ParameterDirection.Input
OleDbParameter.Value = "Berlin"

Using OleDbDataReader As OleDbDataReader =
OleDbCommand.ExecuteReader()
''...

I use this query:

SELECT
C.Country AS "Country",
C.City AS "City"
FROM
Customers C
where

C.City = @Pinco

It works with ACCESS, but gives error with SQL Server (says variable
has not been defined).

What''s the problem here? How do I fix it to work with a general OleDB
connection?

-P

解决方案

Pamela,

When using the OleDb namespace with SQL Server, I don''t think you can use
named parameters. To use named parameters you would need to use the SQLClient
namespace.

If you change your Select statement by replacing @Pinco with the
question-mark placeholder, ?, I think your code should work.

Kerry Moorman
"pa***********@libero.it" wrote:

Hi I am executing some simple sample code:

Using OleDbCommand As New OleDbCommand(Me.DBQuery.Text,
Me.OleDbConnection)

Dim OleDbParameter As OleDbParameter =
OleDbCommand.Parameters.Add("@Pinco", OleDbType.VarWChar)
OleDbParameter.Direction = ParameterDirection.Input
OleDbParameter.Value = "Berlin"

Using OleDbDataReader As OleDbDataReader =
OleDbCommand.ExecuteReader()
''...

I use this query:

SELECT
C.Country AS "Country",
C.City AS "City"
FROM
Customers C
where

C.City = @Pinco

It works with ACCESS, but gives error with SQL Server (says variable
has not been defined).

What''s the problem here? How do I fix it to work with a general OleDB
connection?

-P



Kerry Moorman wrote:

Pamela,

When using the OleDb namespace with SQL Server, I don''t think you can use
named parameters. To use named parameters you would need to use the SQLClient
namespace.

If you change your Select statement by replacing @Pinco with the
question-mark placeholder, ?, I think your code should work.

I really don''t know the differences between OleDb and ODBC or SQL, but
I would have suggested adding a:

declare @Pinco as varchar(<maxsize>)
before the Select.

>
"pa***********@libero.it" wrote:

Hi I am executing some simple sample code:

Using OleDbCommand As New OleDbCommand(Me.DBQuery.Text,
Me.OleDbConnection)

Dim OleDbParameter As OleDbParameter =
OleDbCommand.Parameters.Add("@Pinco", OleDbType.VarWChar)
OleDbParameter.Direction = ParameterDirection.Input
OleDbParameter.Value = "Berlin"

Using OleDbDataReader As OleDbDataReader =
OleDbCommand.ExecuteReader()
''...

I use this query:

SELECT
C.Country AS "Country",
C.City AS "City"
FROM
Customers C
where

C.City = @Pinco

It works with ACCESS, but gives error with SQL Server (says variable
has not been defined).

What''s the problem here? How do I fix it to work with a general OleDB
connection?

-P


pa***********@libero.it wrote:

Hi I am executing some simple sample code:

Using OleDbCommand As New OleDbCommand(Me.DBQuery.Text,
Me.OleDbConnection)

Dim OleDbParameter As OleDbParameter =
OleDbCommand.Parameters.Add("@Pinco", OleDbType.VarWChar)

It might want the maximum length of the string as defined when you created
the table, e.g.:
Dim OleDbParameter As OleDbParameter OleDbCommand.Parameters.Add("@Pinco",
OleDbType.VarWChar, 24)

Andrew


这篇关于SQL参数:这里有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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