简体   繁体   中英

C#, ASP.NET - Gathering data from a database

I have code, which returns me a row out of a database,

            con = new System.Data.SqlClient.SqlConnection();
            dsl = new DataSet();
            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\tbl.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            con.Open();

            string sql = "SELECT * From tbl_fb";
            da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
            da.Fill(dsl, "fb");
            DataRow dRow = dsl.Tables["fb"].Rows[0];
            ViewData["a"] = dRow.ItemArray.GetValue(1).ToString();

            ViewData["b"] = "afagjma";
            con.Close();

Is there a way, that I could use a loop to get all rows from table. The number of rows are unknown. I know I can do it with a loop, by using the variable (i) instead of numbers. But then I would need to use the ViewData array, which is problem for me.

Example: ViewData["a"][i];

You should set up a class that defines what data you're returning from your database.

For instance:

public class Customer  
{
   public int Id { get; set;} 
   public string Name { get; set;} 
} 

And then you would create a List<Customer>() , and set this as the Model for your MVC Page.

For instance:

var data = new new List<Customer>();    
ViewData.Model = data; 

And load your data into your list:

foreach(var row in dsl.Tables["fb"].Rows)
{
   var customer = new Customer(); 
   customer.Id = row.Field<int?>("Id"); 
   customer.Name = row.Field<string>("Name");
   data.Add(customer);
}

And then, on your MVC Page, set the <%@ Page %> header to have the Inherits attribute like so:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Customer>>" %>

Now finally you can bind directly to the Model within the page:

<% foreach (var item in Model) { %>
  Customer Id: <%: item.Id %>
  Name: <%: item.Name %>
<% } %>

The simplest way to go through each DataRow in your "fb" table is:

foreach (DataRow dr in dsl.Tables["fb"].Rows) {
    // do something with each row
}

This will run through all rows of your table.

To return a value of a particular column in your DataRow object, do something like this:

string firstName = (string)dr["FirstName"];
string lastName = (string)dr["LastName"];
int age = (int)dr["Age"];

By doing this, you're saying "Get me the value from the 'FirstName' column and cast it as an string, cast the value from the 'Age' column as an integer, and so-on. However you need to verify the type being returned for each column and adjust your casts accordingly.

You need to loop through dsl as this is the dataset. This is your problem:

DataRow dRow = dsl.Tables["fb"].Rows[0];

You are getting a single DataRow Rows[0] where you need to loop through dsl to get each and every DataRow

Why not use a DataReader instead? It would work in a loop reading one row at a time. You can use it to populate a DataTable and pass it on to a DataSet , if needed.

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