简体   繁体   中英

MVC4 Listing available tables in database

I am required to use use ASP.NET MVC4 Framework to build a data management tool that will perform queries on user provided data. Since the data will be provided by the user, the schema of the table will be different each time, hence the model for the table will be unknown until a query is performed on the database which will show a list of tables in the database and a table is selected.

To get things going, I want to simply start by connecting to a database using a connection-string and performing a query that lists all of the available tables in the database and pass that list to a View.

So far, based on the research that I've done, I have come up with the following code:

/*Inside the model*/
public class DbTablesContext : DbContext
{
    public DbTablesContext(string connectionstring) : base(connectionstring)
    {
    }
}

/*Inside the controller*/ 
   public ActionResult Index()
        {
            string constring= ConnectionString();
            List<string> data = new List<string>();

            DbTablesContext db = new DbTablesContext(constring);

            db.Database.Connection.Open();
            db.Database.SqlQuery(typeof(List<string>), "select * from sys.tables", data);
            db.Database.Connection.Close();

            ViewData["data"] = data;


            return View();
        }

        public string ConnectionString()
        {
            SqlConnectionStringBuilder sqlbuild = new SqlConnectionStringBuilder();

            sqlbuild.DataSource = "(LocalDB)\\v11.0";
            sqlbuild.InitialCatalog = "playDB";

            return sqlbuild.ConnectionString;
        }

/*Inside the view */  
@{
    ViewBag.Title = "Index";
}

@{

    //foreach loop for @ViewData["data"]
    //Since the list returns empty, I'm unsure how the for-each loop will work.
}

When I build and run this code, I do not get any errors, however, when I access the view page, I get a blank page which I believe is because nothing is happening to the list I am passing to SqlQuery(). I am fairly new with the .NET,MVC and Entity Frameworks, hence I am not sure why this behavior is occurring.

Any help or guidance with this is highly appreciated. I also apologize in advance if I am confusing anyone.

Stick to the pattern: MVC. Create YOUR own database (or data storage).

Make your models, controllers and views:

  • /Models

    • Database // Name, Connection, type, whatever.
    • Table // database, columns,
    • Columns // type, value.
    • Query // Queries can be stored so user could repeat it??
  • /Controllers

    • ConnectionController // Here you can put the code you have.

    • QueryController

  • /Views

    • /Connection/Databases
    • /Query/Builder

... in my opinion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM