-4

有人可以帮我吗?我通常不在 C# 中工作,所以我很抱歉。我有以下有效的查询,但需要将 store.Zip 查找更改为 aLIKE而不是=. 当我尝试在编译后更改=toLIKE并添加,但不运行。数据库是 Sql Server 2008:%{0}

public List<Store> DoesStoreExist(string customerNumber, string zipCode) {
    StringBuilder sb = new StringBuilder();
    sb.Append("SELECT id, Company, CustomerNumber, Name, Address, City, State, Zip, Phone, Latitude, Longitude, NumOfReferrals, IsNashville, PredsPromotionParticipant ");
    sb.Append("FROM dbo.Stores store ");
    sb.AppendFormat("WHERE store.CustomerNumber='{0}' ", customerNumber);
    sb.AppendFormat("AND store.Zip ='{0}' ", zipCode);
    sb.AppendFormat("AND store.Locator=1");

    IQuery query = NHibernateSession.CreateSQLQuery(sb.ToString());
    IList results = query.List();
    List<Store> stores = new List<Store>();
    foreach (object result in results) {
        object[] result_arr = result as object[];
        if (result_arr != null) stores.Add(Store.From(result_arr));
    }
    return stores;

}

即使只是将“=”更改为“LIKE”也会导致查询不返回:

public List<Store> DoesStoreExist(string customerNumber, string zipCode) 
{        
        StringBuilder sb = new StringBuilder();
        sb.Append("SELECT id, Company, CustomerNumber, Name, Address, City, State, Zip, Phone, Latitude, Longitude, NumOfReferrals, IsNashville, PredsPromotionParticipant ");
        sb.Append("FROM dbo.Stores store ");
        sb.AppendFormat("WHERE store.CustomerNumber='{0}' ", customerNumber);
        sb.AppendFormat("AND store.Zip LIKE '{0}%' ", zipCode);
        sb.AppendFormat("AND store.Locator=1");

    IQuery query = NHibernateSession.CreateSQLQuery(sb.ToString());
    IList results = query.List();
    List<Store> stores = new List<Store>();
    foreach (object result in results) {
        object[] result_arr = result as object[];
        if (result_arr != null) stores.Add(Store.From(result_arr));
    }
    return stores;
}

当我直接对数据库运行以下命令时,它可以工作:

SELECT * FROM [StoreDB].[dbo].[Stores]WHERE Zip LIKE '33815%'

谢谢!

4

3 回答 3

0

If it times out, then you need to increase the command timeout. Assuming your entity object is called 'db', do this before executing the query in your c# code.

db.CommandTimeout = 300;

The timeout is in seconds.

于 2013-07-08T13:36:51.770 回答
0

尝试这个

    public List<Store> DoesStoreExist(string customerNumber, string zipCode)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("SELECT id, Company, CustomerNumber, Name, Address, City, State, Zip, Phone, Latitude, Longitude, NumOfReferrals, IsNashville, PredsPromotionParticipant ");
        sb.Append("FROM dbo.Stores store ");
        sb.AppendFormat("WHERE store.CustomerNumber ='{0}' ", customerNumber);
        sb.AppendFormat("AND store.Zip like '{0}%' ", zipCode);
        sb.AppendFormat("AND store.Locator=1");
    }
于 2013-07-08T13:28:54.707 回答
0

这应该有效: sb.AppendFormat("AND store.Zip like '{0}%' ", zipCode);

于 2013-07-08T13:30:18.280 回答