Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL选择具有多个整数值的行_C#_Sql Server - Fatal编程技术网

C# SQL选择具有多个整数值的行

C# SQL选择具有多个整数值的行,c#,sql-server,C#,Sql Server,我的表包含一个整数值。我想呈现多个值,类似于: select MeetingId, StartDate, EndDate, RoomId, MeetingStatusId, Subject from Meeting where RoomId in (@roomids ) and StartDate >= @start and EndDate <= @end and CreatedById = @user 但是如何将C中的@roomids

我的表包含一个整数值。我想呈现多个值,类似于:

select 
    MeetingId, StartDate, EndDate, RoomId, MeetingStatusId, Subject
from 
    Meeting
where 
    RoomId in (@roomids )
    and StartDate >= @start and EndDate <= @end
    and CreatedById = @user

但是如何将C中的@roomids参数构造成整数呢?我尝试将RoomId强制转换为varchar,但没有成功。

您可以使用SQL Server字符串分割函数并将参数用作varchar:

select 
    MeetingId, StartDate, EndDate, RoomId, MeetingStatusId, Subject
from 
    Meeting
where 
    RoomId in (SELECT cast(VALUE as int) FROM dbo.string_split(@roomids) )
    and StartDate >= @start and EndDate <= @end
    and CreatedById = @user
参考:

如果由于SQL Server版本的原因,您没有内置的拆分字符串功能,下面是一篇关于如何创建拆分字符串的文章:

在这里,表值参数是一个不错的选择。对于SQL Server中的初学者,如下所示:

create type dbo.IdentifierList as table (Identifier int not null);
下面是一个简单的C函数,用于创建具有以下类型的查询参数的实例:

SqlParameter CreateIdentifierTableParameter(string name, IEnumerable<int> identifiers)
{
    // Build a DataTable whose schema matches that of our custom table type.
    var identifierTable = new DataTable(name);
    identifierTable.Columns.Add("Identifier", typeof(long));
    foreach (var identifier in identifiers)
        identifierTable.Rows.Add(identifier);

    return new SqlParameter
    {
        ParameterName = name,              // The name of the parameter in the query to be run.
        TypeName = "dbo.IdentifierList",   // The name of our table type.
        SqlDbType = SqlDbType.Structured,  // Indicates a table-valued parameter.
        Value = identifierTable,           // The table created above.
    };
}
一开始这似乎需要做很多工作,但是一旦定义了SQL类型并编写了一些代码来创建它的实例,就可以在需要的地方重用它

试试这个

        string RooomList = "5,3,7";
        string DateS = "01-01-2017";
        string DateE = "12-31-2017";
        string userT = "Rami";
        string sqlText = string.Format(@"
        select  MeetingId, StartDate, EndDate, RoomId, MeetingStatusId, Subject
        from Meeting 
        where 
        RoomId in ({0} )
        and StartDate >= {1}  and EndDate <= {2} 
         and CreatedById = {3} ", RooomList, DateS , DateE , userT);

不必在WHERE子句中提供房间ID列表以过滤结果,只需使用表或公共表表达式的内部联接来限制结果。因此,与其在查询中内联指定房间ID,不如创建一个临时表或一个公共表表达式,其中包含要筛选的房间ID,然后将其连接到该临时表或公共表表达式。您可以创建一个用户定义的表类型,然后在C中将其用作数据表或某些其他机制来传递多个值。在SQL中,您将连接到参数,就像它是一个表一样。您可以使用dapper传递参数-下面介绍如何使用ToString而不是强制转换为varchar。正确,但这是假设SQL Server 2016,他们询问如何在cearlier版本中构造参数,至少在2014年支持它…这需要在SQL Server中进行更改…他们将其构造为varchar字符串…我提到过,您能否详细说明早期版本支持它,以及需要在SQL Server中进行哪些更改?我不知道这件事。谢谢这是一个手写拆分字符串,不是SQL Server更高版本中内置的字符串。。。但这仍然不是一个很好的解决方案。@scsimon我认为版本并不重要,因为如果它不像SQL 2008中那样存在,这就是我所拥有的。你可以自己创建它,我在过去已经做过了
        string RooomList = "5,3,7";
        string DateS = "01-01-2017";
        string DateE = "12-31-2017";
        string userT = "Rami";
        string sqlText = string.Format(@"
        select  MeetingId, StartDate, EndDate, RoomId, MeetingStatusId, Subject
        from Meeting 
        where 
        RoomId in ({0} )
        and StartDate >= {1}  and EndDate <= {2} 
         and CreatedById = {3} ", RooomList, DateS , DateE , userT);