0

Is it possible that from within my Asp.net MVC (EF) application i connect to another database, run some scripts and then close the connection.

Since this application already connects to a default database as soon as i run the application. Can i then connect to another one simultaneously (from a controller action) for a while and run some scripts on that one and then close connection?

6
  • 2
    Yes. It is possible. Commented Oct 19, 2017 at 23:51
  • how can i go about it? Commented Oct 19, 2017 at 23:53
  • Read about SqlCommand Commented Oct 20, 2017 at 0:22
  • What's the point of using an ORM like EF just so to run adhoc SQL queries? You are arguably better off just using something like ADO.NET learn.microsoft.com/en-us/dotnet/framework/data/adonet/… Commented Oct 20, 2017 at 1:14
  • Besides using EF on our whole project. As per project requirement, we need an admin page (mvc view) where admin can specify connection details and click install which should run scripts in the background. Hope this answers your question Commented Oct 20, 2017 at 1:18

2 Answers 2

1

Setup a separate context with the connection string for the other database and use on of the options below.

Writing SQL queries for non-entity types

A SQL query returning instances of any type, including primitive types, can be created using the SqlQuery method on the Database class. For example:

using (var context = new BloggingContext()) 
{ 
    var blogNames = context.Database.SqlQuery<string>( 
                       "SELECT Name FROM dbo.Blogs").ToList(); 
}

The results returned from SqlQuery on Database will never be tracked by the context even if the objects are instances of an entity type.

Sending raw commands to the database

Non-query commands can be sent to the database using the ExecuteSqlCommand method on Database. For example:

using (var context = new BloggingContext()) 
{ 
    context.Database.ExecuteSqlCommand( 
        "UPDATE dbo.Blogs SET Name = 'Another Name' WHERE BlogId = 1"); 
}

Note that any changes made to data in the database using ExecuteSqlCommand are opaque to the context until entities are loaded or reloaded from the database.

Output Parameters

If output parameters are used, their values will not be available until the results have been read completely.

Sign up to request clarification or add additional context in comments.

3 Comments

I have multiple contexts already setup but here i needed to simply run the scripts weather i am logged into the application or not
Are the scripts in actual files? Can you clarify how they would be triggered? In the question you said from and admin page and you just mentioned they would be triggered when you are not logged in.
Yes i have shared complete code that is working in my answer
0

Ok. So This is what i simply did in my controller

public class AdminController : Controller
{
    public ActionResult Install()
    {
        foreach (ConnectionStringSettings c in System.Configuration.ConfigurationManager.ConnectionStrings)
        {


                //get all files from this folder except insert and mysql create scripts and then run each of them
                string script = System.IO.File.ReadAllText(@"C:\Test.sql");
                MySqlConnection conn = new MySqlConnection(c.ConnectionString);
                try
                {
                    conn.Open();
                    MySqlScript m = new MySqlScript(conn, script);
                    m.Delimiter = "$$";
                    m.Execute();
                    conn.Close();
                }
                catch { }

        }

        return View();
    }
}

5 Comments

So what happened to "Besides using EF on our whole project"?
@MickyD we wanted something like a url that can be typed in by admins to run some scripts on any db of our choice even when we r not logged in. Once logged in, the EF establishes its connection with a database and business goes as usual. My confusion was if we are logged in would it be possible to establish another connection and that also without using EF. For which Shyju answered yes. So i started trying. Aron's answer also helped. hope this answers your question
But your answer isnt using EF
thats why i said "Besides using EF" :)
Ugh you make zero sense

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.