快速將網(wǎng)站的Mssql數(shù)據(jù)庫遷移國外
導(dǎo)讀:將網(wǎng)站從國內(nèi)網(wǎng)站搬移到了Lunarpage,程序轉(zhuǎn)移比較簡單,使用cuteftp上傳上去就可以了。但是數(shù)據(jù)庫轉(zhuǎn)移一直都是很棘手的一個(gè)問題。數(shù)據(jù)庫轉(zhuǎn)移最簡單的方法是使用DTS,但是Lunarpages數(shù)據(jù)庫不支持遠(yuǎn)程數(shù)據(jù)庫鏈接,所以無法使用DTS,因此只好使用publishing轉(zhuǎn)移數(shù)據(jù)。
具體步驟如下:
Step1. 運(yùn)行 SqlPubWiz.exe
Publishing類似MS SQL的一個(gè)插件,你可以到
http://www.microsoft.com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A
下載,運(yùn)行后可以在tools下找到
Step2 運(yùn)行后,會(huì)出現(xiàn)運(yùn)行向?qū)?,找到本地?cái)?shù)據(jù)庫
Step3.選項(xiàng)要生成的類型,系統(tǒng)會(huì)自動(dòng)檢測可用內(nèi)容,一般之選擇“表”“存儲(chǔ)過程”和“視圖”,對(duì)于Users就不要讓系統(tǒng)生成了
點(diǎn)擊Next,一直完成。
更改數(shù)據(jù)庫擁有者
以下是核心,非常重要,否則不會(huì)成功。
在我們使用網(wǎng)站時(shí),通常會(huì)使用SP給我們的賬戶,例如我原來的數(shù)據(jù)庫叫做 “bf4190_”
當(dāng)時(shí)網(wǎng)站供應(yīng)商給我的賬戶為 bf419,則系統(tǒng)生成的數(shù)據(jù)表如下
你可以看到,有的表前面有前綴bf419,有的有前綴dbo (db哦,是database owner),這很不同。因?yàn)樵谖覀兘⒈頃r(shí),腳本的寫法略有區(qū)別。
寫法一:
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,
}
對(duì)于第一種,生成的表就是 dbo.ads, 而第二個(gè)表則是 bf419.ads,因?yàn)槟愕腷f419其實(shí)就是dbo,所以系統(tǒng)可以運(yùn)行。
但是,當(dāng)你把數(shù)據(jù)庫轉(zhuǎn)移到新的服務(wù)商時(shí),如果你的賬戶叫做XXXX,則上面建立bf419.ads則出現(xiàn)錯(cuò)誤,而用 dbo.ads 則完全沒有問題。
通常新舊服務(wù)商給用戶開的用戶名并不一樣,所以我們需要更改一下數(shù)據(jù)庫的所有者。
#p#
接下來,用寫字板打開,搜索數(shù)據(jù)庫所有者都更改為dbo
這樣所有的賬戶都改為dbo,即可。
下一步,把腳本命名為sqlscript.txt, 最好不要叫sqlscript.sql,下面會(huì)介紹。
然后通過ftp把腳本放到網(wǎng)站的空間。
編寫腳本,例如命名為runsql.aspx ,然后運(yùn)行該腳本即可還原數(shù)據(jù)庫
<%
// 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”;
是用戶腳本地址,因?yàn)楹芏嗫臻g禁止獲取sql,所以,改成這樣
string fileUrl = @“http://www.sohu.com/sqlscript.sql”;
系統(tǒng)可能無法運(yùn)行。到這,數(shù)據(jù)庫遷移的工作就全部完成啦。希望上文中講到的內(nèi)容對(duì)大家能夠有所幫助。
【編輯推薦】