如果我在数据库里要查一个大区,省市,城市我需要进行判断.从小的城市判断,如果没有城市  则添加城市,如果没有大区则添加大区.以此类推.完整的代码  给个解释.

解决方案 »

  1.   

    LZ  老是回答你的问题.//当大区下面有省市,省市没有城市的时候
                if (!string.IsNullOrEmpty(this.txtRegion.Text) && !string.IsNullOrEmpty(this.txtProvince.Text) && string.IsNullOrEmpty(this.txtCity.Text))
                {
                    SqlCommand SqlCmd = new SqlCommand();
                    //SQL语句则添加进去此省市
                    SqlCmd.CommandText = "INSERT INTO BaseGeography " +
                                                      " (Name_EN,ParentID) " +
                                                      "  VALUES " +
                                                      " ('Name_EN','Name_EN')";                AddCity();
                }
                //当三个东西都有的时候
                if (!string.IsNullOrEmpty(this.txtRegion.Text) && !string.IsNullOrEmpty(this.txtProvince.Text) && !string.IsNullOrEmpty(this.txtCity.Text))
                {
                    SqlCommand SqlCmd = new SqlCommand();
                    //都有的时候则给其提示说此东西都有
                    SqlCmd.CommandText = "INSERT INTO BaseGeography " +
                                                      " (Name_EN,CitySN,ParentID) " +
                                                      "  VALUES " +
                                                      " ('Name_EN','CitySN','Name_EN')";
                    AddCity();
                }
            }
    SqlCmd.CommandText = "SELECT  COUNT(*)   FROM ( SELECT  Regoin.Name_CN AS Regoin," +
                                                                " Procince.Name_CN AS Procince ," +
                                                                " City.Name_CN AS City " +
                                                                " FROM " +
                                                                " BaseGeography AS Regoin LEFT JOIN " +
                                                                " BaseGeography AS  Procince ON Regoin.CityID=Procince.ParentID LEFT JOIN " +
                                                                " BaseGeography AS City ON City.ParentID=Procince.CityID) A" +
                                                                " WHERE   A.Regoin =' " + this.txtRegion.Text + "'" +
                                                                " AND A.Procince ='" + this.txtProvince.Text + " '" +
                                                                " AND A.City ='" + this.txtCity.Text + "'";
                    DataSet ds;
                    if (AccessSqlCmd.Execute(out ds))
                    {
                        ds.Tables["info"].Rows[0][0].ToString();
                    }
                   
                    //如果大区的文本框里面不为空.
                    if (string.IsNullOrEmpty(this.txtRegion.Text) && string.IsNullOrEmpty(this.txtProvince.Text) && string.IsNullOrEmpty(this.txtCity.Text))
                    {
                        //判断大区的txtBox有没有值
                        if (this.txtRegion.Text == null)
                        {
                            Response.Write("大区不能为空");
                            SqlCmd.CommandText = "SELECT * FROM BaseGeography WHERE LevelID=0 OR ParentID=CityID";
                            //对比数据库大区的值
                            //如果有,则Eorr !
                            //else 则 INSERT  
                        }
                    }
                    AddCity();
    参考一下吧 .
      

  2.   

    看 楼主的思路 蛮清晰的嘛。  哪里 有问题,代码 还是 sql语句有问题?