有人可以帮我吗?我通常不在 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%'
谢谢!