if you are using SQL Server, the simpler way is to use SQL statement like this:
select top 1 * from yourtable order by SomeIDOrDateTimeField DESCotherwise, if you do
select * from yourtableand if you are using DataSet, then just get
ds.Rows[ds.Rows.Count-1]["字段"]if you are using SqlDataReader, then you need to do something likeString s;
while (dr.Read())
s = (String)dr["字段"];
select top 1 * from yourtable order by SomeIDOrDateTimeField DESCotherwise, if you do
select * from yourtableand if you are using DataSet, then just get
ds.Rows[ds.Rows.Count-1]["字段"]if you are using SqlDataReader, then you need to do something likeString s;
while (dr.Read())
s = (String)dr["字段"];
解决方案 »
- 得到网页源代码奇怪问题
- 用XML作为服务器端和客户端之间的通讯,是否可行?
- 因缺少 CategoryName 而未能初始化
- 请教一个关于c#形参的低级问题,谢谢!
- 急,急,急~~~
- 在Form1上动态添加了3个ChickBox控件,怎样实现只能使其中的一个被选中呢?即:当其中一个被选中后,再选择其他ChickBox时,弹出对话框,
- 我的第一贴——问问有关文件传输的方面的问题
- ◥◣◥◣紧急求助◢◤◢◤: 对程序集“TeeChart”进行强名称验证失败。。
- ?info_hash=%b4%15T%9b%86q%24%88%19H%c6%c7%8d%9d%90%be%a8d%a1%b1 怎么转为 b415549b867124881948c6c78d9d90bea864a1b1 100分!!!急!!!!!!
- 光标定位!!! 求大神指教
- 谁能搞定这个Exce数据库,快疯了!放分(含程序范例与数据库下载)
- 怎样使报表控件动态连接数据库,动态绑定控件?在线等待?
是反会一个数据集吗?该怎样提取某一个字段的值?
谢谢!
SqlCommand myCommand = new SqlCommand("select top 1 * from yourtable order by SomeIDOrDateTimeField DESC", myConnection);
myCommand.Connection.Open();
SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (myReader.Read())
{
System.Console.WriteLine(myReader["YourField"].ToString());
}myReader.Close();or better yet, if you only need that one column, you can use ExecuteScalar(), for example:SqlConnection conn = new SqlConnection("Server=localhost;Database=pubs;UID=sa;PWD=;");
SqlCommand cmd = new SqlCommand("Select top 1 au_lname from authors order by au_lname desc",conn);
conn.Open();
String s = (String)cmd.ExecuteScalar();
Console.WriteLine(s);
conn.Close();