简体   繁体   English

如何将数据表中的最后一行数据获取到字符串

[英]How can i get the last row data in my datatable to the string

我需要显示我进入数据表的最后一行,但我总是将第一行显示在这一行中。让我们假设这是调试时可以看到的图像,它有2列5行,我需要获得第二行第5列

Just change your query to 只需将查询更改为

"select top 1 id,name from demo where id='" + id + "' order by id desc;

You should create an sqlParameter for the id by the way... 您应该通过以下方式为id创建一个sqlParameter ...

To be honest I don't even see the point on using a datatable for this. 老实说,我什至看不到为此使用数据表的意义。

You should change your code to something like this: 您应该将代码更改为以下内容:

    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ReferenceToYourConnectionString"].ToString()))
    {
        cn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add("@Id", SqlDbType.Int).Value = int.Parse(ViewState["id"].ToString()); //Whatever your id columntype is
        cmd.CommandText = "SELECT TOP 1 id,name FROM demo WHERE id = @id ORDER BY id, name DESC";
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
             CKEditor1.Text = dr["name"].ToString()
        }
    }

更改代码以引用数据表的最后一行

string text = dt.Rows[dt.Rows.Count-1][1].ToString();

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

相关问题 如何获取数据表中一行的最后一个单元格? - How do I get the last cell in a row of a DataTable? 如何获得刚刚添加到DataTable中的行的标识? - How can I get the identity of a row that was just added to a DataTable? 如何从 DataTable 中获取一行或一列 - How I can get one Row or Column From DataTable 如何获取数据表的最后 5 行? - How do I get the last 5 rows of a datatable? 如何使我的Get DataTable方法更动态 - How can I make my Get DataTable Method more Dynamic 如何从jquery-datatable中选择一行,从控制器中获取更多数据并在表下显示它们? (mvc4) - How can I select a row from a jquery-datatable, get more data from the controller and show them under the table? (mvc4) 如何将数据添加到dataGridView1的最后一行/最后一列? - How can i add to dataGridView1 a data to the last row/column? 我如何从xml文件获取数据到数据表 - How can i get data from xml file to datatable 如何从特定选定行的事件发送者数据中获取模型数据? - How can I get the model data from my event sender data for specific selected row? 我做了一个数据表。 但是,它每次都需要我的数据库/数据表的第一列。 我该如何取第二行/列? - I made a datatable. But it takes every time the first column of my database/datatable. How can I take the second row/column of it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM