31 thg 3, 2012

Phương thức lấy CSDL SQL SERVER thông qua Web Service



//Phương thức lấy tên thành phố theo mã thành phố được truyền vào.
        public static Model.City GetCityIdOfNameCity(string cityId)
        {


         
            try
            {


                conn = new SqlConnection(cs);
                string sql = "SELECT cityName FROM city WHERE cityId = '" + cityId + "' ";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                reader = cmd.ExecuteReader();
                reader.Read();


                Model.City city = new Model.City();


            //    city.CityID = reader["cityId"].ToString();
                city.CityName = reader["cityName"].ToString();
            //    city.CityDescript = reader["cityDescription"].ToString();
                return city;
            }
            catch (Exception exp)
            {
                //Adding logging
                HttpContext.Current.Trace.Warn("Error", "Error in GetCityIdOfNameCity", exp);
            }
            finally
            {
                if (reader != null) reader.Close();
                if (conn != null && conn.State != ConnectionState.Closed) conn.Close();
            }
            return null;
        }



        //Phương thức lấy tất cả tên thành phố trong bảng "city"
        // Kết quả trả về là 1 List
        public static List<string> GetAllCity()
        {


            try
            {
                conn = new SqlConnection(cs);
                string sql = "SELECT cityName FROM city";
                SqlCommand command = new SqlCommand(sql, conn);
                conn.Open();
                reader = command.ExecuteReader();
                 List<string> _allCity = new List<string>();
                while (reader.Read())
                {
                    _allCity.Add(reader.GetString(0));
                }
                return _allCity;


            }
            catch (Exception)
            {
                HttpContext.Current.Trace.Warn("Error", "Error at GetAllCity !!!!");
            }
            finally
            {
                if (reader != null) reader.Close();
                if (conn != null && conn.State != ConnectionState.Closed) conn.Close();
            }
            return null;
        }


        //Phương thức lấy tất cả tên ngân hàng trong bảng "bank"
        // Kết quả trả về là 1 List

        public static List<string> GetAllBank()
        {


            try
            {
                conn = new SqlConnection(cs);
                string sql = "SELECT bankName FROM bank";
                SqlCommand command = new SqlCommand(sql, conn);
                conn.Open();
                reader = command.ExecuteReader();
                List<string> _allBank = new List<string>();
                while (reader.Read())
                {
                    _allBank.Add(reader.GetString(0));
                }
                return _allBank;


            }
            catch (Exception)
            {
                HttpContext.Current.Trace.Warn("Error", "Error at GetAllBank !!!!");
            }
            finally
            {
                if (reader != null) reader.Close();
                if (conn != null && conn.State != ConnectionState.Closed) conn.Close();
            }
            return null;




        }



        //Lấy các quận theo tên mã thành phố truyền vào
        public static List<string> GetDistrictOfCity(string cityId){

            try
            {
             
             
                conn = new SqlConnection(cs);
                string sql = "SELECT distrctName FROM district WHERE cityId = '" + cityId + "' ";
                SqlCommand command = new SqlCommand(sql, conn);
                conn.Open();
                reader = command.ExecuteReader();
                List<string> _allDist = new List<string>();
                while (reader.Read())
                {
                    _allDist.Add(reader.GetString(0));
                }
                return _allDist;
            }
            catch (Exception)
            {
                HttpContext.Current.Trace.Warn("Error", "Error at GetDistrict !!!!");
            }
            finally
            {
                if (reader != null) reader.Close();
                if (conn != null && conn.State != ConnectionState.Closed) conn.Close();
            }
            return null;


      }

     
        //Lấy các cây atm theo ngân hàng và quận trong thành phố
        public static List<string> getAtms(string bankId, string districtId)
        {
            try
            {




                conn = new SqlConnection(cs);
                string sql = "SELECT atmName FROM atm WHERE districtId = '" + districtId + "' AND  bankId = '" + bankId + "'";
                SqlCommand command = new SqlCommand(sql, conn);
                conn.Open();
                reader = command.ExecuteReader();
                List<string> _allAtms = new List<string>();
                while (reader.Read())
                {
                    _allAtms.Add(reader.GetString(0));
                }
                return _allAtms;
            }
            catch (Exception)
            {
                HttpContext.Current.Trace.Warn("Error", "Error at GetDistrict !!!!");
            }
            finally
            {
                if (reader != null) reader.Close();
                if (conn != null && conn.State != ConnectionState.Closed) conn.Close();
            }
            return null;


        }


database download tại đây : http://code.google.com/p/and-project-lbd/downloads/detail?name=data.rar&can=2&q=#makechanges

Không có nhận xét nào:

Đăng nhận xét

Bản beta đầu tiên

Sau 6 tháng cả team cặm cụi làm việc điên cuồng, bản alpha cũng được giới thiệu ra toàn bộ công ty và được testing nội bộ công ty mà thôi. ...