程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> 使用publishing轉移MS SQL數據(2)

使用publishing轉移MS SQL數據(2)

編輯:關於MYSQL數據庫

    接下來,用寫字板打開,搜索數據庫所有者都更改為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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved