数据库中有两个表,一个表A结构及数据如下id  name
1   x
2   xdfd
3   fdfd
.
.
n   fdklj另个表B结构如下bid   list         bname
1     1|2|3        woha
2     3|4|9|19     hawow
.
.
B表中list的意思就是包含A表中那些数据的ID,是通过 | 分开的
下面是.aspx.cs代码//这里已经获得B的ID,代码省略
public string AinBlist()
    {       //链接数据库省略
        SqlCommand cmd = new SqlCommand("SELECT  * FROM  A,B where  B.bid='" + ID + "' ", objConnection);
        SqlDataReader dr = cmd.ExecuteReader();
        string strBody = null;
        while (dr.Read())
        {
            strBody += "" + dr["name"] + "";
        }
        //关闭,省略
    }在.aspx页面中代码<%=AinBlist()%>我想通过上面实现的是,当页面读出B表的一条记录的时候,<%=AinBlist()%>显示的是B表该记录的list里面,对应的A表中相关的名称,请问这里的条件语句该怎么写呢?谢谢

解决方案 »

  1.   

    条件语句是说SQL语句是不?
    查询出list的内容,再以|分开,再select name from A where id in("+list里的ID+")
      

  2.   

    我只会用2条语句实现,一条查出list 字段 如3 ¦4 ¦9 ¦19 ,再用后台代码按'¦' 截取数组在循环取出id  对应的name值 
      

  3.   

    或者试试,id是数字型,list是字符型,我没试过好使不select name from A where id in(select list from B where bid= 你取的值 )
      

  4.   


    //这里已经获得B的ID,代码省略 
    public string AinBlist() 
        {        //链接数据库省略 
            SqlCommand cmd = new SqlCommand("select list from b where bid=" + id, objConnection);
            string aIDList = Convert.ToString(cmd.ExecuteScalar());//取出B表中对应1|2|3|4...的值
            aIDList = aIDList.Replace("|", ",");//将|符号换成,号
            cmd.CommandText = "select name from A where id in (" + aIDList + ") order by id";
            SqlDataReader dr = cmd.ExecuteReader();
            StringBuilder strBody = "";//用stringBuidler性能好些,因为你不知道blist里面有多少aid,若数据量大的话,这个比string性能好的多
            while (dr.Read())
            {
                strBody.Append(dr["name"].ToString());
            }
            //关闭,省略 
        } 
      

  5.   

    那怎么统计B表中一条数据的list里面ID个数呢?
      

  6.   

    晕。我写下代码就有三人回复了,呵呵。没抢住沙发回三楼那样是不行的,数据类型不一致。二楼还要处理数组没必要了,可以直接replace替换一楼在SQL语句in里面没处理字符|,呵,结贴给分:)
      

  7.   

    将 nvarchar 值 '1| 30' 转换为数据类型为 int 的列时发生语法错误。 提示这样的错误,因为我的这里的list是nvarchar,谢谢我的条件语句是这样写的SELECT  * FROM A where id in(select list from B where bid= '" + Id + "' ) and myid=0 order by id asc
      

  8.   

    那怎么统计B表中一条数据的list里面ID个数呢?
      

  9.   

    你按我4楼的代码写会有问题???要得到ID个数,有两种方式,若B表list中的ID在A表中都存在的话,在while(dr.read())里面加个变量进行统计就行了
    第二种方式就是
    string[] a = aIDList.Split(",");
    int idCount = a.Length;//得到ID总数
      

  10.   

    B表中LIST在A中肯定存在,能给个代码吗?
    被这两个联合在一起的问题,搞得有点蒙
      

  11.   

    那个问题我已经解决了,我想请您帮忙
    写下对list中ID统计的下面你说的方法,我不晓得该怎么做要得到ID个数,有两种方式,若B表list中的ID在A表中都存在的话,在while(dr.read())里面加个变量进行统计就行了
    第二种方式就是
    string[] a = aIDList.Split(",");
    int idCount = a.Length;//得到ID总数 谢谢,,马上结贴
      

  12.   

    我把两种方式的代码全贴给你1、//这里已经获得B的ID,代码省略 
    public string AinBlist() 
        {         //链接数据库省略 
            SqlCommand cmd = new SqlCommand("select list from b where bid=" + id, objConnection);
            string aIDList = Convert.ToString(cmd.ExecuteScalar());//取出B表中对应1|2|3|4...的值
            aIDList = aIDList.Replace("|", ",");//将|符号换成,号
           int idCount = aIDList.Split(",").Length;//得到ID总数 存入idCount
            cmd.CommandText = "select name from A where id in (" + aIDList + ") order by id";
            SqlDataReader dr = cmd.ExecuteReader();
            StringBuilder strBody = "";//用stringBuidler性能好些,因为你不知道blist里面有多少aid,若数据量大的话,这个比string性能好的多
            while (dr.Read())
            {
                strBody.Append(dr["name"].ToString());
            }
            //关闭,省略 
        } 2、//这里已经获得B的ID,代码省略 
    public string AinBlist() 
        {         //链接数据库省略 
            SqlCommand cmd = new SqlCommand("select list from b where bid=" + id, objConnection);
            string aIDList = Convert.ToString(cmd.ExecuteScalar());//取出B表中对应1|2|3|4...的值
            aIDList = aIDList.Replace("|", ",");//将|符号换成,号
             int idCount=0;//ID总数初始值为0
            cmd.CommandText = "select name from A where id in (" + aIDList + ") order by id";
            SqlDataReader dr = cmd.ExecuteReader();
            StringBuilder strBody = "";//用stringBuidler性能好些,因为你不知道blist里面有多少aid,若数据量大的话,这个比string性能好的多
            while (dr.Read())
            {
                strBody.Append(dr["name"].ToString());
                idCount+=1;//统计一共多少
            }
            //关闭,省略 
        }