如何在按钮单击时填充Windows窗体上的字段 [英] How do I populate fields on windows form at button click
问题描述
我正在尝试使用存储过程单击搜索按钮来填充表单上的字段。在数据库级别SP工作正常。我试图搜索的文本框是varchar类型
private void btnsearch_Click(object sender,EventArgs e)
{
bool temp = false;
SqlConnection con = new SqlConnection(Data Source = xxxxxxxxxxx; Initial Catalog = xxxxxxxx; Trusted_Connection = true);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText =SP_name;
con.Open();
cmd.Connection = con;
cmd.Parameters.AddWithValue(@ abc ,txtabc.Text);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
//需要这部分的帮助。
}
if(temp == false)
MessageBox.Show(not found);
con.Close();
}
I当用户在文本框中输入值时需要帮助填充其他字段,如果数据库中存在值,则单击搜索按钮。
谢谢。
I am trying to populate fields on the form on click of search button with stored procedure. At database level SP works fine. The textbox I am trying to search with is varchar type
private void btnsearch_Click(object sender, EventArgs e)
{
bool temp = false;
SqlConnection con = new SqlConnection("Data Source=xxxxxxxxxxx;Initial Catalog=xxxxxxxx;Trusted_Connection=true");
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_name";
con.Open();
cmd.Connection = con;
cmd.Parameters.AddWithValue("@abc", txtabc.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//need help with this part.
}
if (temp == false)
MessageBox.Show("not found");
con.Close();
}
I need help with populating other fields when user enters value into the textbox and click on search button if value exists in the db.
Thanks.
推荐答案
如评论中所述,从DataReader访问数据非常简单。
例如:
As mentioned in the comments, accessing the data from a DataReader is quite trivial.
For example:
TextBox1.Text = dr["someField"].ToString();
TextBox2.Text = dr["someField2"].ToString();
等。
etc.
这篇关于如何在按钮单击时填充Windows窗体上的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!