从数据库逐行写入数据到文本框
Write data to textbox row by row from database
我想在 table
中逐行显示数据库中的数据
如何将数据从数据库写入每个文本框行
(即)日期 activity1、activity2 等,然后是下一行日期 activity1、activyt2 等。
如何编写正确的循环
我尝试获取第一行数据的代码:
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();
}
}
使用 GridView 并将 OracleCommand 绑定为数据源。您可以自定义每个单元格以显示您想要的数据,而不必进行任何循环或手动添加数千个文本框。
这是包含大量示例的文档:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview(v=vs.110).aspx
您在使用GridView吗?您可以在 RowDataBound 事件中一次性创建行模板并绑定网格视图
这些是绑定 gridview 的简单方法。 this就是一个很好的例子
@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("Date{0}TextBox", i.ToString()));
txtDate.Text = READER[1].ToString();
txtDate.Enabled = false;
}
txtDA = (TextBox)cph.FindControl(string.Format("D1A{0}TextBox", 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();
}
}
我想在 table
中逐行显示数据库中的数据如何将数据从数据库写入每个文本框行
(即)日期 activity1、activity2 等,然后是下一行日期 activity1、activyt2 等。 如何编写正确的循环
我尝试获取第一行数据的代码:
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();
}
}
使用 GridView 并将 OracleCommand 绑定为数据源。您可以自定义每个单元格以显示您想要的数据,而不必进行任何循环或手动添加数千个文本框。
这是包含大量示例的文档: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview(v=vs.110).aspx
您在使用GridView吗?您可以在 RowDataBound 事件中一次性创建行模板并绑定网格视图
这些是绑定 gridview 的简单方法。 this就是一个很好的例子
@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("Date{0}TextBox", i.ToString()));
txtDate.Text = READER[1].ToString();
txtDate.Enabled = false;
}
txtDA = (TextBox)cph.FindControl(string.Format("D1A{0}TextBox", 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();
}
}