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

时间:2018-07-20 15:40:47

标签: 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参数构造为整数?我尝试将RoomId强制转换为varchar,但这没用。

3 个答案:

答案 0 :(得分:3)

您可以使用SQL Server STRING_SPLIT函数并将该参数用作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

参考:https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017

如果由于您的SQL Server版本而没有内置的拆分字符串功能,请参阅以下有关如何创建拆分字符串的文章: https://sqlperformance.com/2012/07/t-sql-queries/split-strings

答案 1 :(得分:0)

在这里,表值参数是一个不错的选择。对于初学者,您可以在SQL Server中create a custom table type,如下所示:

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.
    };
}

然后,将@RoomIds参数写为查询,就好像它是数据库中的其他任何表一样,调用上面创建的函数来构建表值参数,然后将其添加到SQL命令中就像其他SqlParameter一样。例如:

void GetMeetings(IEnumerable<int> roomIdentifiers)
{
    // A simplified version of your query to show just the relevant part:
    const string sqlText = @"
        select
            M.*
        from
            Meeting M
        where 
            exists (select 1 from @RoomIds R where M.RoomId = R.Identifier);";

    using (var sqlCon = new SqlConnection("<your connection string here>"))
    {
        sqlCon.Open();
        using (var sqlCmd = new SqlCommand(sqlText, sqlCon))
        {
            sqlCmd.Parameters.Add(CreateIdentifierTableParameter("RoomIds", roomIdentifiers));

            // Execute sqlCmd here in whatever way is appropriate.
        }
    }
}

开始时似乎需要做很多工作,但是一旦定义了SQL类型并编写了一些代码来创建它的实例,就很容易在任何需要的地方重复使用。

答案 2 :(得分:0)

尝试一下

        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);