【问题标题】:Passin Bit field parameters to stored procedure using Entity Framework 4.1使用 Entity Framework 4.1 将位字段参数传递给存储过程
【发布时间】:2011-09-15 00:43:59
【问题描述】:

全部,

我整天都被以下问题困扰。我正在尝试进行包含三位字段参数的参数化存储过程调用。我一直遇到的异常是“将数据类型 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();

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

谢谢, 德里克

【问题讨论】:

  • 为什么将布尔参数声明为 vars?尝试将它们声明为 bool,然后删除您的 convert.toboolean 语句。
  • 习惯声明隐式类型的力量。我会试试你的建议。谢谢。

标签: c# entity-framework frameworks parameterized-query


【解决方案1】:

也许尝试做类似的事情

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

【讨论】:

  • 我发现了我的问题。这是两部分。一个是将 SqlParameter 声明为 SqlDbType.Bit。第二部分是存储过程中声明了一个游标,这也导致了问题。感谢您的帮助。
猜你喜欢
  • 2020-03-05
  • 2016-04-05
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多