在vb.net中触发SQL查询时显示错误 [英] Display error while fire sql query in vb.net

查看:69
本文介绍了在vb.net中触发SQL查询时显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图明智地显示记录词.但是在vb.net中的火灾查询中出现问题
这是我的SQL查询

i am trying to display record word wise.but problem in fire query in vb.net
This is my sql query

SELECT * FROM emp.dbo.di1
        'WHERE city LIKE 's%'




我进行以下查询




and i making the following query

cmd = New SqlCommand("select * from Di1 Where Word LIKE" & "'" & TextBox7.Text & "'" & "%" & "", cn)
        adp = New SqlDataAdapter(cmd)
        adp.Fill(ds, "Di1")
        DataGridView1.DataSource = ds.Tables("Di1")



上面的代码在%符号附近显示语法错误

并且网格视图控件不会使用上述代码进行更新.

请更正我的查询和网格视图



the above code display syntax error near % sign

and also grid view control does not update using above code.

pls correct my query and grid view

推荐答案

首先,使用参数化查询来执行您的SQL语句,而不是您正在使用的此字符串连接.为什么?这样做可以轻松避免出现问题.

这是您当前查询的样子:
First, use parameterized queries to execute your SQL statement, not this string concatenation you''re using. Why? You have a problem that is EASILY avoided by doing do.

This is what you''re query currently looks like:
select * from Di1 Where Word LIKE''somevalue''%


您在LIKE后缺少空格,并且'%'字符必须在单引号内,而不是在单引号内.


You''re missing a space after LIKE and the ''%'' character needs to be INSIDE the single quote marks, not outside of it.


您的代码应像这样:


Your code should be like this:


cmd = New SqlCommand("select * from Di1 Where Word LIKE" & "'" & TextBox7.Text & "%'",cn)
        adp = New SqlDataAdapter(cmd)
        adp.Fill(ds, "Di1")
        DataGridView1.DataSource = ds.Tables("Di1")



您的上层查询是正确的,但是在第二段代码中,您使用的是"s"%,而不是"s%".

%"包括在单个qoute中.



your upper query is correct but in the 2nd piece of code you have ''s''% instead of ''s%''.

''%'' includes within single qoute.


这篇关于在vb.net中触发SQL查询时显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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