SQL查询C#的In子句中的多个ID [英] Multiple Id's in In clause of SQL Query C#

查看:290
本文介绍了SQL查询C#的In子句中的多个ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在sql查询的In子句中基本上使用多个iD. 现在我有两个选择,一个是从文本框中获取逗号分隔的ID,或者我可以在列表视图或网格视图中插入ID,然后获取要在sql语句中使用的ID.能否请您帮我提供代码,该怎么做?

I want to basically use multiple iD's in In clause of my sql query. Now i have two options one is to get the comma separated ID's from a textbox or i can put a list view or grid view to insert id's there and then get the id's to be used in sql statement. Can you please help me with the code, how to do this thing?

推荐答案

要获取文本框值,您必须像这样进行编码:-

In order to get textbox value you have to code like this:-

"select * from table where id in ( "+textbox1.text+")";

但是,这将导致您遇到Sql Injection问题.因此,更好的方法是:-

But this will lead you to Sql Injection problem. So a better approach will be:-

var command = new SqlCommand("SELECT * FROM table WHERE id = @value")
{
  Connection = connection();
};

command.Parameters.AddWithValue("value", textbox1.text);
var dataReader = command.ExecuteReader();

这篇关于SQL查询C#的In子句中的多个ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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