异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html

后期会在博客首发更新:http://dnt.dkill.net/Article/Detail/312

今天一同志问我这个问题,这个是过程还原:

调用SQLHelper的时候发现输出参数没值了???

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

不用sqlhelper也是没有?神马情况?

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

用sqldataadapter却可以?

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

吓死宝宝了,赶紧看看啥情况。先换种方法看看

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

,,,我去,可以哇!那么是不是ExecuteReader容易有啥坑的问题呢?想了想,对头,是不是返回的reader有点问题?

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

先关了看看~我去,有值了!不会这么容易就搞定了吧?不行,得再试试

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

~~的确,reader没关闭,那输出参数就没值(其实也可以理解~reader说:本大王还没读取完呢,你丫急什么,给我等着)

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

不能就这样算了啊,我不能总自己写吧,sqlhelper的问题还是得解决

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

我去,还是没有。。。。。。。这可不行,看看sqlhelper的源码怎么搞的

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

万恶的清除啊!再试试

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

居然可以了,ok,收工了?来来来,我们继续看看

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

SQLHelper怎么写的?

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

额,经常听前辈说SqlDataAdapter是个神奇的东西,果然...

 

扩:一般很少直接返回SqlDataReader对象的,

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

贴一个比较弱的转换(有更好的可以贴评论中的,我就先抛个砖头)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public static IEnumerable<T> SqlDataReaderToList<T>(SqlDataReader reader) where T : new()
        {
            using (reader)
            {
                if (reader.HasRows)
                {
                    IList<T> list = new List<T>();
                    while (reader.Read())
                    {
                        T t = new T();
                        PropertyInfo[] propertys = t.GetType().GetProperties();
                        foreach (PropertyInfo pi in propertys)
                        {
                            string tempName = pi.Name;
                            //设置 RowFilter
                            reader.GetSchemaTable().DefaultView.RowFilter = string.Format("ColumnName='{0}'", tempName);
                            //判断SqlDataReader是否存在某列
                            if (reader.GetSchemaTable().DefaultView.Count > 0)
                            {
                                if (!pi.CanWrite)
                                {
                                    continue;
                                }
                                var value = reader[tempName];
                                if (value != DBNull.Value)
                                {
                                    pi.SetValue(t, value, null);
                                }
                            }
                        }
                        list.Add(t);
                    }
                    return list;
                }
            }
            return null;
        }

 

 


本文转自毒逆天博客园博客,原文链接:http://www.cnblogs.com/dunitian/p/5362528.html,如需转载请自行联系原作者


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
猜你喜欢
  • 2022-12-23
  • 2021-08-14
  • 2021-08-14
  • 2019-11-22
  • 2021-09-21
  • 2021-10-15
相关资源
相似解决方案