非常简单的一小小段代码 请大家帮我解释一下哦~
1.这一小段代码是一个程序BLL层下的代码,是按降序排出前5名,不知道代码有没有问题~~如果有问题希望可以帮我改一下哦!
2.希望可以帮我解释一下如果可以每句都解释一下就更加好了,就是简单指点一下也行,我会做笔记的。
3.之后可以再概括一下,中间的INNER JOIN部分我完全不明白~~~~
[code=C#][/code]using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL;
namespace BLL
{
public class Sales
{
public static string SP_Top5Sales = "SP_Top10Sales";
public static string SP_SalesOrder_ByName = "SP_SalesOrder_ByName";
/// <summary>
/// T-SQL
/// </summary>
/// <returns></returns>
public DataTable GetTop5Sales(string PaymentMethod, string CardHolder)
{
DataAccess da = new DataAccess();
string SQL = string.Format(@"SELECT Top 5 with ties c.FirstName, c.LastName, count(c.FirstName) as ordercount, sum(d.LineTotal) as Total
FROM Person.Contact AS c INNER JOIN
Sales.SalesOrderHeader AS h ON c.ContactID = h.ContactID INNER JOIN
Sales.CreditCard AS z ON h.CreditCardID = z.CreditCardID INNER JOIN
Sales.SalesOrderDetail AS d ON h.SalesOrderID = d.SalesOrderID
WHERE (z.CardType = '{0}') AND (z.CardNumber like '%{1}%') AND (c.FirstName like '%{2}%')
Group by c.FirstName, c.LastName
Order by Total DESC",cardtype,cardno,cardholder);
return da.ExecSQLRtnData(SQL);
}
/// <summary>
/// VW_SalesOrder_ByReasonType
/// </summary>
/// <returns></returns>
public DataTable Get_SPSales_OrderByReasonType()
{
DataAccess da = new DataAccess();
return da.ExecSPReturnData(SP_SalesOrder_ByName);
}
}
}
------解决方案--------------------就是个执行sql语句,inner join 是sql语法,联合两个表根据某个或某些字段,比如
select * from A inner join B on A.id=B.id
查找AB两个表中id相同的记录
------解决方案--------------------简单说就是一个sql
几张表的关联查询
查询Person.Contact和 Sales.SalesOrderDetail 表的数据
inner join 是两个表之间的关联
非常简单
如果看不懂可以将c,d,h,z都替换成原来的表名就明白了
------解决方案--------------------public DataTable Get_SPSales_OrderByReasonType()
{
DataAccess da = new DataAccess();
return da.ExecSPReturnData(SP_SalesOrder_ByName);
}
是获取数据表的一个方法(函数)呐
------解决方案--------------------“SP_SalesOrder_ByName” 这是一个存有“存储过程名称”的变量
------解决方案--------------------把存储过程返回的datatable返回