private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar==13) //在textBox1按下回车时执行
{
SqlConnection con = new SqlConnection(连接数据库的字符串);
con.Open();
SqlCommand com = new SqlCommand("select ID from table2 where userName='" + textBox1.Text.Trim() + "'", con);
SqlDataReader dr = com.ExecuteReader();
if(dr.Read())
{
textBox2.Text = dr[0].ToString();
}
else
{
textBox2.Text = "没找到该名字对应的ID";
}
dr.Dispose();
com.Dispose();
con.Close();
}
}
C#当输入用户名来自数据表,自动显示数据表中对应的id
最新推荐文章于 2023-02-17 10:01:51 发布