在Windows App中从文本框中的数据库读取数据 [英] Read data from Database inside textboxes in a Windows App
问题描述
嗨
我想从窗体上所有文本框中的sql数据库中显示表数据(10列,表中有1条记录).我的表单上有10个文本框-txtLearnerName,txtSurname,txtIDnumber,txtAddress等.
我写了以下内容.
///类中的方法:clslogin
Hi
I want to display the tables data(10 Columns,1 Record inside the table), from a sql database inside all the textboxes on my form. There are 10 Textboxes on my form - txtLearnerName, txtSurname, txtIDnumber, txtAddress etc.
I have written the following.
///Method inside of the class: clslogin
public string update()
{
SqlDatabase sqldb = new SqlDatabase(ConnectionString);
IDataReader reader = sqldb.ExecuteReader("pSEL_mALL", new object[] { });
string test = "";
while (reader.Read())
{
test = reader["Lname"].ToString();
}
return test.ToString();
}
///调用方法
///Calling the method
private void button1_Click(object sender, EventArgs e)
{
string reader = login.update().ToString();
txtLearnername.Text = reader.ToString();
}
它正在工作.它将学习者名称写在表单的文本框中..
但是...
如果要在所有文本框中显示所有表列数据,则必须编写10个方法.并在按钮中调用所有方法.这花了太多时间.
如何从方法中返回多个值?
It is working.. It writes the Learner name inside the textbox on the form..
BUT...
If I want to display all the tables columns data in all the textboxes, then I have to write 10 methods.. And call all of the methods in the button. It is taking too much time.
How can return more than one value from the method?
Isn''t there a shorter way?
推荐答案
像这样更改您的方法
Hi,
Change your method like this
public SqlDataReader update()
{
SqlDatabase sqldb = new SqlDatabase(ConnectionString);
SqlDataReader reader = sqldb.ExecuteReader("pSEL_mALL", new object[] { });
return reader;
}
Now in cs file of windows app use this like
SqlDataReader reader=//methodname;
while(reader.Read())
{
Lnametxt.Text=reader["Lname"].ToString();
//SImilarly other textboxes
}
谢谢.有用.我只是用IdataReader替换SqlDataReader
Thank You. It Works. I just replace SqlDataReader with IdataReader
public IDataReader update()
{
SqlDatabase sqldb = new SqlDatabase(ConnectionString);
IDataReader reader = sqldb.ExecuteReader("pSel_mAll", new object[] { });
return reader;
}
private void button1_Click(object sender, EventArgs e)
{
IDataReader reader = login.update();
while (reader.Read())
{
txtLearnername.Text = reader["Lname"].ToString();
txtSurname.Text = reader["LSurname"].ToString();
}
}
这篇关于在Windows App中从文本框中的数据库读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!