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

发布于 2024-12-20 11:42:22 字数 1697 浏览 1 评论 0原文

我想使用 C#、ADO.NET 和连接模型将 Oracle 数据库单元中的数据绑定到中继器控件内的一堆标签。显示页面重复器是这样的(为了简单起见,这只是一个评估):

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

现在,属性字符串“ppCustSurvey”是我的属性类中名为 ppContent 的一个属性。在我的代码隐藏页面中,我将此作为页面加载的一部分

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

并且 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();
        }
    }
}

现在我认为我需要传递列 id 的参数,以便将 ppCustSurvey 识别为属于该列1.我该怎么做?

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. 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. 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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦妄 2024-12-27 11:42:22

我会将我的解决方案发布给任何认为有用的人。我通过将以下代码添加到我的类中解决了这个问题:

    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;
    }

然后在后面的代码中,我像这样传递外键的参数

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

这是为每个中继器完成的。我为 ppCustSurvey 创建了一个带有 get 和 set 的属性类,并将属性名称分配给显示页面上中继器的 Eval 函数。无论如何,我就是这样解决的。现在我很好奇你会采取什么方式来减少对服务器的打击,但这是另一个问题的主题。

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. 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.

半世晨晓 2024-12-27 11:42:22

如果您要将数据库中的大量数据收集到一系列像这样的标签中,我怀疑您的 DBA 可能会质疑对数据库的重复敲击(即每次打开一个连接并为每个标签执行 select 语句)页面已呈现),特别是如果这是一个高流量站点。

在这种情况下,我们通常会一次性从数据库中获取所有必要的数据,并在代码隐藏中动态生成所需的 HTML。这可能需要预先做更多的工作,但它应该更快并且对数据库的影响更小。

如果您想坚持当前的设计,我们需要了解如何在客户端定义列的 PK,但这可能很简单,只需向 ppCustSurvey 方法添加一个参数即可。

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 (i.e. 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.

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文