最近,将网站从国内网站搬移到了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下找到

window更新yarn window更新要将数据传输境外_c#

 

Step2 运行后,会出现运行向导,找到本地数据库

window更新yarn window更新要将数据传输境外_c#_02

 

Step3.选项要生成的类型,系统会自动检测可用内容,一般之选择“表”“存储过程”和“视图”,对于Users就不要让系统生成了

window更新yarn window更新要将数据传输境外_数据库_03

 

点击Next,一直完成。

 

 

更改数据库拥有者

以下是核心,非常重要,否则不会成功。

在我们使用网站时,通常会使用SP给我们的账户,例如我原来的数据库叫做 “bf4190_”

当时网站供应商给我的账户为 bf419,则系统生成的数据表如下

window更新yarn window更新要将数据传输境外_xhtml_04

 

你可以看到,有的表前面有前缀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 则完全没有问题。

 通常新旧服务商给用户开的用户名并不一样,所以我们需要更改一下数据库的所有者。

接下来,用写字板打开,搜索数据库所有者都更改为dbo

window更新yarn window更新要将数据传输境外_c#_05

 

这样所有的账户都改为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";

系统可能无法运行。

 

这样,就完成了数据库转移。