在此项目中,用户将有机会创建具有属性的对象数组,并且这些属性与数据库表匹配,对象的属性与数据库电缆中的列相同。 SQL看起来像:
create table ServiceData
(ServiceId int
,ServiceDescription varchar(50)
)
go
create type ServiceType as table
(ServiceId int
,ServiceDescription varchar(50)
)
go
create proc spInsertService
@service ServiceType readonly
as
begin
insert into ServiceData(ServiceId,ServiceDescription)
select * from @service
end
这里我创建了一个自定义类型,并以表值参数的形式将该自定义类型传递给存储过程。 SQL和以下C#代码执行并正常工作:
[WebMethod]
public void InsertServiceData()
{
List<ServiceData> sdList = new List<ServiceData>();
ServiceData sd1 = new ServiceData(1, "first");
ServiceData sd2 = new ServiceData(2, "second");
sdList.Add(sd1);
sdList.Add(sd2);
DataTable dt = new DataTable();
dt.Columns.Add("ServiceId");
dt.Columns.Add("ServiceDescription");
foreach (var data in sdList)
{
dt.Rows.Add(data.ServiceId, data.ServiceDescription);
}
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
using (var con = new SqlConnection(cs))
{
using (var cmd = new SqlCommand("spInsertService",con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@service", dt);
cmd.ExecuteNonQuery();
}
}
}
你可以看到,在这个工作示例中,我没有使用任何AJAX调用来向web方法发送数据。此代码当前有效,并从该硬编码列表中插入数据。所以当我改变代码实际上尝试采用像这样的JavaScript数组时:
$(document).ready(function ()
{
sd1 = {};
sd1.ServiceId = 1;
sd1.ServiceDescription = "test";
sd2 = {};
sd2.ServiceId = 2;
sd2.ServiceDescription = "other test";
//create array which is meant to mirror the List<ServiceData> in the
//earlier example
service = new Array();
service.push(sd1);
service.push(sd2);
//wrap the array in a data transfer object
var dto = {'sdList': service};
$('#btnSubmit').click(function ()
{
$.ajax(
{
type: "POST",
url: "WebService.asmx/InsertServiceData",
contentType: "application/json",
dataType: "json",
//stringify the dto
data: JSON.stringify(dto),
success: function(data)
{
console.log('success');
},
error: function(thrownError)
{
console.log(thrownError);
}
});
});
});
新C#
[WebMethod]
//this attempts to deserialize the DTO into a list of ServiceData objects
//which are then inserted into the TVP and then to the database
public void InsertServiceData(string sdList)
{
var jss = new JavaScriptSerializer();
List<ServiceData> list = jss.Deserialize<List<ServiceData>>(sdList);
DataTable dt = new DataTable();
dt.Columns.Add("ServiceId");
dt.Columns.Add("ServiceDescription");
foreach (var data in list)
{
dt.Rows.Add(data.ServiceId, data.ServiceDescription);
}
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
using (var con = new SqlConnection(cs))
{
using (var cmd = new SqlCommand("spInsertService",con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@service", dt);
cmd.ExecuteNonQuery();
}
}
}
目前该代码给出了错误:Type\u0027System.String\u0027isnotsupportedfordeserializationofanarray
如果我不将数组包装在DTO对象中,但仍然将其字符串化,我得到
`System.Collections.Generic.IDictionary`2[[System.String,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken`
我不想为此使用ViewState的SessionState。因为我知道如果我没有将JavaScript数组传递给WebMethod,代码工作正常,它必须在数组的序列化和反序列化中的某个地方打破它。我怎样才能解决这个问题?它已经让我疯了几天了
答案 0 :(得分:0)
请注意以下步骤,然后更改代码行。
sd1={}
和sd2{}
。var list = [];
,然后像list.push({"ServiceId":1,"ServiceDescription":"test"}, {"ServiceId":2,"ServiceDescription":"other test"})
var data = "{'sdLists':" +JSON.stringify(list)+"}";
这样的ajax参数,并将日期作为参数传递。创建一个带有变量的bean,以映射上面列表中添加的json对象。看到打击。
public class SdList { private int Serviceid; public int Serviceid { 得到{return Serviceid; } 设置{Serviceid = value; } } 私有字符串ServiceDescription; 公共字符串ServiceDescription { 获取{return ServiceDescription; } 设置{ServiceDescription = value; } } }
现在将List<SdList> sdLists
作为参数列表传递给
var data =“{'sdLists':”+ JSON.stringify(list)+“}”; public void InsertServiceData(List sdLists)
然后使用java脚本序列化将Json List转换为通用列表,如下所示:
JavaScriptSerializer jss= new JavaScriptSerializer();
List<SdList> list = jss.ConvertToType<List<SdList>>(sdLists);
我已按照上述步骤进行操作。