组合框凡条款

时间:2015-01-17 00:02:33

标签: c# sql-server where

我有一张POMain po_no表和一张Shipping发票表,然后当我搜索po_no时,我会添加一张发票。我想要做的是,如果po_no已经有发票,搜索按钮中的po_no将不会出现

public AddForm()
            {
                InitializeComponent();
    string ID = cb_po_search.SelectedValue.ToString();
                string strPRSconn = ConfigurationManager.ConnectionStrings["POSdb"].ConnectionString;
                SqlConnection sc = new SqlConnection(strPRSconn);
                sc.Open();
                string strQry = "SELECT POMain.po_no FROM POMain LEFT JOIN Shipping ON POMain.po_no = Shipping.po_no WHERE Shipping.invoice IS NULL AND POMain.po_no = '" + ID + "'";
                SqlCommand scmd = new SqlCommand(strQry, sc);
                SqlDataReader dr = scmd.ExecuteReader();

                DataTable dt = new DataTable();
                dt.Columns.Add("po_no", typeof(string));
                dt.Load(dr);

                cb_po_search.ValueMember = "po_no";
                cb_po_search.DisplayMember = "po_no";
                cb_po_search.DataSource = dt;

                sc.Close();

            }

            }

1 个答案:

答案 0 :(得分:0)

您需要在此使用LEFT OUTER加入而不是INNER加入

string strQry ="SELECT POMain.po_no FROM POMain INNER JOIN Shipping ON POMain.po_no = Shipping.po_no WHERE Shipping.invoice IS NULL";

你需要在最后的WHERE子句中添加另一个条件:

AND POMain.po_no = " + ID

话虽如此,使用存储过程而不是尝试形成内联查询会更好。