日期:2014-05-17  浏览次数:20759 次

关于某列不属于表Table的问题
遇到个棘手的问题,在功能中数据层有个方法是返回一个泛型的方法,发布后在IIS中测试,调用10多次都没有问题,过后就出现这个问题,感觉极为不稳定,然后这个方法一直过不去,要重启IIS才行,报错信息如下:
异常详细信息: System.ArgumentException: 列“Props_TypeName”不属于表 Table。
我把这个方法贴出来。
C# code

 public static List<Idol_PropsPurchases> GetPropsBagInfo(int BagId, int Idol_Id)
        {
            List<Idol_PropsPurchases> Lipp = new List<Idol_PropsPurchases>();
            string Sql = "select (select dbo.Props_GetParentName(Pt_Id) from Props_Info where Pi_Id = Ipp.Pi_Id) as Props_TypeName,(select Pi_SmallImg from Props_Info where Pi_Id = Ipp.Pi_Id) as Pi_SmallImg,* from Idol_PropsPurchases Ipp where datediff(s,convert(varchar(20),getdate(),120),Ipp_EndTime)>0 and Ipp_IsUse = 0 and Bag_Id="+BagId+" and Idol_Id="+Idol_Id+"";
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MemberDBCS"].ConnectionString);
            con.Open();
            SqlDataAdapter Sda = new SqlDataAdapter(Sql, con);
            DataSet Ds = new DataSet();//;SqlHelper.GetDataTable(Sql)
            Sda.Fill(Ds);
            DataTable Dt = Ds.Tables[0];
            con.Close();
                if (Dt.Rows.Count != 0)
                {
                    foreach (DataRow Row in Dt.Rows)
                    {
                        Idol_PropsPurchases Ipp = new Idol_PropsPurchases();
                        Ipp.Props_TypeName = Row["Props_TypeName"].ToString();
                        Ipp.Pi_SmallImg = Row["Pi_SmallImg"].ToString();
                        Ipp.Pi_Id = int.Parse(Row["Pi_Id"].ToString());
                        Ipp.Ipp_AccumulationCount = int.Parse(Row["Ipp_AccumulationCount"].ToString());
                        Ipp.Ipp_PositionNum = int.Parse(Row["Ipp_PositionNum"].ToString());
                        Ipp.Ipp_IsProps = bool.Parse(Row["Ipp_IsProps"].ToString());
                        Lipp.Add(Ipp);
                    }
                }
            
            
            return Lipp;
        }



------解决方案--------------------
先看下SQL運行中是否存在這個表,,,
確認到底返回數據集有幾個,需要的返回集是否在 Ds.Tables[0];

------解决方案--------------------
select (select dbo.Props_GetParentName(Pt_Id) from Props_Info where Pi_Id = Ipp.Pi_Id) as Props_TypeName,(select Pi_SmallImg from Props_Info where Pi_Id = Ipp.Pi_Id) as Pi_SmallImg,* from Idol_PropsPurchases Ipp where datediff(s,convert(varchar(20),getdate(),120),Ipp_EndTime)>0 and Ipp_IsUse = 0 and Bag_Id="+BagId+" and Idol_Id="+Idol_Id+"";


楼主改为inner join 方式试试吧。
select dbo.Props_GetParentName(Props_Info.Pt_Id) as Props_TypeName, Props_Info. Pi_SmallImg as
Pi_SmallImg, Ipp.* from Idol_PropsPurchases Ipp 
inner join Props_Info on Props_Info. Pi_Id = Ipp . Pi_Id
where datediff(s,convert(varchar(20),getdate(),120),Ipp_EndTime)>0 and Ipp_IsUse = 0 and Bag_Id="+BagId+" and Idol_Id="+Idol_Id+"";

这样可以少查询一次。 另外最好使用sqlParameter,省的sql注入。
另外注意写法,
using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MemberDBCS"].ConnectionString))
{
}

或者try {} catch{} finnaly{}


------解决方案--------------------
看了你的代码 有两个方面的和我见得不一样 

一是:不使用静态的方法
二是:在查询数据库是使用的DataReader直接循环读取里面的值构建泛型对象

不妨你试一哈



个人意见 仅供参考 绿绦工作室
------解决方案--------------------
把你的sql语句 换成存储过程