当前位置:编程学习 > C#/ASP.NET >>

求大神解说解说!不明白为什么数据库只有一条数据时就可以执行,但当数据库有两条数据时就出错了

public static List<OutRecord> LoadOutRecordList(string name)
        {
            OleDbConnection conn = new OleDbConnection(_ConString);
            try
            {
                string cmdTxt = "select [支出编号],[姓名],[支出时间],[记录时间],[支出方式]," + 
                                       "[支出类型],[金额],[银行卡号],[支出说明] from [支出记录] where [姓名]= '" + name + "'";
                OleDbCommand cmd = new OleDbCommand(cmdTxt,conn);
                conn.Open();
                List<OutRecord> outRecordList = new List<OutRecord>();
                OleDbDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    OutRecord outRecord = new OutRecord();
                    outRecord.ID = dr.GetInt32(0);
                    outRecord.OutTime = dr.GetDateTime(2);
                    outRecord.RecordTime = dr.GetDateTime(3);
                    outRecord.OutType = dr.GetString(4);
                    outRecord.OutUsage = dr.GetString(5);
                    outRecord.Amount = dr.GetDecimal(6);
                    outRecord.BankCard = dr.GetString(7);
                    outRecord.Descrption = dr.GetString(8);
                    outRecordList.Add(outRecord);//执行第二条记录时,跳过这条语句,跳到catch语句去了,不明白
                }
                return outRecordList;
            }
            catch(Exception)
            {
                return null;
            }
            finally
            {
                //关闭连接
                conn.Close();
            }
        } --------------------编程问答-------------------- 检查数据是否有null值的情况,最好加判断,不要直接GetString、GetDateTime… --------------------编程问答-------------------- 谢谢,真的是空值问题,但是我还是不明白!为什么一条记录中有一个空值没有什么问题,但是两条记录(每一条都有一列是空值)有空值就出问题?
--------------------编程问答-------------------- dr.GetString可以空值,GetDateTime如果不是DateTime?则不行。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,