尝试将 where 子句附加到 sql 语句
您好,我正在尝试将 where 子句附加到这样的 sql 语句中,
public static DataTable paymentType(string paymenttype, string ddproviders, string overdue)
{
string paymenttypestr = "";
string ddproviderstr = "";
if (paymenttype != "")
{
paymenttypestr = string.Format("AND membertomships.memberToMship_PayMethod = '{0}'", paymenttype);
}
if (ddproviders != "")
{
ddproviderstr = string.Format("AND ddproviders.ddProvider_Name = '{0}'", ddproviders);
}
if (overdue == "OverDue-Now")
{
string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";
}
string sql = string.Format(
@"SELECT members.member_Id,
members.member_Lastname As Last_name,
members.member_Firstname AS First_name,
members.member_PostCode As Post_Code,
ddaccounts.ddAccount_DdReference As dd_reference,
ddproviders.ddProvider_Name As dd_providername,
memberToMship_ChargePerPeriod As monthly_amount,
mshiptypes.mshipType_Name As Membership_type,
mshipstatustypes.mshipStatusType_Name As Status,
membertomships.memberToMship_EndDate As Expiry_Date,
membertomships.memberToMship_PayMethod As payment_method
FROM members
LEFT JOIN membertomships ON membertomships.member_Id = members.member_Id
LEFT JOIN memberpaysched ON memberpaysched.memberPaySched_memberId = members.member_Id
LEFT OUTER JOIN ddaccounts ON ddaccounts.member_Id = members.member_Id
LEFT OUTER JOIN ddproviders ON ddaccounts.ddProvider_Id=ddproviders.ddProvider_Id
LEFT JOIN mshipstatustypes ON mshipstatustypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
LEFT JOIN mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
LEFT JOIN mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
WHERE members.member_Active LIKE 'y%'
AND mshipoptions.mshipOption_Period = 'month'
AND (mshipstatustypes.mshipStatusType_Id <> 5)
{0}
{1}
ORDER BY members.member_Lastname",
paymenttypestr, ddproviderstr);
return getdata(sql,mf);
}
但它在这一行给出了错误,如“;”需要这个符号;
string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";
我想添加这个 where 子句 if overdue == "overdue-now"
我怎样才能根据条件添加这个 where 子句,
任何人都会对此提供帮助....
Modified Query : string sql += @"memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid";
仍然显示此错误 “无效的表达术语+=”
HiI am trying to attach the where clause to sql statement like this
public static DataTable paymentType(string paymenttype, string ddproviders, string overdue)
{
string paymenttypestr = "";
string ddproviderstr = "";
if (paymenttype != "")
{
paymenttypestr = string.Format("AND membertomships.memberToMship_PayMethod = '{0}'", paymenttype);
}
if (ddproviders != "")
{
ddproviderstr = string.Format("AND ddproviders.ddProvider_Name = '{0}'", ddproviders);
}
if (overdue == "OverDue-Now")
{
string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";
}
string sql = string.Format(
@"SELECT members.member_Id,
members.member_Lastname As Last_name,
members.member_Firstname AS First_name,
members.member_PostCode As Post_Code,
ddaccounts.ddAccount_DdReference As dd_reference,
ddproviders.ddProvider_Name As dd_providername,
memberToMship_ChargePerPeriod As monthly_amount,
mshiptypes.mshipType_Name As Membership_type,
mshipstatustypes.mshipStatusType_Name As Status,
membertomships.memberToMship_EndDate As Expiry_Date,
membertomships.memberToMship_PayMethod As payment_method
FROM members
LEFT JOIN membertomships ON membertomships.member_Id = members.member_Id
LEFT JOIN memberpaysched ON memberpaysched.memberPaySched_memberId = members.member_Id
LEFT OUTER JOIN ddaccounts ON ddaccounts.member_Id = members.member_Id
LEFT OUTER JOIN ddproviders ON ddaccounts.ddProvider_Id=ddproviders.ddProvider_Id
LEFT JOIN mshipstatustypes ON mshipstatustypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
LEFT JOIN mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
LEFT JOIN mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
WHERE members.member_Active LIKE 'y%'
AND mshipoptions.mshipOption_Period = 'month'
AND (mshipstatustypes.mshipStatusType_Id <> 5)
{0}
{1}
ORDER BY members.member_Lastname",
paymenttypestr, ddproviderstr);
return getdata(sql,mf);
}
but it was giving error at this line like ";" this symbol is needed;
string sql += @"WHERE memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid ";
I want to add this where clause if overdue == "overdue-now"
how can i add this where clause according to condition
would any one help on this ....
Modified Query : string sql += @"memberpaysched.memberPaySched_dateDue < NOW() AND memberpaysched.memberPaySched_amountDue > memberpaysched.memberPaySched_amountPaid";
still it was showing this error
"Invalid EXPRESSION TERM +="
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
乍一看你有 2 个 where 子句。更改要添加到 AND 的值,看看会发生什么。
On 1st glance you have 2 where clauses. Change the one you're adding to an AND and see what happens.
因此,如果 overdue == "OverDue-Now" 您正在声明一串 sql 并 += ing 它?但随后您会覆盖您认为刚刚创建的字符串。
我认为这就是您想要实现的目标:
So if overdue == "OverDue-Now" you are declaring a string of sql and +=ing it? But then you overwrite the string you thought you just created.
I think this is what you're trying to accomplish:
我不是 .NET 专家,但我可能会将:更改
为:(
即删除字符串关键字,并用 AND 替换 WHERE)
并将其放在主 SQL 块之后(即声明字符串 sql 变量的位置) ..
I'm no .NET expert, but I'd probably change:
to:
(i.e. remove the string keyword, and substitute AND for WHERE)
and put it AFTER the main SQL block (i.e. where you're declaring your string sql variable)..