一個小把戲:如何上傳一個.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.

 

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