当前位置:编程学习 > C#/ASP.NET >>

这是关于northwind数据库查询的c#代码,无论怎么查,都是返回无结果,纳闷??????????那位高手帮妹妹查查看,先谢了

lstProducts.Items.Clear(); 
            //  here we go.... 
            // let's think about this.  what do we need to build the 
            // correct sql string 
            string sql = "SELECT * from  Products where 1=1 "; 

            SqlConnection objconnection = 
                  new SqlConnection(connection); 
            SqlCommand objCommand = new SqlCommand(); 

            if (cboSearchCategory.SelectedIndex != -1) 
            { 
                Categories categ = new Categories (); 
                categ = (Categories)cboSearchCategory.SelectedItem; 
                sql += "and CategoryID=@categId "; 
                objCommand.Parameters.AddWithValue("@categId", 
                    categ.getCategoryId()); 
            } 

            if (cboSearchSupplier .SelectedIndex !=-1) 
            { 
                // there's something selected in here 
                // get it and and the appropriate where clause 
                Suppliers supl = new Suppliers(); 
                supl = (Suppliers)cboSearchSupplier.SelectedItem; 
                sql += " and SupplierID=@supId "; 
                objCommand.Parameters.AddWithValue("@supId", 
                    supl.getSupplierId()); 
            } 

            if (chbSearchDiscontinued.Checked) 
            { 
                sql += " and Discontinued=1 "; 
            } 
            else if (!chbSearchDiscontinued.Checked) 
            { 
                sql += " and Discontinued=0 "; 
            } 

            objCommand.Connection = objconnection; 
            objCommand.CommandText = sql; 

            try 
            { 
                objconnection.Open(); 
                SqlDataReader reader = 
                    objCommand.ExecuteReader(); 
                while (reader.Read()) 
                { 
                    Products prod = new Products(); 
                    prod.setProductId(reader.GetInt32(0)); 
                    prod.setProductName(reader.GetString(1)); 
                    //prod.setQuantityPerUnit(reader.GetString(4)); 
                    //prod.setUnitPrice(reader.GetDecimal(5)); 
                    //prod.setUnitsInStock(reader.GetInt32(6)); 
                    //prod.setUnitsOnOrder(reader.GetInt32(7)); 
                    //prod.setReOrderLevel(reader.GetInt32(8)); 
                    //prod.setDisContinued(reader.GetBoolean(9)); 
                  
                    lstProducts .Items.Add(prod); 
                } 
                if (lstProducts.Items.Count == 0) 
                { 
                    MessageBox.Show("There were no results to display for your query"); 
                } 
            } 
            catch (SqlException ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
            finally 
            { 
                objconnection.Close(); 
            } --------------------编程问答--------------------

            objCommand.Connection = objconnection; 
            objCommand.CommandText = sql; 

你可以在我引用这段代码中加个断点调试看看,调试的时候注意观察下objCommand.CommandText的内容,并且将这个内容拷贝到查询分析其中运行下看出什么结果,是不是你预期的结果?不是的话估计就是你前面部分判断什么的弄的不对。 --------------------编程问答-------------------- 这类问题,调试就能解决了。。 --------------------编程问答-------------------- 老兄 ,像楼上所说,调试解决。
补充:.NET技术 ,  其他语言
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,