我试图从数据库中获取数据显示在ASP.NET DataGrid
中。
显示上述错误。
这是选择命令:
public MasterJobList GetListForGrid(int RecCount, int PageNo, string OrderBy)
{
strSql = "WITH TempTable AS(Select JobDetails.JobCode,JobDetails.CurrentStatus,MasterModel.Name As ModelNumber,MasterModel.Code As ModelCode,MasterBrand.Code As BrandCode,MasterBrand.Name As BrandName,MasterDeviceType.Code As DeviceCode,MasterDeviceType.Name As DType,ROW_NUMBER() OVER (ORDER BY " + OrderBy + ") AS RowNumber From JobDetails JobDetails Inner Join MasterDeviceType ON JobDetails.DType = MasterDeviceType.Code Inner Join MasterBrand ON JobDetails.BCode = MasterBrand.Code Inner join MasterModel ON JobDetails.ModelNumber = MasterModel.Code WHERE 1 = 1) SELECT * FROM TempTable WHERE RowNumber BETWEEN {2} AND {3}";
MasterJobList objList = new MasterJobList();
DataTable dt = new DataTable();
dt = objDB.GetDataTableFromSQL(strSql);
if (dt != null)
{
foreach (DataRow Dr in dt.Rows)
{
jobs obj = new jobs();
obj.JobCode =Convert.ToInt32(Dr["JobCode"].ToString());
if (Dr["DType"] != DBNull.Value)
obj.DType = Dr["DType"].ToString();
else
obj.DType = "";
if (Dr["BrandName"] != DBNull.Value)
obj.BrandName = Dr["BrandName"].ToString();
else
obj.BrandName = "";
if (Dr["ModelNumber"] != DBNull.Value)
obj.ModelNumber = Dr["ModelNumber"].ToString();
else
obj.ModelNumber = "";
if (Dr["CurrentStatus"] != DBNull.Value)
obj.CurrentStatus = Dr["CurrentStatus"].ToString();
else
obj.CurrentStatus = "";
objList.Add(obj);
}
}
return objList;
}
确切的错误是:
异常详细信息:System.Data.SqlClient.SqlException:不正确 关键字' OVER'附近的语法。
请查看我的完整代码......
答案 0 :(得分:1)
您的ROW_NUMBER
- "列"来自JOIN
s。
将其更改为:
string sql = @"
WITH TempTable AS
(
Select
JobDetails.JobCode,
JobDetails.CurrentStatus,
MasterModel.Name As ModelNumber,
MasterModel.Code As ModelCode,
MasterBrand.Code As BrandCode,
MasterBrand.Name As BrandName,
MasterDeviceType.Code As DeviceCode,
MasterDeviceType.Name As DType,
ROW_NUMBER() OVER (ORDER BY {0}) AS RowNumber
From JobDetails JobDetails
Inner Join MasterDeviceType
ON JobDetails.DType = MasterDeviceType.Code
Inner Join MasterBrand
ON JobDetails.BCode = MasterBrand.Code
Inner join MasterModel
ON JobDetails.ModelNumber = MasterModel.Code,
WHERE {1}
)
SELECT * FROM TempTable WHERE RowNumber BETWEEN {2} AND {3}";
现在使用sql = String.Format(sql, orderBy, filter, rnStart, rnEnd)
分配值。
变量可以是:
string orderBy = "ModelNumber ASC";
string filter = "BrandName = 'Sony'";
int rnStart = PageNo == 1 ? 1 : ((PageNo - 1) * RecCount) + 1;
int rnEnd = PageNo == 1 ? RecCount : PageNo * RecCount;
更新:根据您的修改显示完整方法(至少是相关代码)。
public MasterJobList GetListForGrid(int RecCount, int PageNo, string OrderBy)
{
string strSql = @"
WITH TempTable AS
(
Select
JobDetails.JobCode,
JobDetails.CurrentStatus,
MasterModel.Name As ModelNumber,
MasterModel.Code As ModelCode,
MasterBrand.Code As BrandCode,
MasterBrand.Name As BrandName,
MasterDeviceType.Code As DeviceCode,
MasterDeviceType.Name As DType,
ROW_NUMBER() OVER (ORDER BY {0}) AS RowNumber
From JobDetails JobDetails
Inner Join MasterDeviceType
ON JobDetails.DType = MasterDeviceType.Code
Inner Join MasterBrand
ON JobDetails.BCode = MasterBrand.Code
Inner join MasterModel
ON JobDetails.ModelNumber = MasterModel.Code,
WHERE {1}
)
SELECT * FROM TempTable WHERE RowNumber BETWEEN {2} AND {3}";
int rnStart = PageNo == 1 ? 1 : ((PageNo - 1) * RecCount) + 1;
int rnEnd = PageNo == 1 ? RecCount : PageNo * RecCount;
strSql = String.Format(strSql, OrderBy, "1=1", rnStart, rnEnd);
DataTable dt = objDB.GetDataTableFromSQL(strSql);
// ...
return objList;
}
答案 1 :(得分:0)
请在ssms中检查此示例并相应更改。
declare @t table(name varchar(50))
insert into @t values ('a'),('b'),('c'),('d'),('e')
select ROW_NUMBER() over ( order by name)
, name
from @t
检查评论,您只需按值
应用“orderby” WITH temptable
AS (SELECT jobdetails.jobcode,
jobdetails.currentstatus,
mastermodel.NAME AS ModelNumber,
mastermodel.code AS ModelCode,
masterbrand.code AS BrandCode,
masterbrand.NAME AS BrandName,
masterdevicetype.code AS DeviceCode,
masterdevicetype.NAME AS DType,
Row_number()
OVER (
ORDER BY " + orderby + ") AS RowNumber --here you have to assign the order by value
FROM jobdetails JobDetails
INNER JOIN masterdevicetype
ON jobdetails.dtype = masterdevicetype.code
INNER JOIN masterbrand
ON jobdetails.bcode = masterbrand.code
INNER JOIN mastermodel
ON jobdetails.modelnumber = mastermodel.code
WHERE 1 = 1)
SELECT *
FROM temptable
WHERE rownumber BETWEEN {2} AND {3}