当前位置: 代码迷 >> .NET报表 >> 代码连接数据库以后,如何显示字段值
  详细解决方案

代码连接数据库以后,如何显示字段值

热度:236   发布时间:2016-05-05 01:36:49.0
代码连接数据库以后,怎么显示字段值?
我用代码连接数据库以后,
想把数据库里的字段,
帮定到水晶报表里显示。
下面的代码怎么改?




                    ReportDocument myReport = new ReportDocument();
                   
                    string reportPath2 = "D:\\vs2010\\Hosting_Manager\\Hosting_Manager\\Hosting_Manager\\company_list.rpt";
 
                    myReport.Load(reportPath2);

                    //绑定数据集,注意,一个报表用一个数据集。
                    myReport.SetDataSource(ds);
                    this.crystalReportViewer1.ReportSource = myReport;

                    myReport.DataDefinition.FormulaFields["UnboundString1"].Text = "{enterprise.ent_name}";
                    myReport.DataDefinition.FormulaFields["UnboundString1.title"].Text = "'得分情况'";



------解决思路----------------------
http://blog.sina.com.cn/s/blog_4c495b5f0100t2z5.html
------解决思路----------------------
<asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333" 
                        Width="438px" >
                        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                        <SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                        <ItemTemplate>
                            <table>
                                <tr>
                                    <td style="text-align: left">
                                        商品类别编号:<%# DataBinder.Eval(Container.DataItem, "GoodsTypeID")%></td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:DataList ID="DataList2" runat="server" 
                                            DataSource='<%#((System.Data.DataRowView)Container.DataItem).CreateChildView("tableRelation") %>' 
                                            DataKeyField="GoodsTypeID" Width="433px" >
                                            <ItemTemplate>
                                                <table style="width: 520px; "  cellpadding="0" cellspacing="0" 
                                                    border="1">
            <tr>
                <td style="width: 84px">
                    <asp:Label ID="Label3" runat="server" Font-Size="9pt" Text="商品名称:" Width="81px"></asp:Label>
                </td>
                <td >
                    <asp:Label ID="labName" runat="server" Font-Size="9pt" 
                        Text='<%# DataBinder.Eval(Container.DataItem,"GoodsName") %>'></asp:Label>
                </td>
                <td >
                    <asp:Label ID="Label5" runat="server" Font-Size="9pt" Text="类别名称:" Width="81px"></asp:Label>
                </td>
                <td style="width: 99px">
                    <asp:Label ID="labTypeName" runat="server" Font-Size="9pt" 
                        Text='<%# DataBinder.Eval(Container.DataItem,"GoodsTypeName") %>'></asp:Label>
                </td>
            </tr>
            <tr>
                 <td style="width: 84px;">
                     <asp:Label ID="Label9" runat="server" Font-Size="9pt" Text="商品价格:" Width="94px"></asp:Label>
                 </td>
                <td >
                    <asp:Label ID="labPrice" runat="server" Font-Size="9pt" 
                        Text='<%# DataBinder.Eval(Container.DataItem,"GoodsPrice") %>'></asp:Label>
                 </td>
                <td >
                    <asp:Label ID="Label11" runat="server" Font-Size="9pt" Text="商品简介:" 
                        Width="90px"></asp:Label>
                 </td>
                <td style="width: 99px;">
                    <asp:Label ID="labIntroduce" runat="server" Font-Size="9pt" 
                        Text='<%# DataBinder.Eval(Container.DataItem,"GoodsIntroduce") %>'></asp:Label>
                 </td>
            </tr>
            
       
        </table>
                                            </ItemTemplate>
                                        </asp:DataList></td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <AlternatingItemStyle BackColor="White" />
                        <ItemStyle BackColor="#E3EAEB" />
                        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    </asp:DataList>用DataList控件编辑绑定显示数据库中商品信息
二、处理程序
public partial class Default : System.Web.UI.Page
{

    SqlConnection sqlcon;
    string strCon = ConfigurationManager.AppSettings["conStr"];
    protected void Page_Load(object sender, EventArgs e)
    {
        string sqlstr = "select top 3 * from tb_GoodsType select b.GoodsTypeID,b.GoodsName,a.GoodsTypeName,b.GoodsIntroduce,b.GoodsPrice,b.GoodsIsNew from tb_GoodsType as a inner join tb_GoodsInfo1 as b on a.GoodsTypeID=b.GoodsTypeID";
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds);
        myds.Tables[0].TableName = "tb_GoodsInfo";
        myds.Tables[1].TableName = "tb_GoodsType";
        DataColumn Parent = myds.Tables["tb_GoodsInfo"].Columns["GoodsTypeID"];
        DataColumn Child = myds.Tables["tb_GoodsType"].Columns["GoodsTypeID"];
        DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child, false);
        myds.Relations.Add(tableRelation);
        DataList1.DataSource = myds.Tables["tb_GoodsInfo"].DefaultView;
        DataList1.DataBind();
        sqlcon.Close();
    }
  相关解决方案