简体   繁体   English

ADO.net如何使用C#和连接的模型在页面加载时将特定数据库表单元格中的数据绑定到转发器控件上?

[英]ADO.net how to bind data from specific database table cell to a repeater control on page load using C# & the connected model?

I want to bind the data from an oracle database cell to a bunch of labels within a repeater control, using C#, ADO.NET and the connected model. 我想使用C#,ADO.NET和连接的模型将oracle数据库单元中的数据绑定到转发器控件中的一堆标签。 The display page repeater goes something like this (this is just one eval for simplicity): 显示页面中继器是这样的(为简单起见,这只是一个评估):

<asp:Repeater ID="rptMain" runat="server" >
   <ItemTemplate>
     <h3 id="contactUs"> <%#Eval("ppCustSurvey")%> %></h3>
   </ItemTemplate>
</asp:Repeater>

Now the property string "ppCustSurvey" is a property that in my property class called ppContent. 现在,属性字符串“ ppCustSurvey”是在我的属性类ppContent中的属性。 In my code behind page I have this as part of the page load 在页面后面的代码中,我将此作为页面加载的一部分

if (!Page.IsPostBack)
{
    clsContent objCon = new clsContent();
    rptMain.DataSource = objCon.getContent();
    rptMain.DataBind();
}

And the getContent() method points to this 而getContent()方法指向此

public class clsContent
{
    static readonly string _strConn;

    static clsContent()
    {
        _strConn = WebConfigurationManager.ConnectionStrings["MyDilbert_Nov30"].ConnectionString;
    }
    public List<ppContent> getContent()
    {
        List<ppContent> objAllContent = new List<ppContent>();
        OracleConnection conn = new OracleConnection(_strConn);
        try
        {
            conn.Open();
            string strCmd = "Select site_content from content";
            OracleCommand cmd = new OracleCommand(strCmd, conn);
            OracleDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ppContent objCon = new ppContent();
                objCon.ppCustSurvey = (dr["site_content"].ToString());
            }
            return objAllContent;
        }
        catch(Exception)
        {
            objAllContent.Clear();
            return objAllContent;
        }
        finally{
            conn.Close();
        }
    }
}

Now I think that I need to pass the parameter of the id of the column in order to identify ppCustSurvey as belonging to the column with the PK of 1. How do I do this? 现在,我认为我需要传递列id的参数,以将ppCustSurvey标识为PK等于1的列。我该如何做?

I will post my solution for anyone who finds it useful. 我会将解决方案发布给任何认为有用的人。 I solved the issue by adding this code to my class: 我通过将以下代码添加到我的课程中解决了该问题:

    public List<ppControls> getControls(int _id)
{
    List<ppControls> objAllControls = new List<ppControls>();
    OracleConnection conn = new OracleConnection(_strConn);
    OracleCommand cmd = new OracleCommand("SELECT * FROM controls WHERE reference =: parID", conn);
    cmd.Parameters.AddWithValue(":parID", _id);
    try
    {
        conn.Open();
        //string strCmd = parSelect;
        //OracleCommand cmd = new OracleCommand(strCmd, conn);
        OracleDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {                objCon.ppCustSurvey = dr["controls_content"].ToString();}
        return objAllControls;
    }

Then in the code behind I pass the parameter of the foreign key like this 然后在后面的代码中,我像这样传递外键的参数

        rptMain.DataSource = objCon.getControls(7);
        rptMain.DataBind();

This is done for each repeater. 这是针对每个中继器完成的。 I created a properties class with a get and set for ppCustSurvey, and assigned the property name to the Eval function of the repeater on the display page. 我创建了一个带有get和set的ppCustSurvey属性类,并在显示页面上将属性名称分配给了转发器的Eval函数。 Anyhow, that's how I solved it. 无论如何,这就是我解决的方法。 Now I am curious about what way you would do this to lessen the hammering on the server, but that's a topic for another question. 现在,我很好奇您将以何种方式减轻服务器上的负担,但这是另一个问题。

If you are going to be gathering a lot of data from the database into a series of labels like this, I suspect that your DBAs could question the repeated hammering on the database (ie opening a connection and performing the select statement for each label each time the page is rendered), especially if this is a high-traffic site. 如果您打算从数据库中将大量数据收集到一系列这样的标签中,那么我怀疑您的DBA可能会对重复敲打数据库提出质疑(即每次打开连接并为每个标签执行select语句)页面呈现),尤其是在这是一个人流量大的网站时。

In cases like this, we usually fetch all of the necessary data from the database in one fell swoop and dynamically generate the required HTML in code-behind. 在这种情况下,我们通常会一口气从数据库中获取所有必要的数据,并在后台代码中动态生成所需的HTML。 This might take a little more work up front, but it should be much faster and have a lot less impact on the database. 这可能需要更多的工作,但是它应该更快,并且对数据库的影响也要小得多。

If you want to stick with the current design, we would need to see how the PK for the column is defined in the client side, but it could be as easy as just adding a parameter to your ppCustSurvey method. 如果您要坚持当前的设计,我们将需要查看如何在客户端定义列的PK,但这就像在ppCustSurvey方法中添加参数一样简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#从Excel到数据库ADO.net的导入数据中选择空格表 - select spacific sheet in import data from Excel to database ADO.net using C# 使用ADO.net将数据从Azure数据库添加到下拉列表中C# - Adding data to dropdown list from Azure database using ADO.net c# 如何使用ADO.NET在C#中从SQL Server检索数据? - How to retrieve data from SQL Server in C# using ADO.NET? 通过C#中的OPC UA客户端插入具有ADO.NET实体数据模型的数据库 - Insert to Database with ADO.NET Entity Data Model through OPC UA Client in C# 将数据从ADO.NET绑定到WPF - Bind data from ADO.NET to WPF 如何通过Ado.net使用MVC从数据库中绑定多个下拉列表 - How to Bind mulitple dropdownlist from database using MVC via Ado.net 如何使用C#winform与带有ADO.NET实体的SQL Server 2008建立远程连接 - How to make a remote connection with SQL Server 2008 with ADO.NET entity Data model from C# winform 如何使用ADO.Net C#将null插入到sql数据库中 - how to insert null into sql database with ADO.Net C# 如何在C#中使用ODBC读取特定Excel工作表中的所有数据行到ADO.NET数据表中 - How to read all data rows in a specific Excel sheet into an ADO.NET Datatable with out using ODBC in C# 如何以编程方式从ADO.Net实体模型创建数据库? - How to programatically create a database from an ADO.Net Entity Model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM