从数据库中逐行将数据写入文本框

Posted

技术标签:

【中文标题】从数据库中逐行将数据写入文本框【英文标题】:Write data to textbox row by row from database 【发布时间】:2015-04-15 04:29:40 【问题描述】:

我想在表格中逐行显示数据库中的数据

如何将数据库中的数据逐行写入每个文本框

(即)日期活动1,活动2等然后下一行日期活动1,活动2等。 如何编写正确的循环

我尝试获取第一行数据的代码:

private void GetActivity()
    
        try
        
            CON = new OracleConnection(Connection);
            CON.Open();
            OracleCommand COMMAND1 = new OracleCommand("Select ACTIVITYID,to_char(ACTIVITYDATE,'DD-MON-YYYY') as ACTIVITYDATE,TITLE,STARTTIME,ENDTIME,ACTIVITYTIME as TIMETAKEN from DAILY_ACTIVITIES1 where ACTIVITYDATE= (Select min(ACTIVITYDATE) from DAILY_ACTIVITIES1)", CON);

            OracleDataReader READER = COMMAND1.ExecuteReader();            
            int count = READER.FieldCount;

            if (READER.HasRows)
            
                while (READER.Read())
                
                    if (Date1TextBox.Text == "")
                    
                        Date1TextBox.Text = READER[1].ToString();
                        Date1TextBox.Enabled = false;
                    

                    if (D1A1TextBox.Text == "")
                    
                        D1A1TextBox.Text = "Title:" + READER[2].ToString() + "\n";
                        D1A1TextBox.Text += "Start-time:" + READER[3].ToString() + "\n";
                        D1A1TextBox.Text += "End-time:" + READER[4].ToString() + "\n";
                        D1A1TextBox.Text += "Timetaken:" + READER[5].ToString() + "\n";
                        D1A1TextBox.Enabled = false;
                    
                    else
                    
                        D1A1TextBox.Enabled = false;
                    

                    if (D1A2TextBox.Text == "")
                    
                        D1A2TextBox.Text = "Title:" + READER[2].ToString() + "\n";
                        D1A2TextBox.Text += "Start-time:" + READER[3].ToString() + "\n";
                        D1A2TextBox.Text += "End-time:" + READER[4].ToString() + "\n";
                        D1A2TextBox.Text += "Timetaken:" + READER[5].ToString() + "\n";
                        D1A2TextBox.Enabled = false;
                    
                    else
                    
                        D1A2TextBox.Enabled = false;
                    

                    if (D1A3TextBox.Text == "")
                    
                        D1A3TextBox.Text = "Title:" + READER[2].ToString() + "\n";
                        D1A3TextBox.Text += "Start-time:" + READER[3].ToString() + "\n";
                        D1A3TextBox.Text += "End-time:" + READER[4].ToString() + "\n";
                        D1A3TextBox.Text += "Timetaken:" + READER[5].ToString() + "\n";
                        D1A3TextBox.Enabled = false;
                    
                    else
                    
                        D1A3TextBox.Enabled = false;
                    

                    if (D1A4TextBox.Text == "")
                    
                        D1A4TextBox.Text = "Title:" + READER[2].ToString() + "\n";
                        D1A4TextBox.Text += "Start-time:" + READER[3].ToString() + "\n";
                        D1A4TextBox.Text += "End-time:" + READER[4].ToString() + "\n";
                        D1A4TextBox.Text += "Timetaken:" + READER[5].ToString() + "\n";
                        D1A4TextBox.Enabled = false;
                    
                    else
                    
                        D1A4TextBox.Enabled = false;
                    

                    if (D1A5TextBox.Text == "")
                    
                        D1A5TextBox.Text = "Title:" + READER[2].ToString() + "\n";
                        D1A5TextBox.Text += "Start-time:" + READER[3].ToString() + "\n";
                        D1A5TextBox.Text += "End-time:" + READER[4].ToString() + "\n";
                        D1A5TextBox.Text += "Timetaken:" + READER[5].ToString() + "\n";
                        D1A5TextBox.Enabled = false;
                    
                    else
                    
                        D1A5TextBox.Enabled = false;
                    

                    if (D1A6TextBox.Text == "")
                    
                        D1A6TextBox.Text = "Title:" + READER[2].ToString() + "\n";
                        D1A6TextBox.Text += "Start-time:" + READER[3].ToString() + "\n";
                        D1A6TextBox.Text += "End-time:" + READER[4].ToString() + "\n";
                        D1A6TextBox.Text += "Timetaken:" + READER[5].ToString() + "\n";
                        D1A6TextBox.Enabled = false;
                    
                    else
                    
                        D1A6TextBox.Enabled = false;
                    
                
                      
            CON.Close();
        

        catch (Exception et)
        
            et.ToString();
        
    

【问题讨论】:

kumartyr ,你从哪里得到 D1A1TextBox~D1A6TextBox? @AngusChung 这些都是放置在表格第一行的文本框,其中 multiline = true (即)突出显示的是 D1A3textbox 我知道,但是你怎么能直接在 GetActivity() 中使用它们呢? 我在我的 webform 的 Pageload 中调用了这个方法 好的,我明白你的问题,你想要一个循环来做吗? 【参考方案1】:

@kumartyr,根据你的需要,你不想用Gridview。

所以,我重写了 GetActivity() 函数以进行 动态文本框 访问。

private void GetActivity()

    try
    
        CON = new OracleConnection(Connection);
        CON.Open();
        OracleCommand COMMAND1 = new OracleCommand("Select ACTIVITYID,to_char(ACTIVITYDATE,'DD-MON-YYYY') as ACTIVITYDATE,TITLE,STARTTIME,ENDTIME,ACTIVITYTIME as TIMETAKEN from DAILY_ACTIVITIES1 where ACTIVITYDATE= (Select min(ACTIVITYDATE) from DAILY_ACTIVITIES1)", CON);

        OracleDataReader READER = COMMAND1.ExecuteReader();
        int count = READER.FieldCount;

        if (READER.HasRows)
        
            int i = 1;
            TextBox txtDate;
            TextBox txtDA;
            ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("MainContent");
            while (READER.Read() && i <= 6)
            
                if (i == 1)
                
                    txtDate = (TextBox)cph.FindControl(string.Format("Date0TextBox", i.ToString()));
                    txtDate.Text = READER[1].ToString();
                    txtDate.Enabled = false;
                

                txtDA = (TextBox)cph.FindControl(string.Format("D1A0TextBox", i.ToString()));
                if (txtDA.Text == "")
                
                    txtDA.Text = "Title:" + READER[2].ToString() + "\n";
                    txtDA.Text += "Start-time:" + READER[3].ToString() + "\n";
                    txtDA.Text += "End-time:" + READER[4].ToString() + "\n";
                    txtDA.Text += "Timetaken:" + READER[5].ToString() + "\n";
                
                txtDA.Enabled = false;
                i++;
            
        
        CON.Close();
    

    catch (Exception et)
    
        et.ToString();
    

【讨论】:

【参考方案2】:

使用 GridView 并将您的 OracleCommand 绑定为数据源。您可以自定义每个单元格以显示您想要的数据,而无需进行任何循环或手动添加数千个文本框。

以下是包含大量示例的文档: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview(v=vs.110).aspx

【讨论】:

【参考方案3】:

你在使用GridView吗?,你可以在RowDataBound事件中创建行模板并一次性绑定网格视图

这些是绑定 gridview 的简单方法。 this 就是一个很好的例子

【讨论】:

以上是关于从数据库中逐行将数据写入文本框的主要内容,如果未能解决你的问题,请参考以下文章

delphi中怎么逐行读取文本文件的数据并将每行分别写入指定的不同编辑框

Visual c++ 如何从富文本框中读取

将pyspark数据框写入文本文件

Objective C - 创建文本文件以在 Cocoa 中逐行读取和写入

当新数据写入富文本框时,它会滚动到底部

从另一个线程写入文本框? [复制]