我可以在C#中从SQL查询中选择更多特定数字吗? [英] Can I more select specific number from SQL query in C#

查看:75
本文介绍了我可以在C#中从SQL查询中选择更多特定数字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体应用程序中工作c#vs2015集成到sql server 2012

我遇到问题

i需要在同一时间选择更具体的数字但我不知道

我写的查询或我在c中做什么#

当前我使用select * from table userID = @ UserID

这是仅选数字

但如果我需要选择更具体的数字

假设我的数字从1001到1020

i需要选择1005,1009,1012,1017

如何从SQL查询或c#中同时获得更多记录的结果

我可以接受任何方式



我尝试了什么:



我可以从c#查询sql查询中选择具体的数字吗? p $ p> 

解决方案

您是否尝试过INSQL运算符?



 选择 * 来自  
其中 userID = @ UserID
number in 1005 1009 1012 1017


除IN之外,如果是你要选择的范围然后



WHERE userID> = 1001 AND userID< = 1020


引用:

文本框的值为1002,1991,2007,3006

以便如何选择这些值

您需要拆分文本,将每个值转换为整数,并使用 IN 运算符构建参数化SQL命令:

  string  text = YourTextBox.Text; 
string [] parts = text.Split( new [] {' ,'},StringSplitOptions.RemoveEmptyEntries);

string [] parameterNames = new 字符串 [parts.Length];
SqlParameter [] parameters = new SqlParameter [parts.Length];

for int index = 0 ; index < parts.Length; index ++)
{
int value ;
if (!int.TryParse(parts [index], out value ))
{
// TODO:显示用户错误
return ;
}

string name = @ p + index;
parametersNames [index] = name;
parameters [index] = new SqlParameter(name,SqlDbType.Int){Value = value };
}

string sql = string .Format( SELECT * FROM table WHERE userID IN({0}) string .Join( ,parameterNames));

使用 var connection = new SqlConnection( ...))
使用 var command = new SqlCommand(sql,connection ))
{
command.Parameters.AddRange(参数);
...
}


I work in windows form application c# vs2015 integrated to sql server 2012
I face problem
i need to select more specific number in same time but i dont know
what query i write or what i do in c#
current i using select * from table where userID=@UserID
this is select only number
but if i need to select more specific number
suppose i have numbers from 1001 to 1020
i need to select 1005,1009,1012,1017
How to get result for more records from SQL query or c# in same time
any way i can accept

What I have tried:

can i more select specific number from sql query in c#

解决方案

Have you tried the 'IN' SQL operator ?

select * from table
where userID=@UserID
and number in (1005, 1009, 1012, 1017)


In addition to IN, if it is a range you want to select then

WHERE userID >= 1001 AND userID <=1020


Quote:

textbox have values 1002,1991,2007,3006
so that how to select these values


You'll need to split the text, convert each value to an integer, and build up a parameterized SQL command using the IN operator:

string text = YourTextBox.Text;
string[] parts = text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

string[] parameterNames = new string[parts.Length];
SqlParameter[] parameters = new SqlParameter[parts.Length];

for (int index = 0; index < parts.Length; index++)
{
    int value;
    if (!int.TryParse(parts[index], out value))
    {
        // TODO: Display an error to the user
        return;
    }
    
    string name = "@p" + index;
    parametersNames[index] = name;
    parameters[index] = new SqlParameter(name, SqlDbType.Int) { Value = value };
}

string sql = string.Format("SELECT * FROM table WHERE userID IN ({0})", string.Join(", ", parameterNames));

using (var connection = new SqlConnection("..."))
using (var command = new SqlCommand(sql, connection))
{
    command.Parameters.AddRange(parameters);
    ...
}


这篇关于我可以在C#中从SQL查询中选择更多特定数字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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