How To Connect Remotely To A SQL-Server Database: National Park Service
How To Connect Remotely To A SQL-Server Database: National Park Service
2
1. Introduction
This SOP documents the process for connecting remotely to a SQL-Server database.
The following figure shows the basic steps which are further detailed in the following
sections.
START
Determine
Database
Properties
Yes
Determine Tool to
Use
No
Connect to
Databse
FINISH
3
2. Determine Database Properties
To connect to a SQL-Server database, you will need to know the following:
Server Name – The path to the server. In most cases, it will look something like the
following: INP2300FCSGUMO6\GIS_Working_Data
Database Name – The name of the database on the server.
SQL Server supports two types of authentication, so you will need to determine which
one to use:
http://msdn.microsoft.com/en-us/data/aa937733.aspx
4
Both. ODBC
recommended for
Python Windows Authentication. Allows customized coding with python language
ArcGIS ODBC Integrate tables into mapping applications
Both. ODBC
recommended for
R Windows Authentication. Open source statistical software
If the application you intend to use requires ODBC, refer to Section 5. Otherwise,
proceed to the section for your software.
The following section shows you how to establish an ODBC connection to SQL-Server.
1. Be sure you have SQL Server Native Client 10.0. If you don‟t, see Section
3.
2. Select Start Control Panel
5
3. Select System and SecurityAdministrative Tools Data Source(ODBC)
6
5. Select the „User1 DSN Tab‟ and click on the Add Button
1
If you have administrative permissions, you will also be able to set up System DSNs.
7
7. Specify the following:
Name – Give the database connection an intuitive name
Description – Give the database an informative description
Server – Provide the Server Name
8
9. Select the Change Default Database
9
12. Test Data Source
If you see „TESTS COMPLETED SUCCESSFULLY!‟, then you are good to go.
10
6. Connect To SQL-Server Database
Below are directions for connecting to the SQL Database using a variety of applications.
NOTE: The following screen shots may look different depending on your installed
version of SSME and the database you are connecting to.
2. Run SQL Server Management Studio Express and „Connect to the Server‟:
11
3. Select the appropriate database from the set of databases:
6.2. Python
There are a number of libraries that can be used to connect to SQL-Server. We have
found the pyodbc works best and is well documented at
(http://code.google.com/p/pyodbc/):
12
2. Run the following example python script. Note that you will need to change the
following (highlighted in yellow):
########################################################################
##Created by Brent Frakes 6/2/2011
##This demo script provides an example of how to connect to a SQL-Server Database
######################################################
#import pyodbc library
Import pyodbc
#Establish connection to SQL-Server 2008 Database using DSN and Windows Authentication
cnxn = pyodbc.connect(';DSN = ClimateDB;Trusted_Connection =yes')
#Create a cursor which manages attributes of data returned from the SQL-Server data source
cursor = cnxn.cursor()
#########################################################################
13
6.3. MS Access
14
4. Select the Machine Data Source tab and find the appropriate data source. Hit OK.
15
5. Select the Appropriate Objects
While MS Access works best with tables and views, it is still possible to execute Table-
and Scalar-Valued Functions from Access by creating a pass-through query. To create
a pass-through query, follow these steps:
3. Select Pass-Through from the Query Type section on the Design tab.
16
4. Type in your T-SQL statement and click the Run button on the Results section on
the Design tab.
5. When prompted on the Select Data Source window, select the Machine Data
Source tab and find the data source. Hit „OK‟.
6. On the SQL Server Login window, check the Use Trusted Connection checkbox
and click „OK‟.
The statement should execute and return results. You can save the query in
Access.
17
6.4. R
Below is example of how to connect using R where the ODBC data source is called
NPS_Climate:
6.5. MS Excel
18
2. Select the appropriate Data Source (Example here is for ReferenceDB). Make
sure the “Use Query Wizard to Create/Edit Queries” is checked.
3. Select the appropriate tables or views. If you don‟t see them, select options and
specify the appropriate types of information to show.
19
4. Select Data to Include in Query
20
6. Optionally Specify Sort Order
7. Run the query by selecting “Return Data to Microsoft Excel” or get more
advanced query options by selecting “View data or edit query in Microsoft Query”
21
6.6. ArcGIS 10
Process
1. Start ArcCatalog
2. Click „Add OLE DB Connection‟ -> Data Link Properties -> Provider tab->SQL
Server Native Client 10.0->Click Next.
3. Fill out the Database Connection Properties dialog box as shown below:
22
Provide a name for the database connection. Type any name you wish, then press
enter.
4. Open ArcMap
23