使用 Entity Framework 4.1 将位字段参数传递到存储过程

发布于 2024-11-15 13:51:37 字数 2894 浏览 6 评论 0原文

所有,

我一整天都被以下问题困扰。我正在尝试进行包含三位字段参数的参数化存储过程调用。我不断遇到的异常是“将数据类型 nvarchar 转换为位时出错。”

这是我的源代码:

存储过程

ALTER PROCEDURE usp_TestParamProc
    @price_segment_set_id VARCHAR(8) = NULL,
    @segment_group_id VARCHAR(8) = NULL,
    @segment_price_band_id VARCHAR(8) = NULL,
    @segment_id VARCHAR(8) = NULL,
    @include_source_price_bands BIT = 0,
    @apply_segmentation BIT = 0,
    @print_sql BIT = 0
AS
BEGIN
    SET NOCOUNT ON;
    SELECT *
    FROM MarketingSegmentDetails
    WHERE SegmentID = @segment_id

END
GO

C# 代码:

        var segment_id = "1";
        string segment_group_id = "1";
        string segment_price_band_id = "1";
        string price_segment_set_id = "1";
        var include_source_price_bands = false;
        var apply_segmentation = false;
        var print_sql = false;

        using (var context = new PricingContext())
        {
            var list = new List<object>();
            list.Add(price_segment_set_id == null
                         ? new SqlParameter("price_segment_set_id", DBNull.Value)
                         : new SqlParameter("price_segment_set_id", price_segment_set_id));
            list.Add(segment_group_id == null
                        ? new SqlParameter("segment_group_id", DBNull.Value)
                        : new SqlParameter("segment_group_id", segment_group_id));
            list.Add(segment_price_band_id == null
                        ? new SqlParameter("segment_price_band_id", DBNull.Value)
                        : new SqlParameter("segment_price_band_id", segment_price_band_id));
            list.Add(segment_id == null
                        ? new SqlParameter("segment_id", DBNull.Value)
                        : new SqlParameter("segment_Id", segment_id));
            list.Add(include_source_price_bands == null 
                        ? new SqlParameter("include_source_price_bands", DBNull.Value)
                        : new SqlParameter("include_source_price_bands", Convert.ToBoolean(include_source_price_bands)));
            list.Add(apply_segmentation == null
                        ? new SqlParameter("apply_segmentation", DBNull.Value)
                        : new SqlParameter("apply_segmentation", Convert.ToBoolean(apply_segmentation)));
            list.Add(print_sql == null
                         ? new SqlParameter("print_sql", DBNull.Value)
                         : new SqlParameter("print_sql", Convert.ToBoolean(print_sql)));

            var segments = context.Database.SqlQuery<usp_SegmentProcessing>("usp_TestParamProc price_segment_set_id, segment_group_id, segment_price_band_id, segment_id, include_source_price_bands, apply_segmentation, print_sql", list.ToArray()).ToList();

任何帮助将不胜感激。我在项目工作中落后了。

谢谢, 德里克

All,

I've been stuck on the following issue all day. I'm trying to make a parametrized stored procedure call that includes three bit field parameters. The exception I keep encountering is "Error converting data type nvarchar to bit."

Here is my source code:

Stored procedure

ALTER PROCEDURE usp_TestParamProc
    @price_segment_set_id VARCHAR(8) = NULL,
    @segment_group_id VARCHAR(8) = NULL,
    @segment_price_band_id VARCHAR(8) = NULL,
    @segment_id VARCHAR(8) = NULL,
    @include_source_price_bands BIT = 0,
    @apply_segmentation BIT = 0,
    @print_sql BIT = 0
AS
BEGIN
    SET NOCOUNT ON;
    SELECT *
    FROM MarketingSegmentDetails
    WHERE SegmentID = @segment_id

END
GO

C# code:

        var segment_id = "1";
        string segment_group_id = "1";
        string segment_price_band_id = "1";
        string price_segment_set_id = "1";
        var include_source_price_bands = false;
        var apply_segmentation = false;
        var print_sql = false;

        using (var context = new PricingContext())
        {
            var list = new List<object>();
            list.Add(price_segment_set_id == null
                         ? new SqlParameter("price_segment_set_id", DBNull.Value)
                         : new SqlParameter("price_segment_set_id", price_segment_set_id));
            list.Add(segment_group_id == null
                        ? new SqlParameter("segment_group_id", DBNull.Value)
                        : new SqlParameter("segment_group_id", segment_group_id));
            list.Add(segment_price_band_id == null
                        ? new SqlParameter("segment_price_band_id", DBNull.Value)
                        : new SqlParameter("segment_price_band_id", segment_price_band_id));
            list.Add(segment_id == null
                        ? new SqlParameter("segment_id", DBNull.Value)
                        : new SqlParameter("segment_Id", segment_id));
            list.Add(include_source_price_bands == null 
                        ? new SqlParameter("include_source_price_bands", DBNull.Value)
                        : new SqlParameter("include_source_price_bands", Convert.ToBoolean(include_source_price_bands)));
            list.Add(apply_segmentation == null
                        ? new SqlParameter("apply_segmentation", DBNull.Value)
                        : new SqlParameter("apply_segmentation", Convert.ToBoolean(apply_segmentation)));
            list.Add(print_sql == null
                         ? new SqlParameter("print_sql", DBNull.Value)
                         : new SqlParameter("print_sql", Convert.ToBoolean(print_sql)));

            var segments = context.Database.SqlQuery<usp_SegmentProcessing>("usp_TestParamProc price_segment_set_id, segment_group_id, segment_price_band_id, segment_id, include_source_price_bands, apply_segmentation, print_sql", list.ToArray()).ToList();

Any help would be greatly appreciated. I'm falling behind in project work.

Thanks,
Derek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆 2024-11-22 13:51:37

也许尝试做类似的事情

list.Add(
   new SqlParameter("print_sql", System.Data.SqlDbType.Bit) { Value = (print_sql != null ? print_sql : DBNull.Value) }
);

http://msdn.microsoft.com/en-us/库/h8f14f0z.aspx

Maybe try doing something like

list.Add(
   new SqlParameter("print_sql", System.Data.SqlDbType.Bit) { Value = (print_sql != null ? print_sql : DBNull.Value) }
);

http://msdn.microsoft.com/en-us/library/h8f14f0z.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文