我需要返回Test列中的所有值,但不知何故只返回第一行值...
我试图添加dr2.NextResult();代码,但没有帮助
SqlCommand command = conn.CreateCommand();
command.CommandText = "select Test from PrescTest ";
conn.Open();
command.ExecuteNonQuery();
SqlDataReader dr2 = command.ExecuteReader();
try
{
while (dr2.Read())
{
// get the results of column "Test"
string Tests = (string)dr2["Test"].ToString();
TextBox15.Text = Tests +" ";
}
conn.Close();
}
catch (SqlException sqlexception)
{
Response.Write("ERROR ::" + sqlexception.Message);
}
catch (Exception ex)
{
Response.Write("ERROR ::" + ex.Message);
}
finally
{
conn.Close();
}
答案 0 :(得分:0)
将为您分配值,并删除文本框的上一个值。试试这个:
while (dr2.Read())
{
// get the results of column "Test"
string Tests = dr2["Test"].ToString();
TextBox15.Text += Tests +" ";
}
答案 1 :(得分:0)
您正在覆盖来自数据库的值。参见下面的修订代码
SqlCommand command = conn.CreateCommand();
command.CommandText = "select Test from PrescTest ";
conn.Open();
//Nothing to execute so you can scrap this line
//command.ExecuteNonQuery();
SqlDataReader dr2 = command.ExecuteReader();
try {
string Tests = "";
if (dr2.HasRows) {
while (dr2.Read()) {
// get the results of column "Test"
Tests += dr2["Test"].ToString() + " ";
}
}
TextBox15.Text = Tests.Trim();
conn.Close();
} catch (SqlException sqlexception) {
Response.Write("ERROR ::" + sqlexception.Message);
} catch (Exception ex) {
Response.Write("ERROR ::" + ex.Message);
} finally {
conn.Close();
}