快速将网站的Mssql数据库迁移国外

数据库
将网站的Mssql数据库迁移国外是一种很高科技的技术,含金量是相当高的,本文就为大家介绍快速将网站的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”;

系统可能无法运行。到这,数据库迁移的工作就全部完成啦。希望上文中讲到的内容对大家能够有所帮助。

【编辑推荐】

  1. MySQL数据库中数据被删除后的恢复
  2. 网店与商城要重视数据库营销
  3. SQL Server数据库服务器高性能设置
责任编辑:迎迎 来源: 博客网
相关推荐

2023-05-10 09:24:10

TypeScript工具

2010-09-30 09:11:01

2011-03-14 13:43:56

2010-05-21 17:51:58

MySQL数据库

2012-10-19 10:21:07

数据库负载均衡mssqlserver

2011-05-11 10:26:36

MySQL数据库无缝迁移

2011-09-23 09:09:38

数据库迁移

2020-08-13 07:42:15

数据库Flyway代码

2010-06-04 18:32:48

MySQL数据库

2011-07-14 15:24:26

MSSQL数据库跨数据库查询

2010-06-01 17:45:57

MySQL数据库

2010-05-28 13:21:48

MySQL数据库

2023-12-21 09:16:40

Electron前端多进程架构

2011-04-29 14:30:23

2019-08-13 15:52:34

数据库同步迁移

2009-03-19 09:44:07

SQL Server数据库迁移数据库

2010-05-28 11:05:40

MSSQL Serve

2017-06-22 16:00:07

数据库NoSQL迁移实践

2011-10-14 13:50:54

数据库迁移

2017-11-22 09:20:41

数据库在线数据迁移Subscriptio
点赞
收藏

51CTO技术栈公众号