我用DataGrid绑定数据的时候,其中有一个是类型ID,我想显示类型名称[在另外一个表]
    protected void ShowTypeName()
    {
        string showSql = "Select * From AD";
        OleDbCommand myCommand = new OleDbCommand(showSql, myConnection);
        myConnection.Open();
        OleDbDataReader dr = myCommand.ExecuteReader();
        while (dr.Read())
        {
            string TypeSql = "Select * From Type Where id = " + dr["Typeid"] + "";
            OleDbCommand myCommand1 = new OleDbCommand(TypeSql, myConnection);
            OleDbDataReader dr1 = myCommand1.ExecuteReader();
            Response.Write(dr["Type"]);
            if (dr1.Read())
            {
                Response.Write(dr1["TypeName"]);
            }
        }
        myConnection.Close();
    }
当我用这个方法来显示类型名称的时候,所有的名称都出来了,怎么才可以显示每条记录对应的类型名称呢

解决方案 »

  1.   

    SQL语句用左联接。直接查个完整的数据集。这样效率也高
    select a.id,b.name from table a left join table b on a.id = b.id
      

  2.   

    根据你的要求,大致是这样    protected void ShowTypeName()
        {
            string showSql = "Select ad.Typeid, ad.[type], [type].typeName From AD inner join [Type] on ad.typeid = [type].typeid";
            OleDbCommand myCommand = new OleDbCommand(showSql, myConnection);
            myConnection.Open();
            OleDbDataReader dr = myCommand.ExecuteReader();
            while (dr.Read())
            {
                Response.Write(dr["Type"].ToString() + " " + dr["TypeName"].ToString() + "<br />");
            }
            myConnection.Close();
        }
      

  3.   

    System.Data.OleDb.OleDbException: 不支持连接表达式
    应该怎么修改?
      

  4.   

    TO高歌:我那两个表的字段ID的类型不一样,出现表达式中的类型不匹配这样的错误,我不想该ACCESS里的类型,那我怎么做呢
      

  5.   

    那你这两个表关联的字段是不是typeid呢?
      

  6.   

    Type 里的ID是和AD里的typeid一样,不过类型不一样 
      

  7.   

    通过select语句转换其中一个typeid的类型,然后关联检索结果到另一个表
      

  8.   


    Select a.Typeid, a.[type], [type].typeName From (select convert(int,typeid) as typeid,[type] from AD) as a inner join [Type] on ad.typeid = [type].typeid
      

  9.   

    我修改了类型,OK了
    不过还是把所有的类型名称给打印出来了,我这个问题的意思,一个DATAGRID绑定了的,有一列是类型ID显示的,我想把这一列用类型名称给显示出来。
      

  10.   

    Select ad.Typeid, ad.[type], [type].typeName From AD inner join [Type] on ad.typeid = [type].typeid
    =====
    假设你的ad表的typeid是文本类型Select ad.Typeid, ad.[type], [type].typeName From AD , [Type] on ad.typeid = cstr([type].typeid)如果是其他类型,请参考以下类型转换函数
      cbool(expression)   
        
      cbyte(expression)   
        
      ccur(expression)   
        
      cdate(expression)   
        
      cdbl(expression)   
        
      cdec(expression)   
        
      cint(expression)   
        
      clng(expression)   
        
      csng(expression)   
        
      cstr(expression)   
        
      cvar(expression)   
        
      cstr(expression)
      

  11.   

    在你的例子中我没见到有什么DataGrid啊
      

  12.   

            string mySql = "Select * From AD ";
            myConnection = new OleDbConnection(connectionString);
            OleDbDataAdapter da = new OleDbDataAdapter(mySql, myConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataGrid1.DataSource = ds.Tables[0];
            DataGrid1.DataBind();首先感谢大家热心的帮助
    我是先绑定它,后来在AD里的类型ID我想通过Type表来显示对应的类型名称,也就是自己写个 ShowTypeName()方法,我表达能力不是很好,请见谅
      

  13.   

    你这样做应该是没有必要的,因为你等于先去了一次数据库得到数据,再去一次数据库去找他的TypeName,实际上应该是第一次就顺便搞定的,你贴贴你的aspx页面的代码看看
      

  14.   

    <asp:DataGrid runat="server" ID="DataGrid1" AutoGenerateColumns="false" AllowPaging="true"
                                            OnItemDataBound="changeRowColor" PageSize="8" OnPageIndexChanged="NewPage">
                                            <Columns>
                                                <asp:TemplateColumn>
                                                    <HeaderTemplate>
                                                        <table width="500px" style="text-align: center;">
                                                            <tr>
                                                                <td style="width: 30px;">
                                                                    ID</td>
                                                                <td style="width: 120px;">
                                                                    文章类别</td>
                                                                <td style="width: 150px;">
                                                                    文章标题</td>
                                                                <td style="width: 50px;">
                                                                    点击率</td>
                                                                <td style="width: 50px;">
                                                                    是否推荐</td>
                                                                <td style="width: 100px;">
                                                                    操作</td>
                                                            </tr>
                                                        </table>
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <table width="500px" style="text-align: center;">
                                                            <tr>
                                                                <td style="width: 30px;">
                                                                    <input type="checkbox" name="articleid" value='<%# DataBinder.Eval(Container.DataItem, "ID") %>' /></td>
                                                                <td style="width: 120px;">
                                                                    <%ShowTypeName();%>
                                                                </td>
                                                                <td style="width: 150px;">
                                                                    <a href="articleShow.aspx?ID=<%#Eval("ID") %>" target="_blank">
                                                                        <%#Eval("Title") %>
                                                                    </a>
                                                                </td>
                                                                <td style="width: 50px;">
                                                                    <%#Eval("Hit") %>
                                                                </td>
                                                                <td style="width: 50px;">
                                                                    <%#Eval("Commend") %>
                                                                </td>
                                                                <td style="width: 100px;">
                                                                    <a href="Article_deal.aspx?action=ArticleEdit&amp;ID=<%#Eval("ID") %>">修改</a>&nbsp;|&nbsp;<a
                                                                        href="Article_deal.aspx?action=ArticleDel&amp;ID=<%#Eval("ID") %>" onclick="return confirm('确定删除')">删除</a></td>
                                                            </tr>
                                                        </table>
                                                    </ItemTemplate>
                                                </asp:TemplateColumn>
                                            </Columns>
                                            <PagerStyle Mode="NumericPages" />
                                        </asp:DataGrid>
    哦 一次搞定,那 da.Fill(ds);DataGrid1.DataSource = ds.Tables[0];
            DataGrid1.DataBind();就有错误,应该怎么修改呢