2012-07-04 165 views
1

我做了一个Web服务,其中我有一个函数来计算我的SQL数据库中的一些数据。我WebService.asmx的下面的代码:Javascript的ASMX Web服务

[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; 
    } 
} 

,在这里我的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 } 
     }); 

我probleme是在这条线:

GestionPro.WebService1.SalesNumberMonth(i, function (e) { sin.push([i, e]); } ,function (response) { alert(response); } ); 

当我换了两个函数,警报显示的很好,但是按照这个顺序,我不能在sin []中添加我的函数的值。我错过了什么,但不知道是什么...

+2

你确定你想在一个打电话给你的web服务13次循环?如果你有两个客户同时打开这个页面怎么办?这使得它可以同时呼叫26个。如果你有10个客户端 - 同时发生130个AJAX调用!!!你认为你的服务器在崩溃之前会走多远?此外,为什么你使用'SELECT *'然后在.NET代码中使用累加器变量来计数?你没有听说过COUNT SQL集合函数吗? –

回答

5

有极大的很多问题与您的代码:

  • 您正在引发Ajax请求的循环。触发一个将返回整个结果的单个AJAX请求将是最佳选择。它总是更好
  • 您正在使用SELECT *,然后在一个循环中的客户端代码计数,而不是使用COUNT SQL聚合函数
  • 您没有配置来发送发送更多的数据,而不是许多小AJAX的要求较少的请求正确任何IDisposable的资源,如数据库连接,命令和读者
  • 您正在使用字符串连接来构建您的SQL查询,而不是使用参数化查询
  • 你没有考虑到AJAX
的异步性

提到的问题,让我们开始修复它们。

让我们先来解决服务器端代码:

[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 [email protected] AND Active = 'true'"; 
      cmd.Parameters.AddWithValue("@Code", i); 
      return (int)cmd.ExecuteScalar(); 
     } 
    } 
} 

OK,现在你会发现,我加入了新的方法,可以计算合计的月数,并返回它们作为一个整数数组避免在毫无意义的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); 
});