1 javascript c# web-services asmx
我创建了一个Web服务,其中我有一个函数来计算我的SQL数据库中的一些数据.这是我的WebService.asmx的代码:
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public int SalesNumberMonth(int i)
{
int total = 0;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);
try
{
string request = "SELECT * FROM sales_Ventes V INNER JOIN sys_AnneesFiscales A ON V.AnneeFiscale = A.Code INNER JOIN sys_Mois M ON V.Mois = M.Code WHERE M.Code='" + i + "'" + " AND Active = 'true'";
connection.Open();
SqlCommand Req = new SqlCommand(request, connection);
SqlDataReader Reader = Req.ExecuteReader();
while (Reader.Read())
{
total++;
}
Reader.Close();
}
catch
{
}
connection.Close();
return total;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我的script.js:
var sin = [], cos = [];
for (var i = 1; i < 13; i += 1) {
GestionPro.WebService1.SalesNumberMonth(i, function (e) { sin.push([i, e]); } ,function (response) { alert(response); } );
cos.push([i, 2]);
}
var plot = $.plot($("#mws-test-chart"),
[{ data: sin, label: "Sin(x)²", color: "#eeeeee" }, { data: cos, label: "Cos(x)", color: "#c5d52b"}], {
series: {
lines: { show: true },
points: { show: true }
},
grid: { hoverable: true, clickable: true }
});
Run Code Online (Sandbox Code Playgroud)
我的问题就在这条线上:
GestionPro.WebService1.SalesNumberMonth(i, function (e) { sin.push([i, e]); } ,function (response) { alert(response); } );
Run Code Online (Sandbox Code Playgroud)
当我交换这两个函数时,警报显示良好,但按此顺序我不能在sin []中添加我的函数的值.我应该错过一些东西,但不知道是什么......
您的代码存在很多问题:
SELECT *然后依次计算循环中的客户端代码而不是使用COUNTSQL聚合函数提到的问题,让我们从修复它们开始.
我们先修复服务器端代码:
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public int[] SalesNumbersMonths(int[] months)
{
// Could use LINQ instead but since I don't know which version
// of the framework you are using I am providing the naive approach
// here. Also the fact that you are using ASMX web services which are
// a completely obsolete technology today makes me think that you probably
// are using something pre .NET 3.0
List<int> result = new List<int>();
foreach (var month in months)
{
result.Add(SalesNumberMonth(month));
}
return result.ToArray();
}
[WebMethod]
public int SalesNumberMonth(int i)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString))
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT COUNT(*) FROM sales_Ventes V INNER JOIN sys_AnneesFiscales A ON V.AnneeFiscale = A.Code INNER JOIN sys_Mois M ON V.Mois = M.Code WHERE M.Code=@Code AND Active = 'true'";
cmd.Parameters.AddWithValue("@Code", i);
return (int)cmd.ExecuteScalar();
}
}
}
Run Code Online (Sandbox Code Playgroud)
好的,你现在会注意到我添加的新方法,它允许计算几个月的总数并将它们作为整数数组返回,以避免在无意义的AJAX请求中浪费带宽.
现在让我们修复您的客户端代码:
var months = [];
for (var i = 1; i < 13; i += 1) {
months.push(i);
}
GestionPro.WebService1.SalesNumbersMonths(months, function (e) {
// and once the web service succeeds in the AJAX request we could build the chart:
var sin = [],
cos = [];
for (var i = 0; i < e.length; i++) {
cos.push([i, 2]);
sin.push([i, e[i]]);
}
var chart = $('#mws-test-chart'),
var data = [
{ data: sin, label: 'Sin(x)²', color: '#eeeeee' },
{ data: cos, label: 'Cos(x)', color: '#c5d52b' }
];
var series = {
series: {
lines: { show: true },
points: { show: true }
}
};
var plot = $.plot(
chart,
data,
series,
grid: { hoverable: true, clickable: true }
);
// TODO: do something with the plot
}, function (response) {
// that's the error handler
alert(response);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
711 次 |
| 最近记录: |