如何解决使用Entity Framework 4.1将位字段参数传递给存储过程
| 所有, 我整天忙于解决以下问题。我正在尝试进行包含三个位字段参数的参数化存储过程调用。我一直遇到的异常是“将数据类型nvarchar转换为bit时出错。” 这是我的源代码: 存储过程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();
任何帮助将不胜感激。我在项目工作上落后。
谢谢,
德里克
解决方法
也许尝试做类似的事情
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。