[英]Fetch data from Database and show in TextBox
我有一张表,我想从中获取数据并在
TextBox
显示数据,分别是StudentFirstName
和SchoolID
,我需要在它们旁边的两个空TextBoxes
不确定如何做到这一点。
我的数据库表
StudentFirstName SchoolID StudCourse
abc sc123 Bcom
cef sc155 Bcom
gij sc133 Bcom
abc sc122 BCA
cef sc156 BCA
gij sc144 BCA
C#
using (MySqlConnection myConnection = new MySqlConnection(constr))
{
string oString = "Select * from euser_student WHERE StudCourse=@StudCourse order by StudentFirstName ASC";
MySqlCommand oCmd = new MySqlCommand(oString, myConnection);
oCmd.Parameters.AddWithValue("@StudCourse", StudCourse);
myConnection.Open();
using (MySqlDataReader oReader = oCmd.ExecuteReader())
{
if (oReader == null || !oReader.HasRows)
{
ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('No Student Found')", true);
}
else
{
while (oReader.Read())
{
}
}
myConnection.Close();
}
}
您可以使用SqlDataReader.GetString Method
以字符串形式获取指定列的值,如下所示:
while (oReader.Read())
{
TextBox1.Text = oReader.GetString(1); // 1 is the Parameter that is The zero-based column ordinal you can change it to what you want
TextBox2.Text = oReader.GetString(2);
}
选择此项以了解更多信息: Retrieving Data Using a DataReader
。
但是,如果要显示多于一行,则最好使用GridView
控件显示数据。 首先像这样向您的aspx
添加一个GridView
:
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
接着:
GridView1.DataSource = oReader;
GridView1.DataBind();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.