如何使用LIKE运算符在C#命令? [英] How to use the LIKE operator in a C# Command?

查看:165
本文介绍了如何使用LIKE运算符在C#命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要插入一个字符串转换为SQL命令

I need to insert a string into an Sql Command

search.CommandText = "SELECT * FROM Contacts WHERE Name like ' + @person + % + '";



它所使用的正确方法 LIKE 在命令

推荐答案

应该是:

SELECT * FROM Contacts WHERE Name like @person + '%'

@person 是一个参数 - 你不需要它周围的单引号。你只需要使用来连接它,这应该有报价

@person is a parameter - you don't need single quotes around it. You only need to concatenate it with %, which should have quotes.

记住:


  • 您也许会把它SELECT * FROM WHERE联系人名称一样@person,并有参数值包含(串连在C#是容易理解)。

  • 您可能还希望已在逃避其他通配符该字符串: _ []

  • You could have kept it "SELECT * FROM Contacts WHERE Name like @person", and have the parameter value contain % (concatenate in C# is simpler to understand).
  • You may also want to escape other wildcard characters already in the string: %, _, [ and ].

这篇关于如何使用LIKE运算符在C#命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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