none
Running external ADO.NET script file programmatically RRS feed

  • Question

  • Hi,

    I want to restore a DB to its original state, programmatically via ASP.NET and ADO.NET tools.

    I'm thinking loading an external sql script file to be ran in ADO.NET from a ASP.NET process.

    So, How can I use ADO.NET commands to run an external script file? or any other suggests if you have them?

    Thanks,

    King 

    • Moved by Vicky SongMicrosoft employee Wednesday, June 27, 2012 5:16 AM (From:Visual Studio Database Development Tools (Formerly "Database Edition Forum"))
    Tuesday, June 26, 2012 2:11 PM

Answers

  • Hi King Coffee,

    Welcome to MSDN Forum.

    Please refer to the code below.

    using System.Data.SqlClient;
    using System.IO;
    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
     
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
                FileInfo file = new FileInfo("C:\\myscript.sql"); 
                string script = file.OpenText().ReadToEnd(); 
                SqlConnection conn = new SqlConnection(sqlConnectionString); 
                Server server = new Server(new ServerConnection(conn)); 
                server.ConnectionContext.ExecuteNonQuery(script);
            }
        }
    }
    

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, June 27, 2012 7:41 AM
    Moderator

All replies

  • Hi King,

    I am moving your case to the ADO.NET Managed Providers forum so that you can get better support there.

    Thanks.


    Vicky Song [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, June 27, 2012 5:15 AM
  • Hi King Coffee,

    Welcome to MSDN Forum.

    Please refer to the code below.

    using System.Data.SqlClient;
    using System.IO;
    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
     
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
                FileInfo file = new FileInfo("C:\\myscript.sql"); 
                string script = file.OpenText().ReadToEnd(); 
                SqlConnection conn = new SqlConnection(sqlConnectionString); 
                Server server = new Server(new ServerConnection(conn)); 
                server.ConnectionContext.ExecuteNonQuery(script);
            }
        }
    }
    

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, June 27, 2012 7:41 AM
    Moderator