我遇到我想要的大量数据的传递到一个存储过程产生一些动态SQL的情况。
我想通过这些数据被存储在我在ASP.NET MVC的Web项目中使用此JSON / C#类对象。
[
{
"code":"ABC123",
"Count": "12998",
"Params":
[
{"name": "Recent", "value": "0-12m"},
{"name": "Orders", "value": "1"}
]
},
{
"code":"ABC124",
"Count": "13998",
"Params":
[
{"name": "Recent", "value": "0-12m"},
{"name": "Orders", "value": "2"}
]
},
{
"code":"ABC125",
"Count": "7998",
"Params":
[
{"name": "Recent", "value": "0-12m"},
{"name": "Orders", "value": "3"}
]
}
]
.....
然后使用该文本参数转换回JSON对象,我用这一个动作过滤器将其转换为一个对象。
public class ObjectFilter : ActionFilterAttribute
{
public string Param { get; set; }
public Type RootType { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if ((filterContext.HttpContext.Request.ContentType ?? string.Empty).Contains("application/json"))
{
object o =
new DataContractJsonSerializer(RootType).ReadObject(filterContext.HttpContext.Request.InputStream);
filterContext.HttpContext.Request.InputStream.Seek(0, SeekOrigin.Begin); // Rewind InputStream for other filters
filterContext.ActionParameters[Param] = o;
}
else
{
var xmlRoot = XElement.Load(new StreamReader(filterContext.HttpContext.Request.InputStream,
filterContext.HttpContext.Request.ContentEncoding));
object o = new XmlSerializer(RootType).Deserialize(xmlRoot.CreateReader());
filterContext.ActionParameters[Param] = o;
}
}
}
然后用C#等我在CLR存储过程创建一个SQL语句,如:
UPDATE [Sample]
SET [Field] =
CASE
WHEN [Recent] = "0-12m" AND [Orders] = "1" THEN "ABC123"
WHEN [Recent] = "0-12m" AND [Orders] = "2" THEN "ABC124"
WHEN [Recent] = "0-12m" AND [Orders] = "3" THEN "ABC125"
...
这是可能的,有没有人做过这样的事。 我已经看到了使用XML参数,但使用没有使用反序列化(?)JSON一个varchar参数几个帖子。