Archive for the ‘SQL Server’ Category

Configuring Oracle SQL Developer for Microsoft SQL Server and MySQL

Configuring Oracle SQL Developer for Microsoft SQL Server and MySQL

Most of DBAs I know, use TOAD for doing daily tasks, but I prefer Oracle SQL Developer. In my opinion, it has 3 important advantages against TOAD:

  • It’s platform-independent: Although I use Windows on my laptop now, I’m a big fan of Linux and Solaris, and I don’t like being dependent on a specific OS. Thanks to Java, Oracle SQL Developer is platform-independent.
  • It supports multiple databases: You can use SQL Developer for Microsoft Access, Microsoft SQL Server, MySQL, TimesTen, DB2 and (of course) Oracle.
  • It’s extendible: Yes I know that there are not too much documents about it, but it’s extensible. For example, check FourthElephant’s extensions for SQL Developer: http://www.fourthelephant.com/sqldeveloper/download/

TOAD has a better interface because of using native Windows components but you can get used to SQL Developer if you spend time on it. Anyway, this blog has nothing to do with comparing SQL Developer and TOAD. I’ll just show how to configure SQL Developer to connect Microsoft SQL Server and MySQL.

You need to download and install the required (and supported) JDBC drivers to make Oracle SQL Developer connect MySQL and Microsoft SQL Server.

For MySQL download the J/connecter from http://www.mysql.com/products/connector/

For Microsoft SQL Server download the jTDS from http://sourceforge.net/projects/jtds/. The JDBC driver which is published by Microsoft doesn’t work with Oracle SQL Developer, at least I couldn’t make it work.

After you download the JDBC drivers, unzip them and then open prefences window in SQL Developer ( tools >> preferences ):

To add JDBC driver for Microsoft SQL Server, click “add entry…” button and find “jtds-1.2.5.jar” in the file open dialog.

To add JDBC driver for MySQL, click “add entry…” button and find “mysql-connector-java-5.1.17-bin.jar” in the file open dialog.

You need to restart SQL Developer to make these new JDBC drivers active. Then you can create a connections for Microsoft SQL Server, Sybase (because JDTS supports Sybase) and MySQL:

that’s it.

Advertisements

Use Oracle’s FREE SQL Developer as a Microsoft SQL Server GUI on OSX

Use Oracle’s FREE SQL Developer as a Microsoft SQL Server GUI on OSX

I’m on a Mac, my company uses a SQL Server, and since there is not native SQL Server Enterprise manager client I was forced to find a workaround. One common solution is to have aVirtual Machine running windows but to me that approach feels like using a sledgehammer to pound in a finishing nail.  Thanks to this post on stackoverflow I am able to use Oracle’s FREE SQL Developer application to connect to Microsoft SQL Server Instances on my MacBook Pro running Lion.  I’ve used SQL Developer for Oracle development for a number of years now so the learning curve was pretty small for me but anybody who is familiar with SQL Server Enterprise Manager should be able to find their way around pretty easily, however there are a few things to be aware of which I’ll discuss a bit later.

Installation

Before you can install SQL Developer you’ll need to make sure that you have the JDK installed on your machine.  I have the Java for Mac Developer Preview installed on my machine from: https://developer.apple.com/downloads/.

Once the JDK is installed (you can check by opening a terminal and typing  javac -version ) the next step is to download and install SQL Developer from Oracle’s website here.  Once SQL Developer is installed you’ll need to get the jDTS plugin files from: http://sourceforge.net/projects/jtds/files/, at the time of this writing the most current version is 1.2.5.  Once the zip is download and extracted move the entire folder someplace where your account will have execute permissions on the jdsts-1.2.3.jar file.  I have a lib directory in my User folder where I keep all my third party .jar files.

You now have everything you need to get SQL Developer executing SQL queries, you just need to do some simple setup in SQL Developer.  Open SQL Developer and as of version 3.0 go to Tools -> Preferences  and expand the “Database” node.  Next click on Third Party JDBC Drivers click on “Add Entry…”.  From this menu browse to the /jdts-1.2.5-dist folder you copied in the step above and select jdts-1.2.5.jar (or whichever version corresponds to the version you downloaded) and click “OK”.

Configuration

SQL Server Connection Tab

Once you have the plugin installed exit out to the main SQL Developer IDE and add a new connection by either: clicking on File -> New -> Database Connection or by clicking on the green + icon in the “Connections” tab.   You should now see a “SQLServer” tab where you can enter the connection information for your SQL Server.

Using SQL Developer to Query Microsoft SQL Server Databases

Select Default Database
If you have not used SQL Developer before there are a few things to be aware of.  Since it is primarily an Oracle IDE you have to think in a somewhat “Oracle” way when running queries on your database. Since Oracle is a schema-centric database server you’ll save yourself lots of typing if you set a default database for your current connection.   If you don’t you’ll have to type the fully qualified table name for each table you reference in a query (e.g. database.owner.tablename).  To do this right click on the database you’d like to use and click on “Select Default Database” in the context menu.  Once you do this you can do normal SELECT x FROM Table queries and leave out the fully qualified table name.  You’ll have to do this each time you connect to SQL Server.
SQL Developer will allow you to do many of the basic database tasks you can do in Enterprise Manager, however there are quite a few limitations. You can view your Table/View structure and data, you can also view most of the other objects in SQL Server like Stored Procedures and Functions.   SQL Developer will also allow you write DML (Data Manipulation Language) statements like Update, Delete, and Insert.   You can also write some DDL (Data Design Language) statements like ALTER TABLE.  However, the one huge shortcoming of SQL Developer is its inability to recognize the “BEGIN” keyword.  This means that any statement that uses BEGIN, like transactions, stored procedures, and functions will all generate a syntax error and not execute.One nice “benefit” of using SQL Developer is the “Format” context menu.  When I’m writing code I often get SQL statements in debugging code, however, its usually one long string, debugging this code in Enterprise Manager usually meant having to manually update format the SQL, however in SQL Developer I can paste the code into the SQL Worksheet, right click, and click on “Format” and have a nicely formatted SQL Statement.

Conclusion

While its not a complete replacement for Enterprise Manager I do find that SQL Developer will allow me to do many of the things I spend 90% of my time doing to my databases.  The best part is that this is a completely Free and somewhat mature solution.

Making Database Connections to Remote SQL Server 2008 using Oracle SQL Developer

Making Database Connections

Connect to Oracle and third-party databases from Oracle SQL Developer.

Oracle SQL Developer enables developers and DBAs to browse, create, and update data in a database. Before you can perform those actions, however, you must create at least one database connection —an Oracle SQL Developer object containing the information needed to connect to a specific database as a specific user.

This column explains how to make connections from Oracle SQL Developer to an Oracle Database and third-party databases. It also discusses authorization options for Oracle Database connections and explains the role of JDBC drivers in the connection process.

All the examples in this column require Oracle SQL Developer to be running on your local machine. For some examples, you need access to a running local or remote Oracle Database instance with the sample HR schema (available in the default database installation). Other examples require access to an Oracle Internet Directory server or a third-party database.

Basic Connections

You can connect to a local or remote Oracle Database instance by using the Basic connection type. Basic connections do not require any other Oracle software to be installed on your machine—you don’t need an Oracle home.

Right-click the Connections node in Oracle SQL Developer’s Connections Navigator, and click New Connection to open the New / Select Database Connection dialog box. All of this column’s connection examples start from this dialog box.

To create a basic connection for the HR schema, follow these steps:

1. Enter HR_ORCL for Connection Name . The connection name is an arbitrary alias; conventionally, it’s a combined username and database name.

2. Enter hr for Username , and enter the hr password in the Password field. (If you check the Save Password box, the password will be stored as an encrypted file on your local machine.)

3. Select Basic from the Connection Type list.

4. Provide information for the following settings:

  • Role: This is the set of privileges to be associated with the connection. Accept default for this connection.
  • OS Authentication: Leave this unchecked for this connection.
  • Proxy Connection: Leave this unchecked for this connection.
  • Hostname: This is the host system for the Oracle Database instance. Enter an IP address, a machine name, or localhost (when connecting to a database on the same machine as Oracle SQL Developer). The default is localhost .
  • Port: This is the listener port for the database. The default port for Oracle Database is 1521.
  • SID: This is the system identifier, such as orcl (the default for Oracle Database 10g and Oracle Database 11g) or xe (the default for Oracle Database 10g Express Edition).
  • Service name: This is the network service name of the database. Select either SID or Service name.

5. Click Test to validate the new connection. Figure 1 shows the dialog box after the connection has validated successfully.

6. If your test reports “Status: Success,” click Connect . Oracle SQL Developer will save the new connection, close the dialog box, and connect to the database. (If you click Save instead of Connect , you will be able to create more new connections in the dialog box before connecting.)

Now HR_ORCL appears in Connections Navigator, and you can expand it to browse the database.

TNS Connections

The TNS connection type is an appropriate option in any of the following circumstances:

You have an Oracle client installed on your machine. You have access to many Oracle Database instances. You do not know the machine details of the system hosting the Oracle Database instance you want to connect to.
A TNS connection uses an alias entry from a tnsnames.ora file. Oracle SQL Developer uses only one tnsnames.ora file. You may have more than one on your local machine or want to use the tnsnames.ora file on a remote machine, so note that Oracle SQL Developer looks sequentially for the tnsnames.ora file in the following locations:

1. $HOME/.tnsnames.ora
2. $TNS_ADMIN/tnsnames.ora
3. /etc/tnsnames.ora (non-Windows systems)
4. $ORACLE_HOME/network/admin/tnsnames.ora
5. Registry key

On Windows systems, if a tnsnames .ora file exists but Oracle SQL Developer isn’t using it, create a TNS_ADMIN environment variable via Control Panel -> System -> Advanced -> Environment Variables , specifying the file’s location as the variable’s value.

Follow these steps to create a TNS connection in Oracle SQL Developer:

1. In the New / Select Database Connection dialog box, enter the same connection name, username, and password you used for the basic connection.

2. Select TNS from the Connection Type list. The GUI changes slightly to provide a list of all network alias entries available to you. Select an alias.

3. Click Test and Connect as before.

LDAP Connections

Oracle Internet Directory is a directory service that enables you to store and manage network service names centrally. With it, user identity information can be stored in a directory instead of in multiple databases. Oracle Internet Directory is an implementation of the LDAP directory service and a component of Oracle Identity Management. For information on how to install, set up, and configure Oracle Internet Directory, see the Oracle Internet Directory Administrator’s Guide.

To look up database services in Oracle Internet Directory and create an LDAP connection in Oracle SQL Developer, follow these steps:

1. In the New / Select Database Connection dialog box, enter a new connection name, username, and password for the database user.

2. Select LDAP from the Connection Type list.

3. Select a server from the LDAP Server list, which is populated with entries from an ldap.ora file (similar to the tnsnames.ora file). Alternatively, you can enter LDAP server details directly.

4. Fill in the LDAP Admin User and LDAP Admin Password fields.

5. Click Load to populate the DB Service list with all the database service entries from Oracle Internet Directory.

6. Select a service from the DB Service list, as shown in Figure 2.

7. Click Test and Connect as before.

Other Authentication Options

Alternatives to database authentication and LDAP authentication include operating system (OS) and proxy authentication. You can create connections in Oracle SQL Developer for users who authenticate with these mechanisms. OS authentication. With OS authentication, Oracle Database uses a database user’s OS login credentials to authenticate that user. The user doesn’t provide a username or password to access the database, and Oracle Database doesn’t store and manage the account password. Local OS authentication can be used when the client and the database server are on the same machine. Remote OS authentication is possible but is not considered secure.

To configure local OS authentication for a new user, first find the value of the OS_AUTHENT_PREFIX database initialization parameter in your system’s init.ora file. When you create this new user in the database, you must add this parameter value as a prefix to the OS username. The default value is OPS$, for backward compatibility with earlier database releases. (If the value is “”, the OS username and the database username are the same, so you don’t need to add a prefix to create the Oracle usernames.)

Establish a basic connection with the HR schema as the SYSTEM user. Execute the following from the SQL worksheet, using your database’s OS_AUTHENT_PREFIX prefix and substituting your own OS username for “sue”:

CREATE USER ops$sue IDENTIFIED EXTERNALLY;
GRANT Connect, resource to sue;

Now create a basic connection for this user from the New / Select Database Connection dialog box. Enter a connection name; select Basic for Connection Type ; fill in the Hostname and Port fields; select OS Authentication ; and provide a SID or Service name . Click Test and Connect as before.

Proxy authentication. Proxy authentication means that one JDBC connection acts as a proxy for other JDBC connections. Before you can create a connection that uses proxy authentication, you need a proxy user. In the following example, you create a new user named HR_PROXY_USER and connect that user through the existing HR user.

To create the new proxy user (HR_PROXY_USER) and grant the correct proxy authentication privileges, execute the following in the SQL worksheet:

CREATE USER HR_proxy_user IDENTIFIED BY <
                               password>;
ALTER USER HR_proxy_user GRANT CONNECT THROUGH HR AUTHENTICATED USING PASSWORD;

You also need to grant any other privileges required by the new user, such as CREATE SESSION.

Once your proxy user exists, you can create a new proxy connection for HR in Oracle SQL Developer. Select Proxy Connection in the New / Select Database Connection dialog box, and complete the details in the Oracle Proxy Connection dialog box, as shown in Figure 3.

Connecting to Third-Party Databases

Oracle SQL Developer supports browsing and reviewing data and objects in Microsoft Access, MySQL, Microsoft SQL Server, and Sybase. It also offers a SQL worksheet for ANSI SQL commands to update or create objects for these databases. Users who want to migrate from third-party databases to Oracle Database can use the Oracle SQL Developer Migration Workbench. For any of these situations, you need to create a connection to your third-party database.

Installing third-party drivers. The correct third-party driver must be installed before you can create the connection. You can install third-party drivers either manually or by using Check for Updates ( Help->Check for Updates ). For manual installation, you can download supported drivers from the following locations:

  • MySQL JDBC driver, version 5.08: dev.mysql.com/downloads/connector/j/5.0.html.
  • jTDS driver, version 1.2 (required by Microsoft SQL Server and Sybase): source forge.net/project/showfiles.php?group_id=33291. (See jtds.sourceforge.net for information about this driver.)

Microsoft Access does not require an additional driver, because it uses a JDBC/ODBC bridge.

After downloading the driver you need, expand the driver binary Java Archive (JAR) file, which is typically inside the downloaded archive file:

  • The mysql-connector-java-5.0.8.tar.gz (or .zip) download for MySQL includes mysql-connector-java-5.0.8-bin.jar.
  • The jtds-1.2-dist.zip file for Microsoft SQL Server and Sybase includes jtds-1.2.jar.

Select Tools -> Preferences -> Database -> Third Party JDBC Drivers . Click Add Entry , and add your specific JAR file. Now you can create a connection for your third-party database.

Creating a Microsoft Access connection. To create a connection for Microsoft Access, follow these steps in the New / Select Database Connection dialog box:

1. Enter a connection name.
2. Click the Access tab.
3. Click Browse to locate the .mdb file you want to work with.
4. Click Connect.

You can now expand and browse the new Microsoft Access connection in the Connections Navigator.

Creating a Microsoft SQL Server, Sybase, or MySQL connection. To create a connection to a Microsoft SQL Server, Sybase, or MySQL database in the New / Select Database Connection dialog box, follow these steps:

1. Enter a connection name, username, and password.
2. Click the SQL Server, Sybase , or MySQL tab. Figure 4 shows the SQL Server tab.

figure 1
Figure 1: Basic database connection type
figure 2
Figure 2: LDAP connection with a list of database services
figure 3
Figure 3: Creating a proxy connection
figure 43
Figure 4: Connecting to Microsoft SQL Server

3. Choose one of the password authentication options (each of these connections offers you multiple choices).
4. Fill in the Hostname and Port fields. The default port is 1433 for Microsoft SQL Server, 5000 for Sybase, and 3306 for MySQL.
5. Click Choose database to populate the database list, and select the appropriate database from the list.
6. Click Test and Connect as before.
You can now browse your Microsoft SQL Server, Sybase, or MySQL database in the Connections Navigator.

Conclusion

Oracle SQL Developer provides an array of alternatives for connecting to Oracle and third-party databases. Support for both Oracle JDBC Type IV (thin) and Type II (thick) drivers lets you access Oracle Databases via basic, TNS, or LDAP connection types. You can make connections for Oracle Database users who authenticate via database, OS, or proxy authentication. Connecting to a third-party database from Oracle SQL Developer lets you work in that database and gives you a starting point for migrating to Oracle Database.

Using Oracle SQLDeveloper to access SQLServer

Using Oracle SQLDeveloper to access SQLServer

It is a pretty cool feature to use Oracle’s SQLDeveper 1.1 to access SQLServer.
The steps are:

  • Download jTDS (open-source SQLServer JDBC driver) from here. Unzip and extract the jtds-1.2.jar or whatever the latest version.
  • Start Oracle’s SQLDeveloper, Tools->Preferences->Database->Third Party JDBC Drivers. Click “Add Entry” and point to the jtds-1.2.jar
  • Create a new connection, choose SQLServer tab, type in hostname, port, username and password. It appears that the initial connection name has to be the same as the database and you can click the “Retrieve database” button. Once you found the database, you can rename the connection.

Try it out.
Of course, certain things don’t work. Like explain plan and auto trace.

Per comments below, please make sure jtds 1.2 is used. Apparently, 1.3 does not work.

Deploying a SQL Database to a Remote Hosting Environment (Part 1)

Solution: Deploying a SQL Database to a Remote Hosting Environment

Scenario:

You finish building a great ASP.NET application, have everything tested and working right on your local system, are taking full advantage of the new ASP.NET 2.0 Membership, Role and Profile features, and are ready to publish it to a remote hosting environment and share it with the world.

Copying the .aspx files and compiled assemblies to the remote system is pretty easy (just FTP or copy them up).  The challenge that confronts a lot of developers, though, is how to setup and recreate the database contents – both schema and data – on the remote hosted site.  Unfortunately there hasn’t historically been a super-easy way to accomplish this.

The good news is that the SQL Server team published the release candidate of a new SQL Server Hosting Toolkit that will make it much, much easier to deploy your SQL solutions remotely to a hosted environment.  The toolkit allows you to work with SQL Express, SQL Server 2000, and SQL Server 2005 databases locally, and then easily transfer your schema and data and install them into a shared hosting remote SQL Server account.

The below post describes how you can start using this today.

SQL Server Hosting Toolkit

The SQL Server Hosting toolkit is available for free, and ships with a Database Publishing Wizard that supports two database hosting deployment scenarios:

1) The Database Publishing Wizard enables you to point at a database you are working with on your local system, and then automatically create a .SQL script file that contains the setup logic needed to re-create an exact replica of the database on any remote system.  This .SQL script includes everything needed to create the database schema (tables, views, sprocs, triggers, full-text catalogs, roles, rules, etc – full details here), as well as populate the new database with the same table row contents as your local database (this is analogous to the MySQL dump utility).  The benefit of having this setup logic encapsulated in a single .SQL file is that most hosters already support the ability for you to upload .SQL files to their hosted environments and run these scripts via their hosting admin control panels.  Assuming you have a web hoster that supports this today, you can immediately start using the Database Publishing Wizard to easily deploy your sites without requiring anything to be installed or configured by the hoster.

2) The Database Publishing Wizard also enables you to point at a database you are working with on your local system, and then use web-services to transfer and recreate the database in your remote hoster environment (without you having to create the .SQL file or use the hoster admin control panel to run it).  This publishing option does require that a SQL Publishing web-service be exposed in the hosting environment, and the SQL Server Hosting Toolkit includes a free implementation of this SQL Publishing web-service that we’ll be working with hosters to aggressively deploy.

The Database Publishing Wizard enables you to use either SQL Express or SQL Server 2000/2005 locally, and then use either SQL 2000 or SQL 2005 in the remote hoster environment.  It does not require that the versions of SQL match – so you can use SQL Express 2005 locally and then upload to a SQL 2000 server in the hosting environment without having to change any of your code.

The Database Publishing Wizard also supports handling the built-in ASP.NET 2.0 Membership, Role Management, Profile and Health Monitoring schemas.  A lot of people have run into issues because the built-in .SQL scripts that ship by default with ASP.NET for setting up these schemas require DBO permissions at install-time for the SQL scripts — which a lot of hosters don’t support (note: the scripts do not require DBO permissions at runtime – only for install time, but this can sometimes still be a blocker in itself unless the hoster is willing to install them for you).  The Database Publishing Wizard on the other-hand does not require DBO permissions when installing the ASP.NET Membership, Roles and Profile schemas/data, and should enable you to deploy the ASPNETDB tables + sprocs just as easily as any other database using the Database Publishing Wizard.

First Tutorial: Deploying a SQL Express Database to a SQL Server Hosting Account (using .SQL files)

I’ll be doing a series of posts over the next few weeks showing how to use the various features within the SQL Server Hosting Toolkit.  This first tutorial in the series covers how to use it to easily generate a .SQL installation file of a local SQL Express database that you can then copy to a remote hosting account and use to re-create a SQL Server database for you to use with your hosted site.

Step 0: Download and Install the Database Publishing Wizard

The first step we’ll need to-do is to make sure we have the Database Publishing Wizard from the SQL Hosting Toolkit installed.  Click here to download it and install it.

The Database Publishing Wizard comes with support for both a GUI based wizard, as well as a command-line utility.  The GUI based wizard can be run either standalone or via context-menu support that it adds to the Server Explorer in both Visual Studio 2005 and Visual Web Developer Express.  For the purposes of this tutorial we’ll be using this later Server Explorer integration – which makes publishing really easy.

Step 1: Create an ASP.NET web-site that uses a local SQL Express or SQL Server database

To help with this demo, we will use the built-in Personal Starter Kit template that ships with both VS 2005 and Visual Web Developer Express.  To create a new web project based on it, select File->New Web Site within VWD or VS and choose the “Personal Starter Kit” template in the New Web-Site dialog.  By default the personal starter kit application is configured to use SQL Express (which is free and can be downloaded here).  When run the sample looks like below:

After creating the application, you can then run the web admin tool (choose the WebSite->ASP.NET Configuration menu item in VWD/VS) and create a new user and add them to the “admin” role for the site.  You can then login as this new admin user and try uploading new pictures and/or customizing the existing ones on the site (note that both the picture meta-data, as well as the raw image binaries are stored in a database when you do this):

Once you are all done with the above steps we’ll have two SQL Express databases installed within the \app_data directory for our project.  One of the SQL Express databases is named personal.mdf and contains the tables and stored procedures specific to our web-site (photo and album tables, as well as basic content management support).  The other SQL Express database is named aspnetdb.mdf and contains the database storage for the default ASP.NET 2.0 Membership, Role and Profile providers (which the application above is using for login and admin purposes).

Step 2: Creating .SQL Installation Scripts for our Database

Now that we’ve created a new application + local database, and added custom data to it (new users + their role membership, as well as new photos and albums), we want to deploy the application to a remote hosting server.

The first step we’ll take is to create .SQL script files that will enable us to automate re-creating the exact same database schema + database content on our remote hosting account.  To-do this we’ll use the Database Publishing Wizard we installed as part of the SQL Hosting Toolkit.

To begin, click on the “Server Explorer” tab within Visual Studio or Visual Web Developer to see the databases that the application is using:

As you can see in the above picture, we have two SQL Express databases that we are using: ASPNETDB.MDF and Personal.MDF.  To generate .SQL installation files for each one, simply select the database in the solution explorer, then right-click and select the new “Publish to Provider” context menu item (added by the Database Publishing Wizard) setup on it:

This will launch the Database Publishing Wizard and allow us to walkthrough scripting the installation of our database.  As I mentioned in the intro of this blog post, the Database Publishing Wizard supports two deployment options: 1) To generate .SQL install script files that you can copy to your remote hoster and run using their existing web admin control panel tools, or 2) To upload the database directly using Database Publishing Web-Services on the hoster web-site.

For this first tutorial, we’ll be using the .SQL script file approach – so keep the default radio button selected and provide a name for the .SQL install script file you want to generate:

When you click “next” you’ll be given the option to customize some of preferences when creating the .SQL setup file.  Note that you can control whether to drop existing objects within the target database, whether you want to target SQL 2000 or SQL 2005 with the script, and whether you want to setup both the schema and data, or just the schema, or just the data:

For this tutorial just keep the defaults selected, and hit next and generate the .SQL script:

You now have a Personal .SQL file that contains a script that you can run on any SQL server to re-create all the tables, sprocs, views, triggers, full-text catalogs, etc. for a database, as well as import and add all of the table row data that was in the database at the time the .SQL file was created.

The .SQL file itself is just plain text – so you can open it up with any text editor to see it and/or customize it with your own statements:

Notice above how the .SQL file includes both the SQL DDL needed to create the Photos table (including all of its constraints and primary-key/foreign-key relationships), as well as the SQL to insert data within the table once it is created (in the case above it is even inserting binary data for the photos – since they are stored in the database).

Once you repeat these steps for the ASPNETDB SQL Express database as well you’ll have two .SQL installation scripts that you can use to automatically re-create your SQL database on any SQL Server:

Note that the .SQL files we built can be used to create two separate databases on a server, or they can both be run against the same database to create a single database that has a unified set of tables, sprocs, and data for the application.  To accomplish this, simply run both scripts against the same database, and assuming no table or sproc names conflict, you’ll have a single database containing everything.  This later option is very useful when you have a hosting account that only provides 1 database instance for you to use!

Step 3: Using our .SQL files to create our remote databases

Now that we have our .SQL files, we can go about using them to install our database at our hoster.  Exactly how we use the .SQL files to install the database will vary depending on how the hoster gives us access to our SQL account.  Some hosters provide an HTML based file-upload tool that allows you to provide a .SQL file – which they will then execute against the SQL database you own.

Other hosters provide an online query tool (like below) that allows you to copy/paste SQL statements to run against your database.  If you have a hoster which provides an online query tool like this, then you can open the .SQL file with a text-editor and copy/paste the contents into the query textbox and run it.

The quality of the SQL tools that different hosters provide varies quite a bit.  In testing the Database Publishing Wizard we found that some custom-made SQL admin tools provided by hosters had issues where they incorrectly parsed valid SQL statements (in particular GOTO statements).  This page describes one issue you might see some hosters have with GOTO statements, along with a workaround you can use.  To help improve the overall quality of SQL hosting admin tools, the SQL Server team early next year is going to be shipping the source to a free SQL HTML admin tool that hosters will be able to integrate into their experiences.  Hopefully this will help improve the standard experience with all Windows hosters.

If your hoster has no usable HTML web admin tool for allowing you to easily manage your SQL database, then you can also just write a simple ASP.NET page that you FTP (along with your .SQL file) to your web-site and then hit to read the .SQL file on the server in as text, and then pass it as a string to ADO.NET to execute.  This will give you the same result as the query analyzer above – and fully create your database for you.

Step 4: Updating our connection-string within web.config

Once we’ve got our data uploaded within a database at our hoster, we’ll want to upload our .aspx files, assemblies and content to the remote site (typically this is done over FTP).

The last step we’ll need to take is to open up our web.config file and update the <connectionStrings> section to point at our new database location at the remote hoster.  Note that you’ll need to get the exact SQL server, database name, and username/password account to use from the hoster.

Using our personal starter kit example above, we’d change the <connectionStrings> section within its web.config file from the default connection-string (which uses two SQL Express database in the local \app_data directory):

<connectionStrings>

 <add name=”Personal” connectionString=”Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Personal.mdf” />
<
remove name=”LocalSqlServer”/>
<
add name=”LocalSqlServer” connectionString=”Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf” />
</
connectionStrings>

To instead use a single SQL Server 2000 database (the “scottguDB” database on the “Server123” box).

<connectionStrings>
<add name=”Personal” connectionString=”Data Source=Server123;Initial Catalog=scottguDB;Integrated Security=True” providerName=”System.Data.SqlClient” />
<
remove name=”LocalSqlServer”/>
<
add name=”LocalSqlServer” connectionString=”Data Source=Server123;Initial Catalog=scottguDB;Integrated Security=True” providerName=”System.Data.SqlClient” />
</
connectionStrings>

We were able to use a single database (instead of the two above) because we we ran both .SQL files against the single database – which merged all schema and data into a single database instance.

Step 5: We are done

Now we can run the application remotely in a hosted environment, and it should just work.

Summary

The Database Publishing Wizard that ships as part of the SQL Hosting Toolkit should make creating .SQL files for any database (SQL Express or full SQL Server) really easy.  You can use this to easily dump your local database and then use it to re-create the exact same database on a remote system.

In future tutorials I’ll also show how you can actually re-create your database remotely without even having to generate a .SQL file (instead you can publish the database directly from VS to your hoster over a web-service).  Stay tuned for details on how to-do this soon.

Hope this helps,

%d bloggers like this: