简体   繁体   English

C#,ASP.NET-从数据库收集数据

[英]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. 我知道我可以使用变量(i)而不是数字进行循环处理。 But then I would need to use the ViewData array, which is problem for me. 但是然后我需要使用ViewData数组,这对我来说是个问题。

Example: ViewData["a"][i]; 示例: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. 然后,您将创建一个List<Customer>() ,并将其设置为您的MVC页面的模型。

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: 然后,在您的MVC页面上,将<%@ Page %>标头设置为具有Inherits属性,如下所示:

<%@ 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: 浏览“ fb”表中每个DataRow的最简单方法是:

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: 若要返回DataRow对象中特定列的值,请执行以下操作:

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. 这样,您说的是:“从'FirstName'列获取值,并将其强制转换为字符串,将'Age'列的值强制转换为整数,依此类推。但是,您需要验证输入每一列的类型,并相应地调整您的演员表。

You need to loop through dsl as this is the dataset. 您需要遍历dsl因为这是数据集。 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 您将获得一个DataRow Rows[0] ,您需要在其中循环遍历dsl以获取每个 DataRow

Why not use a DataReader instead? 为什么不使用DataReader呢? 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. 如果需要,您可以使用它来填充DataTable并将其传递给DataSet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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