如何在c#ado .net中显示数据库中的最后一条记录 [英] How to display the last record from the database in c# ado .net

查看:158
本文介绍了如何在c#ado .net中显示数据库中的最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储过程



if(@Activity ='All')

选择top 1 FullName,Email,Pasword,imgurl注册顺序by id desc

结束


用户户币




公开列表< user> ;全部(ActivityMode a)

{

SqlConnection con = ConnectionDAL.SQL();

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText =& amp; quot; Sp_User& amp; ;;

cmd.Parameters.AddWithValue(& amp; quot; @ activity& amp; quot;,a.ToString());

List& amp; lt;使用者&放大器;放大器; GT; lst = null;

con.Open();

使用(con)

{

SqlDataReader DR = cmd.ExecuteReader();

if(DR.HasRows)

{

lst = new List& amp; lt; User& amp; amp; gt;();

while(DR.Read())

{

用户obj =新用户();

obj.Id = Convert.ToInt32(DR [& amp; quot; ID& amp; quot;]);

obj.FullName = DR [& amp; quot; FullName& ; amp; quot;]。ToString();

obj.Email = DR [& amp; quot; Email& amp; quot;]。ToString();

obj.Password = DR [& amp; quot;密码& amp; quot;]。ToString();

obj.Address = DR [& amp; quot;地址& amp; quot; ;]。ToString();

o bj.Imgurl = DR [& amp; quot; Imgurl& amp; quot;]。ToString();

lst.Add(obj);

}

}

}

返回lst;

}& lt; / pre& gt;< / pre>





我尝试过:



i`m运行项目显示错误..



如何解决..

i只是差不多的最后记录..请帮助我..





'/'应用程序中的服务器错误。



ID



描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详情:System.IndexOutOfRangeException:ID



来源错误:





第90行:{

第91行:用户obj =新用户();

第92行:obj.Id = Convert.ToInt32(DR [ID]);

第93行:obj.FullName = DR [FullName]。ToString();

第94行:obj.Email = DR [Email]。ToString();

Store procdure

if(@Activity = 'All')
select top 1 FullName,Email,Pasword,imgurl registration order by id desc
End

in UserDal

public List<user>All(ActivityMode a)
{
SqlConnection con = ConnectionDAL.SQL();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = &amp;quot;Sp_User&amp;quot;;
cmd.Parameters.AddWithValue(&amp;quot;@activity&amp;quot;, a.ToString());
List&amp;lt;User&amp;gt; lst = null;
con.Open();
using (con)
{
SqlDataReader DR = cmd.ExecuteReader();
if (DR.HasRows)
{
lst = new List&amp;lt;User&amp;gt;();
while (DR.Read())
{
User obj = new User();
obj.Id = Convert.ToInt32(DR[&amp;quot;ID&amp;quot;]);
obj.FullName = DR[&amp;quot;FullName&amp;quot;].ToString();
obj.Email = DR[&amp;quot;Email&amp;quot;].ToString();
obj.Password = DR[&amp;quot;Password&amp;quot;].ToString();
obj.Address = DR[&amp;quot;Address&amp;quot;].ToString();
obj.Imgurl = DR[&amp;quot;Imgurl&amp;quot;].ToString();
lst.Add(obj);
}
}
}
return lst;
}&lt;/pre&gt;</pre>
,

What I have tried:

i`m running the project the show the error..

how to resolve..
i am only disply last record..plz help me..


Server Error in '/' Application.

ID

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: ID

Source Error:


Line 90: {
Line 91: User obj = new User();
Line 92: obj.Id = Convert.ToInt32(DR["ID"]);
Line 93: obj.FullName = DR["FullName"].ToString();
Line 94: obj.Email = DR["Email"].ToString();

推荐答案

查看存储过程。你没有选择ID栏。



编辑

这不是它在这里如何运作。代码项目成员不会为您工作。我们花个人时间来帮助这个论坛上的人,我们没有任何报酬。我们这样做是因为我们喜欢发展以及如何帮助他人解决发展问题。我对你的系统一无所知,你想要完成什么,期望想要最后的记录。你是完成任务的最佳人选。如果我为你工作,你会学到什么。没有。甚至要求我写一个完整的解决方案是非常粗鲁的。



解决问题的方法非常简单。您需要做的就是查看错误消息和代码。您的错误消息清楚地告诉您,您正在尝试引用不存在的项目,在本例中为ID。您可以从代码中看到您正在尝试设置obj.Id = Convert.ToInt(DR [ID])。这就是你的错误来源。



现在看看你的存储过程。在你选择列表,FullName,Email,Pasword,imgurl,你选择了4个项目,但其中一个不是ID。
Look at your stored procedure. You are not selecting the ID column.

Edit
That is not how it works around here. Code Project members will not do your work for you. We take our personal time to help people on this forum, and we are not paid for anything. We do this because we love development and what to help others with their development issues. I know nothing about your system, what you are trying to accomplish, expect for wanting the last record. You are best person to complete your task. If I were to do you work for you, what would you learn. Nothing. Even asking me to write a complete solution is very rude.

The solution to your problem is very simple. All you need to do is look at the error message and your code. You error message clearly tells you that you are trying to reference a item that does not exists, in this case ID. And you can see from your code that you are trying to set obj.Id = Convert.ToInt(DR["ID"]). That is where your error is coming from.

Now look at your stored procedure. In you select list,FullName,Email,Pasword,imgurl, you are selecting 4 items, but one of those is not ID.


这篇关于如何在c#ado .net中显示数据库中的最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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