在以下语法方面需要帮助

need help in following syntax

public static DataTable LoadGrid(string SelectedItem,string yearSelected)

{

 DataTable tbl;

string a = SelectedItem;

string b = yearSelected;

string query =" SELECT top 10 SalesOrderID, RevisionNumber, OrderDate,DueDate, 
                            "ShipDate, Status,OnlineOrderFlag,SalesOrderNumber,PurchaseOrderNumber,AccountNumber, CustomerID, SalesPersonID, st.Name AS TerritoryName,BillToAddressID, ShipToAddressID,ShipMethodID, CreditCardID, CreditCardApprovalCode,CurrencyRateID, SubTotal, TaxAmt, Freight,TotalDue,Comment, soh.rowguid, soh.ModifiedDate FROM Sales.SalesOrderHeader soh INNER JOIN Sales.SalesTerritory st ON soh.TerritoryID = st.TerritoryID"+
WHERE  st.Name = "+a+"AND Datepart(year,OrderDate) = "+b;

tbl=DataAccess.cmd(query);
return (tbl);

}

传递给 a 的数据是领土,传递给 b 的是 b 是来自 2 个不同下拉列表的年份,但查询给出错误,指出无效的列名加拿大(假设我选择加拿大从下拉列表中)和 canada 是 table in territory table in Name column

的项目

错误的原因是缺少引号。目前查询是 WHERE st.Name = Candada 虽然它应该是 WHERE st.Name = 'Canada' 相反。

尽管如此,请考虑使用存储过程而不是使用内联 SQL 查询。

更具体地说,您应该使用以下语法

string query = string.Format(
            "SELECT top 10 SalesOrderID, RevisionNumber, OrderDate,"
           +" DueDate,ShipDate, Status,OnlineOrderFlag,SalesOrderNumber,"
        +"PurchaseOrderNumber,AccountNumber, CustomerID,SalesPersonID, st.Name AS TerritoryName,"
        +"BillToAddressID, ShipToAddressID,ShipMethodID,CreditCardID,"
        +"CreditCardApprovalCode,CurrencyRateID, SubTotal, TaxAmt, Freight,TotalDue,Comment,"
        +"soh.rowguid, soh.ModifiedDate FROM Sales.SalesOrderHeader soh "
        +"INNER JOIN Sales.SalesTerritory st ON soh.TerritoryID = st.TerritoryID "
       +" WHERE st.Name='{0}' AND Datepart(year,OrderDate) = '{1}'",a,b);

但在这类工作中使用 sp...:)

SELECT top 10 SalesOrderID, RevisionNumber, OrderDate,DueDate, 
                            "ShipDate, Status,OnlineOrderFlag,SalesOrderNumber,PurchaseOrderNumber,AccountNumber, CustomerID, SalesPersonID, st.Name AS TerritoryName,BillToAddressID, ShipToAddressID,ShipMethodID, CreditCardID, CreditCardApprovalCode,CurrencyRateID, SubTotal, TaxAmt, Freight,TotalDue,Comment, soh.rowguid, soh.ModifiedDate FROM Sales.SalesOrderHeader soh INNER JOIN Sales.SalesTerritory st ON soh.TerritoryID = st.TerritoryID"+
WHERE  st.Name = "+a+"AND Datepart(year,OrderDate) = "+b;

为什么要用+运算符一遍又一遍的组合字符串,效率很低而且难读,为什么不用"{0}".format()?

public static DataTable LoadGrid(string selectedItem,string yearSelected)
{
    DataTable tbl;
    string query = string.Format( @" SELECT top 10 SalesOrderID, RevisionNumber, OrderDate,DueDate,ShipDate, Status,OnlineOrderFlag,SalesOrderNumber,PurchaseOrderNumber,AccountNumber, CustomerID, SalesPersonID, st.Name AS TerritoryName,BillToAddressID, ShipToAddressID,ShipMethodID, CreditCardID, CreditCardApprovalCode,CurrencyRateID, SubTotal, TaxAmt, Freight,TotalDue,Comment, soh.rowguid, soh.ModifiedDate 
                FROM Sales.SalesOrderHeader as soh 
                INNER JOIN Sales.SalesTerritory as st 
                    ON soh.TerritoryID = st.TerritoryID
                WHERE  st.Name = '{0}' AND Datepart(year,OrderDate) = '{1}'", selectedItem, yearSelected);

    tbl=DataAccess.cmd(query);
    return (tbl);
}

这可能会提高代码的可读性,从而使编写的查询更具可读性并且更不容易出现语法错误。