快速將網站的Mssql數據庫遷移國外
導讀:將網站從國內網站搬移到了Lunarpage,程序轉移比較簡單,使用cuteftp上傳上去就可以了。但是數據庫轉移一直都是很棘手的一個問題。數據庫轉移最簡單的方法是使用DTS,但是Lunarpages數據庫不支持遠程數據庫鏈接,所以無法使用DTS,因此只好使用publishing轉移數據。
具體步驟如下:
Step1. 運行 SqlPubWiz.exe
Publishing類似MS SQL的一個插件,你可以到
http://www.microsoft.com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A
下載,運行后可以在tools下找到
Step2 運行后,會出現運行向導,找到本地數據庫
Step3.選項要生成的類型,系統會自動檢測可用內容,一般之選擇“表”“存儲過程”和“視圖”,對于Users就不要讓系統生成了
點擊Next,一直完成。
更改數據庫擁有者
以下是核心,非常重要,否則不會成功。
在我們使用網站時,通常會使用SP給我們的賬戶,例如我原來的數據庫叫做 “bf4190_”
當時網站供應商給我的賬戶為 bf419,則系統生成的數據表如下
你可以看到,有的表前面有前綴bf419,有的有前綴dbo (db哦,是database owner),這很不同。因為在我們建立表時,腳本的寫法略有區別。
寫法一:
CREATE TABLE [dbo].[ads] (
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[img] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
}
寫法二:
CREATE TABLE [ads] (
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[img] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
}
對于第一種,生成的表就是 dbo.ads, 而第二個表則是 bf419.ads,因為你的bf419其實就是dbo,所以系統可以運行。
但是,當你把數據庫轉移到新的服務商時,如果你的賬戶叫做XXXX,則上面建立bf419.ads則出現錯誤,而用 dbo.ads 則完全沒有問題。
通常新舊服務商給用戶開的用戶名并不一樣,所以我們需要更改一下數據庫的所有者。
#p#
接下來,用寫字板打開,搜索數據庫所有者都更改為dbo
這樣所有的賬戶都改為dbo,即可。
下一步,把腳本命名為sqlscript.txt, 最好不要叫sqlscript.sql,下面會介紹。
然后通過ftp把腳本放到網站的空間。
編寫腳本,例如命名為runsql.aspx ,然后運行該腳本即可還原數據庫
<%
// Sample code for executing a T-SQL file using an ASP.NET page
// Copyright (C) Microsoft Corporation, 2007. All rights reserved.
// Written as a sample with use in conjuction with the SQL Server Database Publishing Wizard
// For more information visit http://www.codeplex.com/sqlhost/
// **************************************************************************
// Note: Please ensure that you delete this page once your database has been published to the remote server
// **************************************************************************
%>
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%
// **************************************************************************
// Update these variables here
// **************************************************************************
// Url of the T-SQL file you want to run
string fileUrl = @"http://www.sohu.com/sqlscript.txt";
// Connection string to the server you want to execute against
string connectionString = @"Data Source=11.1.1.1;
User ID=hdd;Password=dd;Initial Catalog=s603";
// Timeout of batches (in seconds)
int timeout = 20000;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Executing T-SQL</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
<%
SqlConnection conn = null;
try
{
this.Response.Write(String.Format("Opening url {0}<BR>", fileUrl));
// read file
WebRequest request = WebRequest.Create(fileUrl);
using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
this.Response.Write("Connecting to SQL Server database...<BR>");
// Create new connection to database
conn = new SqlConnection(connectionString);
conn.Open();
while (!sr.EndOfStream)
{
StringBuilder sb = new StringBuilder();
SqlCommand cmd = conn.CreateCommand();
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
if (s != null && s.ToUpper().Trim().Equals("GO"))
{
break;
}
sb.AppendLine(s);
}
// Execute T-SQL against the target database
cmd.CommandText = sb.ToString();
cmd.CommandTimeout = timeout;
cmd.ExecuteNonQuery();
}
}
this.Response.Write("T-SQL file executed successfully");
}
catch (Exception ex)
{
this.Response.Write(String.Format("An error occured: {0}", ex.ToString()));
}
finally
{
// Close out the connection
//
if (conn != null)
{
try
{
conn.Close();
conn.Dispose();
}
catch (Exception e)
{
this.Response.Write(String.Format(@"Could not close the connection. Error was {0}", e.ToString()));
}
}
}
%>
</body>
</html>
需要注意:
string fileUrl = @“http://www.sohu.com/sqlscript.txt”;
是用戶腳本地址,因為很多空間禁止獲取sql,所以,改成這樣
string fileUrl = @“http://www.sohu.com/sqlscript.sql”;
系統可能無法運行。到這,數據庫遷移的工作就全部完成啦。希望上文中講到的內容對大家能夠有所幫助。
【編輯推薦】