使用C#从SQL Server检索表数据

时间:2013-02-22 05:57:49

标签: c# sql-server asp.net-mvc

我正在尝试检索数据库表中的所有People。该表由以下列组成:

ID, FirstName, LastNamre, Age, School

在下面的代码中,我只保存列表中表的前2个值。我希望将表格中的所有值保存到列表中。

我已经强调了以下代码的一部分;

i.Add(new SelectListItem
      {
          Value = sql.GetInt32(0).ToString(), 
          Text = sql.GetString(1)  
      });

完整代码;

public List<SelectListItem> getAllPeople()
{
   var i = new List<SelectListItem>();

   try
   {
       using (SqlCommand com = new SqlCommand("SELECT * FROM USERS", con))
       {
           con.Open();

           SqlDataReader sql = com.ExecuteReader();

           while (sql.Read())
           {
               i.Add(new SelectListItem
                         {
                            Value = sql.GetInt32(0).ToString(), 
                            Text = sql.GetString(1)  
                         });
           }
        }
    } 
   ...

更新

我的View类,如何在视图中显示值?

@model MyPro.Models.MyPersonModel

....

@using (Html.BeginForm())
{
...


@foreach (var item in MyPro.Models.MyPersonModel)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.?????)
        </td>

3 个答案:

答案 0 :(得分:1)

试试这个:

i.Add(new SelectListItem
{
    Value = sql.GetInt32(0).ToString(), 
    Text = sql.GetString(1) + " " + sql.GetString(2) + " - " + sql.GetString(3) + " - " + sql.GetString(4)
});

<强>更新

如果我这样做,我会使用ajax,这样当SelectListItem更改时,我只会通过People获得必要的数据。ID(这是SelectListItem的值)而不是获取数据库中所有People的所有数据。有点像:

$('#SelectListItemId').change(function(){
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "People/GetDetails",
        data: '{ id: this.val() }',
        success: function (msg) {
            alert(msg.d);
        }
    });
});

在您的问题中添加视图后更新

由于您在发布我的答案时没有发布您的观点而您没有包含MyPersonModel,我认为这是您的MyPersonModel

public class MyPersonModel
{
     public int ID { get;set; }
     public string FirstName { get;set; }
     public string LastNamre { get;set; }
     public int Age { get;set; }
     public string School { get;set; }
}

然后我相信您的观点应该是IEnumerable<MyPro.Models.MyPersonModel>

@model IEnumerable<MyPro.Models.MyPersonModel>

....

@using (Html.BeginForm())
{
...


@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastNamre)
        </td>
...

你的后退代码应该是这样的:

public List<MyPersonModel> getAllPeople()
{
   var people = new List<MyPersonModel>();

   try
   {
       using (SqlCommand com = new SqlCommand("SELECT * FROM USERS", con))
       {
           con.Open();

           SqlDataReader sql = com.ExecuteReader();

           while (sql.Read())
           {
               people.Add(new MyPersonModel
                          {
                              ID = sql.GetInt32(0), 
                              FirstName = sql.GetString(1),
                              LastNamre = sql.GetString(2),
                              Age = sql.GetInt32(3) ,
                              School = sql.GetString(4)
                          });
           }
        }
    }
} 

至于javascript,您对SelectListItem的使用让我相信您想在下拉列表中显示People列表。我只是在暗示,如果是这样的话,我会这样做,我会用javascript来获取其他细节。

答案 1 :(得分:1)

添加课程:

public class SomeClass
{
     public Int32 ID {get;set;
     public String FirstName {get;set;}
     public String LastNamre {get;set;}
     public Int32 Age {get;set;}
     public String School {get;set;}
}

修改选择器:

           i.Add(new SomeClass
                     {
                        ID = sql.GetInt32(0).ToString(), 
                        FirstName = sql.GetString(1),
                        LastNamre = sql.GetString(2),
                        Age = sql.GetInt32(3),
                        School = sql.GetString(4)
                     });

答案 2 :(得分:1)

您需要创建自己的Person课程:

public class Person
{
     public int ID { get;set; }
     public string FirstName { get;set; }
     public string LastNamre { get;set; }
     public int Age { get;set; }
     public string School { get;set; }
}

然后在检索数据时使用它(而不是SelectListItem):

public List<Person> getAllPeople()
{
   var people = new List<People>();

   try
   {
       using (SqlCommand com = new SqlCommand("SELECT * FROM USERS", con))
       {
           con.Open();

           SqlDataReader sql = com.ExecuteReader();

           while (sql.Read())
           {
               people.Add(new Person
                          {
                              ID = sql.GetInt32(0), 
                              FirstName = sql.GetString(1),
                              LastName = sql.GetString(2),
                              Age = sql.GetInt32(3) ,
                              School = sql.GetString(4)
                          });
           }
        }
    }