如何根据表行数据ID从数据库中检索数据? [英] How do I retrieve data from database based on the table row data id?

查看:81
本文介绍了如何根据表行数据ID从数据库中检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将从数据库中检索的数据显示到表格。





我显示到表格后如下所示,如何根据学生ID点击注册时,我是否会注册学生?



我尝试了什么:



I want to display the data retrieve from database to a table.


After i displayed to table as shown below, how do i enroll the student when clicked on "Enroll" based on the student id?

What I have tried:

public string getStudentData()
       {
           string data = "";
           using(SqlConnection conn = new SqlConnection(connectionString))
           {
               using(SqlCommand cmd = new SqlCommand())
               {
                   cmd.Connection = conn;
                   cmd.CommandType = CommandType.StoredProcedure;
                   cmd.CommandText = "UspGetStudents";
                   cmd.Connection.Open();
                   using(SqlDataReader sqlRdr = cmd.ExecuteReader())
                   {
                       // table = new DataTable();
                       // table.Load(reader);
                       if (sqlRdr.HasRows)
                       {
                           while (sqlRdr.Read())
                           {
                               int studentId = sqlRdr.GetInt32(0);
                               string Name = sqlRdr.GetString(1);
                               string EmailAddress = sqlRdr.GetString(2);
                               string Gender = sqlRdr.GetString(3);
                               data += "<tr><td>" + studentId + "</td>
                                            <td>" + Name + "</td>
                                            <td>" + EmailAddress + "</td>
                                            <td>" + Gender + "</td>
                                            <td><a href="#">"+Enroll+"</a></td></tr>";
                           }
                       }
                   }
               }
               return data;
           }
   }

推荐答案

<a href="#">"+Enroll+"</a>

您的锚点参考指向任何内容。将其指向一个页面以接受相关的 QueryString [ ^ ]



或者如果这是一个Asp.Net网站,你会最好使用 DataGrid Class( System.Web.UI.WebControls) [ ^ ]



ASP.NET中DataGrid的示例 [ ^ ]

Your anchor reference points to nothing. Point it to a page to accept an associated QueryString[^]

Or if this is an Asp.Net website, you would be much better off using the DataGrid Class (System.Web.UI.WebControls)[^]

Example of DataGrid in ASP.NET[^]


这篇关于如何根据表行数据ID从数据库中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆