我正在開發Windows應用程序。c#搜索功能查詢/代碼
我說,我正在實現搜索功能。
我有5個過濾器[搜索類別]。
Partycode,branchId,Symbol,BuySell,TerminalId。
如果所有文本框爲空,那麼它應該顯示所有數據。
如果Partycode被填滿,那麼它應該只顯示特定派對代碼的數據。
如果派對代碼和branchId文本框被填充,那麼它應該爲具有特定終端ID的參數提供數據。 ...和條件繼續所有排列和組合。
我寫了不同的if else語句。這些正在成爲許多If-Else聲明。
有沒有特別的方法來爲這種情況編寫查詢?還是有.NET提供的任何功能來處理它?
或者我正朝着正確的方向[有很多If-Else陳述]?
我已經試過>>
private void btnRefresh_Click_1(object sender, EventArgs e)
{
string SQL = "";
if ((txtSearchPartyCode.Text == "") && (txtSearchBranchId.Text == "") && (txtSearchSymbol.Text == "") && (txtSearchTerminalId.Text == "")&&(cmbBuySell.Text==""))
{
SQL = "select Party_Code,TradeNo,Scrip_Code,Inst_Type,Expirydate,Strike_price,Option_type,TerminalId,Branch_Id,Buy_Sell,Trade_Qty,Market_Rate,Sauda_Date,OrderNo from tradeFile";
}
else
if ((txtSearchPartyCode.Text != "") && (txtSearchBranchId.Text == "") && (txtSearchSymbol.Text == "") && (txtSearchTerminalId.Text == "")&&(cmbBuySell.Text==""))
{
SQL="select Party_Code,TradeNo,Scrip_Code,Inst_Type,Expirydate,Strike_price,Option_type,TerminalId,Branch_Id,Buy_Sell,Trade_Qty,Market_Rate,Sauda_Date,OrderNo from tradeFile where Party_Code='"+txtSearchPartyCode.Text+"'";
}
else
if ((txtSearchPartyCode.Text == "") && (txtSearchBranchId.Text != "") && (txtSearchSymbol.Text == "") && (txtSearchTerminalId.Text == "")&&(cmbBuySell.Text==""))
{
SQL="select Party_Code,TradeNo,Scrip_Code,Inst_Type,Expirydate,Strike_price,Option_type,TerminalId,Branch_Id,Buy_Sell,Trade_Qty,Market_Rate,Sauda_Date,OrderNo from tradeFile where Branch_Id='"+txtSearchBranchId.Text+"'";
}
else
if ((txtSearchPartyCode.Text == "") && (txtSearchBranchId.Text == "") && (txtSearchSymbol.Text != "") && (txtSearchTerminalId.Text == "")&&(cmbBuySell.Text==""))
{
SQL="select Party_Code,TradeNo,Scrip_Code,Inst_Type,Expirydate,Strike_price,Option_type,TerminalId,Branch_Id,Buy_Sell,Trade_Qty,Market_Rate,Sauda_Date,OrderNo from tradeFile where Scrip_Code='"+txtSearchSymbol.Text+"'";
}
else
if ((txtSearchPartyCode.Text == "") && (txtSearchBranchId.Text == "") && (txtSearchSymbol.Text == "") && (txtSearchTerminalId.Text != "")&&(cmbBuySell.Text==""))
{
SQL="select Party_Code,TradeNo,Scrip_Code,Inst_Type,Expirydate,Strike_price,Option_type,TerminalId,Branch_Id,Buy_Sell,Trade_Qty,Market_Rate,Sauda_Date,OrderNo from tradeFile where TerminalId='"+txtSearchTerminalId.Text+"'";
}
else
if((txtSearchPartyCode.Text == "") && (txtSearchBranchId.Text == "") && (txtSearchSymbol.Text == "") && (txtSearchTerminalId.Text == "")&&(cmbBuySell.Text!=""))
{
float buy_Sell=0;
if(cmbBuySell.Text=="Buy")
buy_Sell=1;
else
if(cmbBuySell.Text=="Sell")
buy_Sell=2;
SQL="select Party_Code,TradeNo,Scrip_Code,Inst_Type,Expirydate,Strike_price,Option_type,TerminalId,Branch_Id,Buy_Sell,Trade_Qty,Market_Rate,Sauda_Date,OrderNo from tradeFile where Buy_Sell='"+buy_Sell+"'";
}
try
{
da = new SqlDataAdapter(SQL, con);
DataSet ds = new DataSet();
da.Fill(ds);
gvTradeFile.DataSource = ds.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
請救我出去吧。!!!!
不是一個答案,但要避免在SQL查詢字符串連接,因爲它可以導致SQL注入。改爲使用參數化查詢。 – akton 2013-03-22 07:47:23