1

我正在使用 VS 2012 和 SQL Express

我正在尝试构建一个 Windows 窗体应用程序来搜索 C# 中的数据库,它在表单上有不同的控件,这些控件作为参数传递给查询。

查询中的参数不一定传递一些次

我正在尝试使用以下代码示例。

SELECT a.ID AS 'DealID', a.TradeDate, c.COMPANYNAME AS 'Seller Company', 
       a.SellCommission, h.BROKER_FULLNAME AS 'Seller Trader', 
       j.DisplayName AS 'Seller Broker', d.COMPANYNAME AS 'Buyer Company', 
       a.BuyCommission, g.BROKER_FULLNAME AS 'Buyer Trader', 
       i.DisplayName AS 'Buyer Broker', e.PRODUCT_NAME, f.TYPE_DESC AS 'Quantity Type',
       f.NBR_OF_GALLONS AS 'Quantity Multiplier', a.ContractVolume, a.TotalVolume, 
       a.DeliveryPoint, a.Price, a.ContractStart, a.ContractEnd
FROM Confirmations AS a WITH (nolock) 
LEFT OUTER JOIN COMPANY AS c WITH (nolock) 
  ON c.COMPANY_ID = a.SellCompany
LEFT OUTER JOIN COMPANY AS d WITH (nolock) 
  ON d.COMPANY_ID = a.BuyCompany 
LEFT OUTER JOIN BIOPRODUCTTYPES AS e WITH (nolock) 
  ON e.ID = a.ProductID 
LEFT OUTER JOIN BIO_QUANTITY_TYPE AS f WITH (nolock) 
  ON f.ID = a.QuantityTypeID 
LEFT OUTER JOIN COMPANYBROKER AS g WITH (nolock) 
  ON g.COMPANYBROKER_ID = a.BuyTrader 
LEFT OUTER JOIN COMPANYBROKER AS h WITH (nolock) 
  ON h.COMPANYBROKER_ID = a.SellTrader 
LEFT OUTER JOIN Users AS i WITH (nolock) 
  ON i.ID = a.BuyBroker 
LEFT OUTER JOIN Users AS j WITH (nolock) 
  ON j.ID = a.SellBroker
WHERE (@fromdate IS NULL OR @fromdate=' ' OR a.TradeDate >= @fromdate) 
       AND (@todate IS NULL OR @todate=' ' OR a.TradeDate <= @todate) 
       AND (@buycompanyname IS NULL 
            OR @buycompanyname="" 
            OR a.BuyCompany = (SELECT COMPANY_ID
                               FROM COMPANY
                               WHERE (COMPANYNAME = @buycompanyname)))              
       AND (@sellcompanyname IS NULL 
            OR @sellcompanyname="" 
            OR a.SellCompany = (SELECT COMPANY_ID
                                FROM COMPANY
                                WHERE (COMPANYNAME =@sellcompanyname))) 
       AND (@product IS NULL OR @product="" OR e.PRODUCT_NAME= @product)";

除了使用上述查询之外,我是否可以根据我传递的参数动态创建一个查询,这似乎更合乎逻辑,因为如果表中的列具有空值,它不会查找记录。

4

3 回答 3

1

这就是创建ORM 的目的。通过用在运行时构建查询的东西(如Entity FrameworkNHibernate)替换您的硬编码查询,它会为您构建查询的 theSELECTWHERE部分。

通过正确设置对象,您可以使用如下实体框架

Nullable<DateTime> fromDate = //...
Nullable<DateTime> toDate = //...
string buyCompany = //...

//(Snip)

using(var ctx = new MyContext())
{
    var query = ctx.Order;

    if(fromDate.HasValue)
        query = query.Where(ent=> ent.TradeDate >= fromDate.Value);
    if(toDate.HasValue)
        query = query.Where(ent => ent.TradeDate <= toDate.Value);
    if(String.IsNullOrWhitespace(buyCompany) == false)
        query = query.Where(ent => ent.BuyCompany.CompanyName = buyCompany);

    //(Snip)

    return query.ToList();
}
于 2013-07-31T18:24:41.323 回答
0

是的,只需构建您的 sql 查询字符串。

设置你然后设置你SqlCommand.CommandTypeCommandType.Text 参数SqlCommand.Parameters.AddWidthValue(string, object);

于 2013-07-31T18:22:13.327 回答
0

如果您正在调用存储过程,我建议动态构建 SQL 字符串以仅使用您将使用的参数,然后调用 sp_executesql。存储过程如下所示:

DECLARE @sql =nvarchar(MAX), @Parameters nvarchar(max)
SET @sql = 'SELECT * FROM [dbo].[Foo] WHERE Column1 = @Param1'
SET @Parameters = '@Param1 nvarchar(32), @Param2 nvarchar(32)'
IF(@Param2 is not null and @Param2 <> ' ') SET @sql = @sql + ' AND Column2 = @Param2'
EXEC sp_executesql @Sql, @Parameters, @Param1, @Param2

如果您在 C# 中构建查询字符串而不是存储过程,那么这个想法基本上是相同的:

command.CommandText = "SELECT * FROM [dbo].[Foo] WHERE Column1 = @Param1";
command.Parameters.AddWithValue("@Param1", param1);
if(!String.IsNullOrEmpty(param2))
{
    command.CommandText += " AND Column2 = @Param2";
    command.Parameters.AddWithValue("@Param2", param2);
}
于 2013-07-31T20:17:17.007 回答