数据库中有两个表,一个表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 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表中相关的名称,请问这里的条件语句该怎么写呢?谢谢
查询出list的内容,再以|分开,再select name from A where id in("+list里的ID+")
//这里已经获得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());
}
//关闭,省略
}
第二种方式就是
string[] a = aIDList.Split(",");
int idCount = a.Length;//得到ID总数
被这两个联合在一起的问题,搞得有点蒙
写下对list中ID统计的下面你说的方法,我不晓得该怎么做要得到ID个数,有两种方式,若B表list中的ID在A表中都存在的话,在while(dr.read())里面加个变量进行统计就行了
第二种方式就是
string[] a = aIDList.Split(",");
int idCount = a.Length;//得到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;//统计一共多少
}
//关闭,省略
}