Thursday, 15 July 2010

c# - MVC4 Listing available tables in database -



c# - MVC4 Listing available tables in database -

i required utilize use asp.net mvc4 framework build info management tool perform queries on user provided data. since info provided user, schema of table different each time, hence model table unknown until query performed on database show list of tables in database , table selected.

to things going, want start connecting database using connection-string , performing query lists of available tables in database , pass list view.

so far, based on research i've done, have come next code:

/*inside model*/ public class dbtablescontext : dbcontext { public dbtablescontext(string connectionstring) : base(connectionstring) { } } /*inside controller*/ public actionresult index() { string constring= connectionstring(); list<string> info = new list<string>(); dbtablescontext db = new dbtablescontext(constring); db.database.connection.open(); db.database.sqlquery(typeof(list<string>), "select * sys.tables", data); db.database.connection.close(); viewdata["data"] = data; homecoming view(); } public string connectionstring() { sqlconnectionstringbuilder sqlbuild = new sqlconnectionstringbuilder(); sqlbuild.datasource = "(localdb)\\v11.0"; sqlbuild.initialcatalog = "playdb"; homecoming sqlbuild.connectionstring; } /*inside view */ @{ viewbag.title = "index"; } @{ //foreach loop @viewdata["data"] //since list returns empty, i'm unsure how for-each loop work. }

when build , run code, not errors, however, when access view page, blank page believe because nil happening list passing sqlquery(). new .net,mvc , entity frameworks, hence not sure why behavior occurring.

any help or guidance highly appreciated. apologize in advance if confusing anyone.

stick pattern: mvc. create own database (or info storage).

make models, controllers , views:

/models

database // name, connection, type, whatever. table // database, columns, columns // type, value. query // queries can stored user repeat it??

/controllers

connectioncontroller // here can set code have.

querycontroller

/views

/connection/databases /query/builder

... in opinion.

c# sql asp.net-mvc entity-framework asp.net-mvc-4

No comments:

Post a Comment