Vb.net 如何将Handsontable保存到数据库
是否有一种普遍接受的模式将数据保存在手持设备中并返回数据库 我使用ajax和WCF服务从数据库中提取数据以填充表。服务将返回一个对象列表,这些对象表示数据库表中的一行数据 周转基金: 这很好地填补了表格。我正在努力解决的是如何将更改保存回数据库。要求在所有更改完成并点击更新按钮之前,不要保存任何更改Vb.net 如何将Handsontable保存到数据库,vb.net,json,wcf,jquery,handsontable,Vb.net,Json,Wcf,Jquery,Handsontable,是否有一种普遍接受的模式将数据保存在手持设备中并返回数据库 我使用ajax和WCF服务从数据库中提取数据以填充表。服务将返回一个对象列表,这些对象表示数据库表中的一行数据 周转基金: 这很好地填补了表格。我正在努力解决的是如何将更改保存回数据库。要求在所有更改完成并点击更新按钮之前,不要保存任何更改 我知道我可以通过调用handsontable.getData()来获取包含表中所有单元格的对象。我的想法是,我需要将对象序列化为Json,将其发送回我的服务,将其反序列化回对象列表,然后为列表中的
我知道我可以通过调用
handsontable.getData()
来获取包含表中所有单元格的对象。我的想法是,我需要将对象序列化为Json,将其发送回我的服务,将其反序列化回对象列表,然后为列表中的每个对象更新数据库。我走对了吗?如果是这样的话,我实际上如何实现它?有两种方法可以实现这一点:
# coffeescript
onChangeHandler = ->
rowChange = rowID: theDataArray[arguments[0]], rowCol: arguments[1], newVal: arguments[3], dateTime: new Date()
window.MyNamespace.edits.push(rowChange)
tableDiv.handsontable
...
onChange: onChangeHandler
$('#save').click( ->
# save data back to the database
)
有两种方法可以实现这一点:
# coffeescript
onChangeHandler = ->
rowChange = rowID: theDataArray[arguments[0]], rowCol: arguments[1], newVal: arguments[3], dateTime: new Date()
window.MyNamespace.edits.push(rowChange)
tableDiv.handsontable
...
onChange: onChangeHandler
$('#save').click( ->
# save data back to the database
)
因此,我最终拼凑出一个解决方案来满足我的特定需求 我首先需要获取一个JSON格式的字符串,该字符串表示Handsontable的所有单元格,以传递回我的WCF服务。方法
handsontable.getData()
返回一个表示表中所有数据的对象。然后我使用JSON.stringify()
将该对象转换为字符串。从那以后,我很难将字符串传递给我的服务。我最终发现我必须对已经字符串化的对象进行字符串化,以便为我的服务创建适当的字符串参数,同时正确地转义对象中的引号
$("#btnUpdate").click(function () {
var tableData = JSON.stringify(handsontable.getData());
var input = JSON.stringify({ "input": tableData });
$.ajax({
type: 'POST',
url: "TableService.svc/SaveResource",
data: input,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (res) {
if (res.result === 'ok') {
console.text('Data saved');
}
},
error: function (xhr) {
alert(xhr.responseText);
}
});
$("btnUpdate").blur();
});
现在我的表数据回到了服务器端,我需要将JSON解析回一个对象列表。我最终用它来完成这项任务。有了对象列表后,我将每个列表项作为一行添加到一个新的DataTable
,在该表上运行更新数据库所需的SQL
<OperationContract()>
<WebInvoke(Method:="POST", BodyStyle:=WebMessageBodyStyle.WrappedRequest, ResponseFormat:=WebMessageFormat.Json)>
Public Function SaveResource(ByVal input As String) As String
Dim resources As List(Of Resource) = Json.JsonConvert.DeserializeObject(Of List(Of Resource))(input)
UpdateDB(resources)
Return "ok"
End Function
Private Sub UpdateDB(resources As List(Of Resource))
Dim dt As New DataTable
Dim conn = <some connection string>
Dim sql = <some SQL>
Using da As New SqlDataAdapter(sql, conn)
da.FillSchema(dt, SchemaType.Source)
For Each resourceItem In resources
Dim row As DataRow = dt.NewRow()
Dim month As Resource.MonthName
row("ResourceAllocationID") = resourceItem.ResourceAllocationID
row("ResourceName") = resourceItem.ResourceName
row("AllocationPercent") = resourceItem.AllocationPercent
row("Month") = [Enum].TryParse(resourceItem.Month, month)
row("Year") = resourceItem.Year
row("Comments") = resourceItem.Comments
row("ProjectID") = resourceItem.ProjectID
row("ResourceUId") = resourceItem.ResourceUId
dt.Rows.Add(row)
Next
End Using
*<run the appropriate SQL on each row of dt to update the database>*
End Sub
公共函数SaveResource(ByVal输入为字符串)为字符串
Dim resources As List(Of Resource)=Json.JsonConvert.DeserializeObject(Of List(Of Resource))(输入)
更新数据库(资源)
返回“ok”
端函数
私有子更新数据库(资源作为(资源的)列表)
Dim dt作为新数据表
Dim conn=
Dim sql=
将da用作新的SqlDataAdapter(sql,conn)
da.FillSchema(dt,SchemaType.Source)
对于资源中的每个resourceItem
作为DataRow=dt.NewRow()的Dim行
将月份设置为Resource.MonthName
行(“ResourceAllocationID”)=resourceItem.ResourceAllocationID
行(“ResourceName”)=resourceItem.ResourceName
行(“AllocationPercent”)=resourceItem.AllocationPercent
行(“月”)=[Enum].TryParse(resourceItem.Month,Month)
行(“年”)=资源项。年
行(“注释”)=resourceItem.Comments
行(“ProjectID”)=resourceItem.ProjectID
行(“ResourceUId”)=resourceItem.ResourceUId
dt.行.添加(行)
下一个
终端使用
**
端接头
因此,我最终拼凑了一个解决方案来满足我的特定需求
我首先需要获取一个JSON格式的字符串,该字符串表示Handsontable的所有单元格,以传递回我的WCF服务。方法handsontable.getData()
返回一个表示表中所有数据的对象。然后我使用JSON.stringify()
将该对象转换为字符串。从那以后,我很难将字符串传递给我的服务。我最终发现我必须对已经字符串化的对象进行字符串化,以便为我的服务创建适当的字符串参数,同时正确地转义对象中的引号
$("#btnUpdate").click(function () {
var tableData = JSON.stringify(handsontable.getData());
var input = JSON.stringify({ "input": tableData });
$.ajax({
type: 'POST',
url: "TableService.svc/SaveResource",
data: input,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (res) {
if (res.result === 'ok') {
console.text('Data saved');
}
},
error: function (xhr) {
alert(xhr.responseText);
}
});
$("btnUpdate").blur();
});
现在我的表数据回到了服务器端,我需要将JSON解析回一个对象列表。我最终用它来完成这项任务。有了对象列表后,我将每个列表项作为一行添加到一个新的DataTable
,在该表上运行更新数据库所需的SQL
<OperationContract()>
<WebInvoke(Method:="POST", BodyStyle:=WebMessageBodyStyle.WrappedRequest, ResponseFormat:=WebMessageFormat.Json)>
Public Function SaveResource(ByVal input As String) As String
Dim resources As List(Of Resource) = Json.JsonConvert.DeserializeObject(Of List(Of Resource))(input)
UpdateDB(resources)
Return "ok"
End Function
Private Sub UpdateDB(resources As List(Of Resource))
Dim dt As New DataTable
Dim conn = <some connection string>
Dim sql = <some SQL>
Using da As New SqlDataAdapter(sql, conn)
da.FillSchema(dt, SchemaType.Source)
For Each resourceItem In resources
Dim row As DataRow = dt.NewRow()
Dim month As Resource.MonthName
row("ResourceAllocationID") = resourceItem.ResourceAllocationID
row("ResourceName") = resourceItem.ResourceName
row("AllocationPercent") = resourceItem.AllocationPercent
row("Month") = [Enum].TryParse(resourceItem.Month, month)
row("Year") = resourceItem.Year
row("Comments") = resourceItem.Comments
row("ProjectID") = resourceItem.ProjectID
row("ResourceUId") = resourceItem.ResourceUId
dt.Rows.Add(row)
Next
End Using
*<run the appropriate SQL on each row of dt to update the database>*
End Sub
公共函数SaveResource(ByVal输入为字符串)为字符串
Dim resources As List(Of Resource)=Json.JsonConvert.DeserializeObject(Of List(Of Resource))(输入)
更新数据库(资源)
返回“ok”
端函数
私有子更新数据库(资源作为(资源的)列表)
Dim dt作为新数据表
Dim conn=
Dim sql=
将da用作新的SqlDataAdapter(sql,conn)
da.FillSchema(dt,SchemaType.Source)
对于资源中的每个resourceItem
作为DataRow=dt.NewRow()的Dim行
将月份设置为Resource.MonthName
行(“ResourceAllocationID”)=resourceItem.ResourceAllocationID
行(“ResourceName”)=resourceItem.ResourceName
行(“AllocationPercent”)=resourceItem.AllocationPercent
行(“月”)=[Enum].TryParse(resourceItem.Month,Month)
行(“年”)=资源项。年
第(C)行