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

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

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

这样所有的账户都改为dbo,即可。

下一步,把脚本命名为sqlscript.txt, 最好不要叫sqlscript.sql,下面会介绍。

然后通过ftp把脚本放到网站的空间。

编写脚本,例如命名为runsql.aspx ,然后运行该脚本即可还原数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<%
 
    // 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”;

系统可能无法运行。这样,就完成了数据库转移。

作者:mqingqing123 博客:http://www.cnblogs.com/mqingqing123

版权所有:为网站而疯狂-给站长一个五星级的家转载请注明来源,谢谢!

还不快抢沙发       我也不甘寂寞

欢迎留言

友情提示:
1、请勿发表色情、违法、商业广告等信息,谢谢。
2、留言想要有头像?请看 “我” 的。