[已解决]关于SqlDataReader的一个问题

最近在做毕业设计时遇到了一些问题,希望大家能够帮我解决。我是按照.NET PetShop 4.0的结构设计了一个系统,但是在数据接口层遇到了一些问题,主要在用SqlDataReader读取数据时总是出错。具体情况如下:
DAL.Posts
None.gif public  IList < PostInfo >  GetThreads( int  forumID)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnString1"].ConnectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (SqlCommand command = new SqlCommand("GetThreads", connection))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    command.CommandType 
= CommandType.StoredProcedure;
InBlock.gif                    command.Parameters.Add(
new SqlParameter("@forumID", forumID));
InBlock.gif                    connection.Open();
InBlock.gif                    IList
<PostInfo> posts = new List<PostInfo>();
InBlock.gif                    
using (SqlDataReader rdr = command.ExecuteReader())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
while (rdr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
int i = rdr.FieldCount;
InBlock.gif                            PostInfo post 
= new PostInfo(rdr.GetInt32(0),
InBlock.gif                                                       rdr.GetDateTime(
1),
InBlock.gif                                                       rdr.GetString(
2),
InBlock.gif                                                       rdr.GetString(
3),
InBlock.gif                                                       rdr.GetInt32(
4),
InBlock.gif                                                       rdr.GetString(
5),
InBlock.gif                                                       rdr.GetInt32(
6),
InBlock.gif                                                       rdr.GetString(
7),
InBlock.gif                                                       
null,
InBlock.gif                                                       rdr.GetBoolean(
9),
InBlock.gif                                                       rdr.GetBoolean(
10),
InBlock.gif                                                       rdr.GetInt32(
11),
InBlock.gif                                                       rdr.GetInt32(
12),
InBlock.gif                                                       rdr.GetDateTime(
13),
InBlock.gif                                                       rdr.GetString(
14));
InBlock.gif                            posts.Add(post);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
return posts;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif}
在这里数据表的结构就不详细介绍了,其中用到的存储过程"GetThreads"为:
None.gif ALTER PROCEDURE GetThreads
None.gif    @forumID  
int
None.gif
None.gifAS
None.gif
None.gifSELECT Posts.PostID, Posts.AddedDate, Posts.AddedBy, Posts.AddedByIP, Posts.ForumID, Posts.ParentPostID, Posts.Title, Posts.Approved, Posts.Closed, Posts.ViewCount, Posts.ReplyCount, Posts.LastPostDate, Posts.LastPostBy
None.gifFROM Posts INNER JOIN Forums 
None.gifON Posts.ForumID 
=  Forums.ForumID
None.gifWHERE Posts.ForumID 
=  @forumID AND Posts.ParentPostID  =   0  AND Posts.Approved  =   1
None.gifRETURN
前台页面BrowseThreads.aspx为:
None.gif <% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " BrowseThreads.aspx.cs "  Inherits = " Web.BrowseThreads "   Title = " Browse Threads " %>
None.gif
None.gif
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
None.gif
None.gif
< html xmlns = " http://www.w3.org/1999/xhtml "   >
None.gif
< head runat = " server " >
None.gif
</ head >
None.gif
< body >
None.gif    
< form id = " form1 "  runat = " server " >
None.gif    
< div >
None.gif    
< asp:ObjectDataSource ID = " ObjectDataSource1 "  runat = " server "  SelectMethod = " GetThreads "
None.gif            TypeName
= " RFID.BLL.Post " >
None.gif            
< SelectParameters >
None.gif                
< asp:QueryStringParameter Name = " forumID "  QueryStringField = " ForumID "  Type = " Int32 "   />
None.gif            
</ SelectParameters >
None.gif        
</ asp:ObjectDataSource >
None.gif    
None.gif    
</ div >
None.gif        
< asp:GridView ID = " GridView1 "  runat = " server "  AutoGenerateColumns = " False "  DataSourceID = " ObjectDataSource1 " >
None.gif            
< Columns >
None.gif                
< asp:BoundField DataField = " ViewCount "  HeaderText = " ViewCount "  SortExpression = " ViewCount "   />
None.gif                
< asp:BoundField DataField = " ForumTitle "  HeaderText = " ForumTitle "  SortExpression = " ForumTitle "   />
None.gif                
< asp:BoundField DataField = " PostID "  HeaderText = " PostID "  SortExpression = " PostID "   />
None.gif                
< asp:BoundField DataField = " LastPostDate "  HeaderText = " LastPostDate "  SortExpression = " LastPostDate "   />
None.gif                
< asp:BoundField DataField = " AddedDate "  HeaderText = " AddedDate "  SortExpression = " AddedDate "   />
None.gif                
< asp:BoundField DataField = " Body "  HeaderText = " Body "  SortExpression = " Body "   />
None.gif                
< asp:BoundField DataField = " LastPostBy "  HeaderText = " LastPostBy "  SortExpression = " LastPostBy "   />
None.gif                
< asp:BoundField DataField = " Title "  HeaderText = " Title "  SortExpression = " Title "   />
None.gif                
< asp:BoundField DataField = " AddedBy "  HeaderText = " AddedBy "  SortExpression = " AddedBy "   />
None.gif                
< asp:BoundField DataField = " ReplyCount "  HeaderText = " ReplyCount "  SortExpression = " ReplyCount "   />
None.gif                
< asp:BoundField DataField = " ForumId "  HeaderText = " ForumId "  SortExpression = " ForumId "   />
None.gif                
< asp:BoundField DataField = " AddedByIP "  HeaderText = " AddedByIP "  SortExpression = " AddedByIP "   />
None.gif                
< asp:CheckBoxField DataField = " Approved "  HeaderText = " Approved "  SortExpression = " Approved "   />
None.gif                
< asp:BoundField DataField = " ParentPostID "  HeaderText = " ParentPostID "  SortExpression = " ParentPostID "   />
None.gif                
< asp:CheckBoxField DataField = " Closed "  HeaderText = " Closed "  SortExpression = " Closed "   />
None.gif            
</ Columns >
None.gif        
</ asp:GridView >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >
None.gif
但是显示是却总是出错,如下图所示:

我先Google了一下问题,大部分说的都可能是由于SqlDataReader读出的数据项为空值,但是单独取某一项的值时却没有问题,比如说下面的语句是可以正确得到返回值的。这里当然要提到Posts表的主键PostID为Int类型的,并且为自动增值的(每次+1)。
None.gif int  i  =  rdr.GetInt32( 0 );

我换了很多方法来读取还是没有解决问题,希望大家能够帮我一起解决问题。

PS:问题已经解决了,原因是存储过程中的有个字段在迁移的时候遗漏了,导致读取时类型不匹配,但是郁闷的是编译器总是提示第一个字段有错误,真的搞了好久才解决,还好是解决了。^_^

转载于:https://www.cnblogs.com/hustyangli/archive/2007/02/25/656190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值