0

I'am a fresh man of ASP.NET MVC5.

I wonder How to achive below function with MVC5:

In MVC5 , I deign a textarea that user can input select query then click button to do select function and show results. But , there are multiple target DB which means different table with different column count.(user might select different table each time)

Before I start with MVC5 , I did some similar works like:

1.In Windows Form C#, I could use DataSet to store results from multiple tables and show result with DataGridView object

2.In ASP.NET WebForm, I could use DataTable with < asp:GridView > to show a select query result

Now, with ASP.NET MVC5 , the user interface already done.

But when I search how to show result with MVC5, all the reference shows I have to declare datatype in models.cs then call it.But I have so many tables and each table contains different columns count. I stucked. q_q

Would you give me some tips or keywords to find exactly information that I need?

Thanks all. Wish Everyone well.(if my statment are not understandable, please let me know)

Zac
  • 1
  • You can use entity framework in MVC. Refer https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/database-first-approach-in-entity-framework/ – Rutuja Jul 29 '20 at 05:33
  • This is what you need right? https://stackoverflow.com/questions/26749429/anonymous-type-result-from-sql-query-execution-entity-framework/42419101#42419101 – Peter Jul 29 '20 at 05:49
  • did you try ViewModel? – Julius Limson Jul 29 '20 at 06:21
  • To Rutuja , Thanks. I will start to read it. / To Rymo (for now) I think I cannot implement EF technology to achieve my goal. /To Julius I will try. Thanks! – Zac Jul 30 '20 at 03:56

1 Answers1

0

Last Afternoon I found a way to achieve this function and I successfully did it. → /MVC5 + ADO.NET/

My problem is , I cannot design a model(declare datatype) at first because this tool would be used to query multiple DataTable and each DataTable with different column count.(this is might due to my less knowledge of EF. EF is a new technology for me)

Back to theme

(this might not right)I used ADO.NET in Controller , after gain data with DataSet then pass value to DataTable. And returnView with DataTable.

DBDataAdapter da = new DBDataAdapter(UserInputSQLCommand, conn);
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                da.Fill(ds, "result");
                dt = ds.Tables[0];
                return View(dt);

only add below code in Result page(.cshtml file)

<table border="1" cellpadding="5">
<thead>
    <tr>
        @foreach (System.Data.DataColumn col in Model.Columns)
        {
            <th>@col.Caption</th>
        }
    </tr>
</thead>
<tbody>
    @foreach (System.Data.DataRow row in Model.Rows)
    {
        <tr>
            @foreach (var cell in row.ItemArray)
            {
                <td>@cell.ToString()</td>
            }
        </tr>
    }
</tbody>

but I face some problem at present resule at same query page. so I create new page just for result information and it did work.

My new challenge is at sql query view I add

@model MyProjectName.Models.SqlQueryView

Visual studio report there are no definition of Rows and Columns.

Then when I create a new View and there is no using @model But it works. no showing error message. I wonder Rows and Columns might be Systems definition But due to My using @model then I need to declare it ?

Zac
  • 1