Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads
 
Add your own
alternative version
Go to top

ZIP Code Utility

, 2 Jan 2005
This article provides an easy method to lookup a U.S. City/State by ZIP Code, or one or more ZIP Codes by City/State. It also describes a method to calculate the distance between two ZIP Codes and find all other ZIP Codes within a radius of X miles of a specified ZIP Code.
#region FILE HEADER
/// <project>ZipCodeUtil</project>
/// <assembly>SagaraSoftware.ZipCodeUtil.dll</assembly>
/// <filename>IDataProvider.cs</filename>
/// <creator>Jon Sagara</creator>
/// <description>
/// Contains the IDataProvider interface and the DataProvider class.
/// 
/// The original MS Access database was obtained from CFDynamics at 
/// http://www.cfdynamics.com/zipbase/.  
/// </description>
/// <copyright>
/// Copyright (c) 2004 Sagara Software.  All rights reserved.
/// </copyright>
/// <disclaimer>
/// This file is provided "as is" with no expressed or implied warranty.  The author accepts no 
///  liability for any damage/loss of business that this product may cause.
/// </disclaimer>
/// <history>
///	<change date="12/29/2004" changedby="Jon Sagara">File created.</changed>
/// </history>
#endregion

using System;

namespace SagaraSoftware.ZipCodeUtil
{
	/// <summary>
	/// Defines methods common to all data providers.  An instance of this interface is used to 
	///  interact with the ZIP Codes database.  
	/// </summary>
	public interface IDataProvider
	{
		Location DoLookupByZipCode (string inZipCode);
		Location[] DoLookupByCityState (string inCity, string inState);
		LocationInRadius[] GetLocationsWithinRadius (Location inRefLoc, RadiusBox inBounds);
	}


	/// <summary>
	/// The DataProvider class has only one static method that returns an instance of a class that
	/// defines the IDataProvider interface.  The type of instance returned is determined by the
	/// "ZipCodeProviderType" key in the component host's application config file.  Supported types
	/// are Access, MS SQL, and MySQL.
	/// </summary>
	public class DataProvider
	{
		public static IDataProvider GetDataProvider ()
		{
			string strProviderType = System.Configuration.ConfigurationSettings.AppSettings["ZipCodeProviderType"];

			if (null == strProviderType || string.Empty == strProviderType)
				throw new ApplicationException ("The host application must define the ZipCodeProviderType key in the config file.");

			switch (strProviderType.ToUpper ())
			{
				case "ACCESS":
					return new AccessProvider ();
				case "MSSQL":
					throw new NotImplementedException ("SqlProvider is not yet implemented");
				case "MYSQL":
					throw new NotImplementedException ("MySqlProvider is not yet implemented");
				default:
					throw new ApplicationException ("Invalid database provider type specified in config file.");
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jon Sagara
Software Developer (Senior) Sagara Software, Inc.
United States United States
Jon graduated from Cal Poly with a B.S. Computer Engineering. He is currently building a Silverlight-based report scheduling interface for a pharmaceutical reporting company.
 
When he's not fooling around with computers or reading, he's busy spending time with his super wife, Kelly, his two boys, and their rambunctious dog, Homer.
 
Visit my blog
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 2 Jan 2005
Article Copyright 2005 by Jon Sagara
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid