一个小把戏:如何上传一个.sql文件到一个主机并执行这个文件来配置数据库

Tip/Trick: How to upload a .SQL file to a Hoster and Execute it to Deploy a SQL Database
一个小把戏:如何上传一个.sql文件到一个主机并执行这个文件来配置数据库

Last month I posted about the new (free) Database Publishing Wizard that is designed to make it much, much easier to upload and deploy SQL Express and SQL Server databases in a web hoster environment.
上个月,我发布了一片数据库发布向导的文章,里面阐述了如何在网络环境中简单的上传和配置SQL ExpressSQL Server 数据库。

In my first Database Publishing post, I walked through how you can use the Database Publishing Wizard to automatically generate a .SQL installation file that contains the script necessary to recreate your database schema (tables, views, sprocs, triggers, full-text catalogs, etc) and also populate your database with the same table row contents as your original database.  This provides a super easy way to put together a .SQL script that entirely automates replicating your database on another server:
在我的第一篇数据库发布向导中,我简单的讲解了如何使用你的数据库发布想到来生成一个包含您的数据库的所有框架(表,视图,存储过程,触发器,full-text分类,等等的一切,甚至包括您愿数据库的纪录)的.SQL文件。在这里我将会提供一个更简单的方法来使用这些.SQL文件来重新部署您的数据库到另一个服务器上。

In my previous post I mentioned how hosters often provide an online SQL management tool that you can then use to open and run your newly generate .SQL file, or provide a SQL query tool that allows you to copy/paste the .SQL file's contents into a query execution window to run.  Unfortunately, though, not all hosters support tools like this.  And even with hosters that do support it you might run into cases where your generated .SQL file is so big that copying/pasting it into a text box isn't really viable (doing a form post of 200MB of content will typically time out!). 
在我的前一篇文章中,我提到了主机通常会提供一些在线的sql管理工具,这些工具可以用来打开或执行你最近产生的.SQL文件,或者提供一个SQL查询工具来允许你复制/粘贴.SQL文件的内容到一个执行查询的窗口并运行。不幸的是,且不说是否所有的服务器都会提供这些工具,即使是服务器真的支持这类操作也只是简单的让你吧生成的.sql文件的内容复制出来粘贴到页面上然后执行。这样的话当产生的.SQL脚本文件足够的打,那么复制/粘贴到文本框里就不太可能了(在formpost200MB的东西将会超时)

This blog post walks through an alternative way to deploy your .SQL files in a remote hosting environment, and which does not require your hoster to configure or install anything on the backend.
这篇博客通过另一个可选的方法来配置您的.SQL数据库文件到远程主机环境,并且这些不需要您的主机来安装或配置任何的东西。

Steps to Deploy a SQL Database to a Remote SQL Server without using an Admin Tool
使用管理工具来配置远程SQL服务器上的SQL数据库的步骤。

Below are the steps to take to deploy a local database to a remote hoster without requiring or using any admin tool:
下面是配置一个本地数据库到远程服务器上的步骤(不需要任何的管理工具)

Step 1: Generate a .SQL File Containing your Schema and Data
第一步:生成一个包括你的数据库的结构和数据的.SQL文件

The first step to remotely deploy your database in a hosted environment is to generate a .SQL file that dumps the schema and content of your database.  Follow the steps in my first Database Publishing blog post to learn exactly how to generate a .SQL file for either a SQL Express or SQL Server database.
远程配置您的数据库到服务器环境中的第一步是产生一个包含了数据库的架构和内容的.SQL文件。按照我的另一篇博客 My first Database Publishing blog post 中的步骤你将会清晰的知道如何从SQL ExpressSQL Server 数据库中产生.sql文件

Step 2: FTP up the .SQL File to your Remote Hoster
第二步:用FTP上传.SQL文件到您的远程服务器。

Once you've generated your .SQL file, upload it to your remote hoster using FTP or some other file transfer mechanism.  Ideally you should copy this into a protected location where remote users can't easily get access to it (one suggestion: give it a random name and upload it into the /app_data folder which is typically protected by default).
当您产生了您的.SQL文件后使用FTP或其他的方式上传到您的远程服务器,建议您拷贝到一个比较安全的,不是所有用户都可以访问的位置(权限的问题:译者注)(一个比较不错的建议是:随机的生成文件名,并把它保存打/app_data文件夹下面这样的话默认就是受保护的。)

The benefit of uploading this file over FTP is that it won't force you to limit the size of the .SQL file.  It can be 100s of megabytes in size if necessary.
通过FTP上传的好处是这样 不会限制上产文件的大小。,如果需要可以你可以上传100MB

Step 3: Download the RunSQL.aspx Utility Page
第三步:下载RunSQL.aspx功能页面

Visit this page and download the RunSQL.aspx file linked on it. 

The RunSQL.aspx file is an ASP.NET page that the SQL Server team put together that supports two arguments: 1) The name of a .SQL file, and 2) The connection string of a database.  When run the RunSQL.aspx page will open the .SQL file and iterate over each of its statements and execute them against the database (indicated via the connection string).  This will provision the database defined within the .SQL file to the remote database target.
访问该页面下载RunSQL.aspx文件。

RunSQL.aspx
文件是一个ASP.NET页面,该页面支持两个参数1.SQL文件的名字2)数据库的连接字符串。当运行RunSQL.aspx页面的时候,将会打开.SQL文件并逐条的在连接字符串指定的数据库上逐条的执行SQL语句。This will provision the database defined within the .SQL file to the remote database target.

Step 4: Edit the RunSQL.aspx Utility Page
第四步:编辑RunSQL.aspx页面。

Open and locally edit the RunSQL.aspx file and and configure the name of your .SQL file, as well as provide the connection-string your hoster gave you for the SQL database:
在本地编辑RunSQL.aspx文件,并修改稿你的.SQL文件和相应的连接字符串。

Replace the <<YOUR_SCRIPTFILE>> marker and associated connection-string markers with the correct values for your hosted configuration.  Note that unless you know the fully qualified path of the .SQL file, you'll probably want to use ASP.NET's Server.MapPath(fileName) method to calculate the absolute path of the relative .SQL file path in your web application.  For example:

替换<<YOUR_SCRIPTFILE>>标记,修改connection-string标记为正确的值。注意,除非你却型的知道你的SQL 文件的绝对,否则的话您可能要像下面那样使用ASP.NETServer.MapPath(文件名)地方法来计算相应的.SQL文件的路径,例如

    // Filename of the T-SQL file you want to run
    
string fileName Server.MapPath("personal.SQL");    
    
    
// Connection string to the server you want to execute against
    
string connectionString @"Server=server123;User ID=user123;Password=password123;Initial Catalog=MyDBName123";  
    
    
// Timeout of batches (in seconds)
    
int timeout 600;

 

Step 5: Upload the RunSQL.aspx Utility Page to Your Remote Hoster
第五步:上传RunSQL.aspx实用页面到您的远程服务器

Once you have finished updating the fileName and connectionString values above, upload the RunSQL.aspx file to your remote hoster (for example: using FTP). 
当你配置完你的fileNameconnectionString的值后,上传RunSQL.aspx文件到您的远程主机。(例如:实用FTP

For security reasons I recommend giving the file a random file-name when you upload it so that other users can't easily find and run it.
出于安全考虑,我建议你上传的时候给你的文件(RunSQL.aspx)一个随机的名字,这样的话普通的用户将会不那么容易找到或运行它。

Step 6: Hit the RunSQL.aspx Utility Page with a Browser
第六步:在浏览器里运行RunSQL.aspx功能页

Once uploaded, hit the remote RunSQL.aspx page via your browser.  This will cause the page on your remote server to parse the .SQL file, and execute all of its statements.  Since the .SQL file contains all of the database schema creation and population statements necessary to recreate your database, once the page finishes running you'll have an identical database deployed at your hoster:
上传到服务器后,在你的浏览器里远程查看RunSQL.aspx页面,这将会运行该页面的后台代码,从而解释.SQL文件,并执行其中的操作。由于.SQL文件包含了所有的数据库架构和数据内容的创建语句,因此,当该页面运行后,您将会在您的服务器上得到一个和原来的一模一样的数据库。

Step 7: Delete the RunSQL.aspx and .SQL Files
第七步:删除RunSQL.aspx.SQL文件。

Once you've finished running your .SQL script, delete both the RunSQL.aspx page and the .SQL file from your remote hoster server. 
当您运行完您的SQL脚本后,记得从远程服务器上删除RunSQL.aspx页面和.SQL页面

For security reasons you do not want anyone else to be able to remotely hit the RunSQL.aspx page (since it might recreate your database causing you to lose data).
出于安全考虑,你可能不洗碗别人远程的运行RunSQL.aspx页面(因为这样将会重新创建您的数据库导致你丢失数据。)

Step 9: Update the Web.Config file of your application to point to the hoster database
第九步:升级网站的Web.config文件来指向你的新生成的数据库文件。

The only final step remaining then is to update your web.config file's <connectionStrings> section to also point at your remote hoster's database connectionstring value.  Once you do this your app should work fine remotely.
最后你需要升级你的web.config文件的<connectiongStrings>配置节的值为你新建的远程数据库连接字符串的值。这样你的程序就可以很好的远程工作了。

Hope this helps,

Scott

P.S. For more of my ASP.NET Tips, Tricks and Recipes, please visit this page.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章