我已经编写了这段代码来从数据库中读取:
SqlDataReader DR1 = Cmd.ExecuteReader();
if (DR1.Read))
{
Textbox1.text =DR1.GetValue(0).ToString();
TextBox2.text = DR1.GetValue(1).ToString();
}
这将显示表格中的第一个行。如何继续遍历表并存储/显示所有值?
答案 0 :(得分:0)
您需要为SqlDataReader添加while循环。您可能希望使用DataTable或List集合来存储数据行。
SqlDataReader DR1 = Cmd.ExecuteReader();
while(DR1.Read()) // Will read all records
{
// This is only useful for a single interation
Textbox1.text =DR1.GetValue(0).ToString();
TextBox2.text = DR1.GetValue(1).ToString();
}
答案 1 :(得分:0)
你需要一个while循环来读表
SqlDataReader DR1 = Cmd.ExecuteReader();
while(DR1.Read()) // While : read all records
{
// Your code here
}
答案 2 :(得分:0)
本守则运作良好
非常感谢(:
SqlConnection con = new SqlConnection("Data Source");
DataSet dsa = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select where BayNumber='" + comboBox1.Text.Trim() + "';", con);
da.Fill(dsa);
for (int i = 0; i <= 8; i++)
{
for (int k = 0; k <= 8; k++)
{
textBox1.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
label2.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
textBox2.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
label4.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
textBox3.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
label6.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
textBox4.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
label8.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
textBox5.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
label10.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
textBox6.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
label12.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
textBox7.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
label14.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
textBox8.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
label16.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
}
}
由于