View Full Version : درست کردن شرط
sadegh.te
یک شنبه 14 دی 1393, 17:32 عصر
سلام دوستان گرامی من در کد زیر برای گذاشتن شرط از sql استفاده کرم و حالا میخوام این شرط را طوری تغییر دهم که زمانی که چکباس ios را true میکنم فقط گوشی هایی را نمایش بده که سیستم عامل آنها ios
هستش و وقتی که چکباس ios و چکباکس red را true کردم گوشیهایی را نمایش دهد که سیستم عامل آنها ios و رنگ آنها هم قرمز باشد.
اگه میشه راهنمایی کنیید که چطور این شرط را دست کنم؟ ممنون
if (os!=="") {
filterdProducts = alasql('SELECT * FROM ? WHERE os LIKE "' + i + '%" and color LIKE "' + colors + '%"', [products]);
}
meisam12
یک شنبه 14 دی 1393, 19:23 عصر
با سلام شما باید به صورت سلسه مراتبی و هوشمندانه این گونه کدها را تولید کنید به شکل زیر
public string GenerateTSql(string os, string color)
{
string command = "select * from dbName";
List<string> conditions = new List<string>();
if (!string.IsNullOrEmpty(os))
{
conditions.Add("os like '" + os + "%'");
}
if (!string.IsNullOrEmpty(color))
{
conditions.Add("color like '" + color + "%'");
}
if (conditions.Any())
{
command += " where ";
}
int counter = 0;
foreach (string condition in conditions)
{
if (counter == 0)
{
command += condition + " ";
}
else
{
command += " and " + condition;
}
counter++;
}
return command;
}
sadegh.te
یک شنبه 14 دی 1393, 20:56 عصر
با سلام شما باید به صورت سلسه مراتبی و هوشمندانه این گونه کدها را تولید کنید به شکل زیر
public string GenerateTSql(string os, string color)
{
string command = "select * from dbName";
}
خیلی ممنون از شما ولی من یه کوئری میخوام که این کار رو برام انجام بده چون دارم این کا رو با jquery انجام میدم.
ham3d1988
دوشنبه 15 دی 1393, 10:42 صبح
سلام
بهتره یه پراسیجر بنویسید بعدش توش کوئری داینامیک تولید کنید
این نمونه شاید کمکتون کنه:
create Procedure XXX
(
@RecId int,
@DateFrom datetime,
@DateTo datetime
)
declare @Statement nvaechar(max)
-----------------------------------
set @statement='select * from TableName where 1=1'
if (@DateFrom is not null and @DateFrom<>'')
set @statement=@statement+ ' and RecCreationDate>= ''' + @DateFrom + ''''
if (@DateTo is not null and @DateFrom<>'')
set @statement=@statement+ ' and RecCreationDate<= ''' + @DateTo + ''''
if (@RecId is not null and @RecId<>-1)
set @statement=@statement+ ' and RecId= ' + @RecId
print @statement
sp_executesql @statement
.
.
.
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.