VB.NET错误:操作数类型冲突:日期与int不兼容 [英] VB.NET Error: Operand type clash: date is incompatible with int

查看:72
本文介绍了VB.NET错误:操作数类型冲突:日期与int不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用where子句从数据库中显示名为staffpresentee的表中的记录...



i am using visual studio 2010 ...



表设计如下 -

      fieldname       ;            数据类型          允许为空

1     staff_id(FK)              数字(18,0)        是

2      日期       &NBSP ;                   日期     &NBSP ;           no

3   &nbs磷;本                       焦炭( 1)              yes



注意类型第二个字段是日期而不是日期时间......



我想显示表中所有日期都在''日期'的记录字段...

我当前的查询是 -



cmd =新的SqlClient.SqlCommand(select * from staffpresentee where date =& Date.Now.Date,cn)

dr = cmd.ExecuteReader



for dr = cmd.ExecuteReader我收到错误操作数类型冲突:日期与int不兼容



i尝试select * from staffpresentee查询而没有正确运行的where子句...

解决方案

两个问题:

1)date是一个SQL保留字,所以叫你的co lumn总会给出问题。

2)将日期作为参数发送而不是连接字符串 - 这总是一个更好的主意,因为它也避免了SQL注入攻击。

 cmd =  SqlCommand(  select * from staffpresentee,其中[date] = @ DT,cn)
cmd.Parameters.AddWithValue( < span class =code-string> @ DT,DateTime.Now。 Date
dr = cmd.ExecuteReader()


I am trying to display records from a table named staffpresentee from a database using the where clause...

i am using visual studio 2010...

the table design is as follows-
     fieldname                  datatype          allownull
1    staff_id (fk)              numeric(18,0)       yes
2      date                           date                no
3    present                       char(1)             yes

Note that the type of the 2nd field is date and not datetime...

I want to display all records from the table that have today''s date int the ''date'' field...
My current query is-

cmd = New SqlClient.SqlCommand("select * from staffpresentee where date=" & Date.Now.Date, cn)
dr = cmd.ExecuteReader

for dr = cmd.ExecuteReader i get the error "Operand type clash: date is incompatible with int"

i tried the select * from staffpresentee query without the where clause it runs correctly...

解决方案

Two problems:
1) "date" is an SQL reserved word, so calling your column that is always going to give problems.
2) Send the date as a parameter rather that concatenating strings - that is always a better idea as it avoids SQL injection attacks as well.

cmd = New SqlCommand("select * from staffpresentee where [date]=@DT", cn)
cmd.Parameters.AddWithValue("@DT", DateTime.Now.Date)
dr = cmd.ExecuteReader()


这篇关于VB.NET错误:操作数类型冲突:日期与int不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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