我正在尝试使用 mongodb 作为数据库来构建 vb.net Web 应用程序。应用程序需要使用 Web 服务并返回 JSON 字符串。用作 Kendo UI 图表的数据源。
我的网络服务:-
Imports MongoDB.Bson
Imports MongoDB.Driver
Imports MongoDB.Driver.Builders
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
'Imports System.Web.Script.Serialization
Imports System.ComponentModel
Imports System.IO
Imports AjaxControlToolkit
' To allow this Web Service to be called from script, using ASP.NET AJAX,'uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService_BufferFile
Inherits System.Web.Services.WebService
Dim connectionString As String = "mongodb://localhost"
Dim client = New MongoClient(connectionString)
<WebMethod()> _
Public Function BufferFile_Power(ByVal UserId As Integer, ByVal DataBaseName As String, ByVal Collection As String, ByVal FileDate As DateTime, ByVal FileName As String) As String
'<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)> _
'runner = Mongo2Go.MongoDbRunner.Start()
'runner.Import(strDatabase, strCollection, ImportFilePath, False)
'MongoImportExport.Import("C:\MongoDB\bin\", 27017, strDatabase, strCollection, ImportFilePath, False)
Dim ImportFilePath As String = "E:\SCADA\RAW\" & DataBaseName & "\" & Collection & "\" & Format(FileDate, "yyyy") & "\Buffer\" & FileName & ""
If File.Exists(ImportFilePath) Then
Dim ImportProcess As New Process()
ImportProcess.StartInfo.UseShellExecute = True
ImportProcess.StartInfo.CreateNoWindow = True
ImportProcess.StartInfo.WorkingDirectory = "C:\MongoDB\bin"
ImportProcess.StartInfo.FileName = "mongoimport.exe"
ImportProcess.StartInfo.Arguments = " --db " & DataBaseName & " --collection " & Collection & "_" & Format(FileDate, "yyMMdd") & "_" & UserId & " --type csv --file " & ImportFilePath & " --headerline"
ImportProcess.Start()
ImportProcess.WaitForExit(1500)
ImportProcess.Dispose()
Else
Return 0
Exit Function
End If
Dim server = client.GetServer()
Dim database = server.getDatabase(DataBaseName)
Dim UserCollection = Collection & "_" & Format(FileDate, "yyMMdd") & "_" & UserId
Dim Docs As MongoCursor = database.GetCollection(UserCollection).FindAll()
Docs.SetSortOrder(SortBy.Ascending("timestamp"))
Docs.SetFields(Fields.Include("timestamp", "converter_UL1", "converter_UL2", "converter_UL3", "converter_I1", "converter_I2", "converter_I3").Exclude("_id"))
Docs.SetLimit(3000)
Dim JsonString = Docs.ToJson
If Docs.Count > 0 Then
Docs.Database.DropCollection(UserCollection)
End If
'Dim jsonSearializer As New JavaScriptSerializer()
'Return jsonSearializer.Serialize(JsonString)
'Return 234
Return JsonString
End Function
End Class
HTML 页面:-
<link href="CSS/kendo.common.min.css" rel="stylesheet" />
<link href="CSS/kendo.dataviz.min.css" rel="stylesheet" />
<link href="CSS/kendo.default.min.css" rel="stylesheet" />
<script type="text/javascript"> </script>
<script src="Scripts/jquery-1.7.1.min.js"></script>
<script src="Scripts/kendo.all.min.js"></script>
<table border="0" align="center">
<tr>
<td class="auto-style1" >
<div id="BufferPowerChart" class="k-content">
<div class="PowerChart-wrapper" style="margin: auto;">
<div id="PowerChart"></div>
</div>
<script>
function createPowerChart() {
$.ajax(
{
type: 'POST',
url: '/WebService_BufferFile.asmx/BufferFile_Power',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: '{"UserId":174,"DataBaseName":"ChitraDurga","Collection":"NSLHK-06","FileDate":"05/05/2013","FileName":"NSLHk-06_b130505_1819.txt"}',
async: false,
success: OnSuccess,
error: OnError
});
function OnSuccess(PowerChartData) {
alert(PowerChartData.d);
var PowerChartDataSource = JSON.parse(PowerChartData.d);
$("#PowerChart").kendoChart({
title: {
text: "Power"
},
legend: {
visible: true,
position: "bottom"
},
dataSource: PowerChartDataSource,
series: [{
type: "line",
field: "converter_UL1",
name: "converter_UL1"
}, {
type: "line",
field: "converter_UL2",
name: "converter_UL2"
}, {
type: "line",
field: "converter_UL3",
name: "converter_UL3"
}, {
type: "line",
field: "converter_I1",
name: "converter_I1"
}, {
type: "line",
field: "converter_I2",
name: "converter_I2"
}, {
type: "line",
field: "converter_I3",
name: "converter_I3"
}],
categoryAxis: {
field: "timestamp",
labels: {
rotation: -5
},
majorGridLines: {
visible: true
}
},
valueAxis: {
max: 1500,
line: {
visible: true
},
majorUnit:100,
minorGridLines: {
visible: true
}
},
tooltip: {
visible: true,
template: "#= series.name #: #= value #"
}
});
}
function OnError(msg) {
alert('Error = ' + msg.d);
}
}
$(document).ready(function () {
//debugger;
setTimeout(function () {
createPowerChart();
}, 500);
});
</script>
<style scoped>
.PowerChart-wrapper, .PowerChart-wrapper .k-chart {
height: 520px; width:850px;
}
</style>
</div>
</td>
</tr>
</table>
任何帮助,将不胜感激。