C#和sql数据库的问题

来源:百度知道 编辑:UC知道 时间:2024/05/04 15:03:25
我在做一个搜索框,根据别人输入的姓名,将员工的姓名 籍贯 家庭住址等显示在textbox1,textbox2,textbox3..上,看下我代码(很简单的代码)
private void button1_Click(object sender, EventArgs e)
{

if (textBox1.Text.Trim()=="")
{

MessageBox.Show("请输入姓名");
}

else

{
SqlDataReader read;
SqlConnection conn = new SqlConnection();

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from majinqi where name='"+textBox1.Text+"'";

read = cmd.ExecuteReader();
read.Read();

if (read.HasRows == true)
{
textBox2.T

用一个 数据显示控件 绑定 查出来的 人员列表。在列表里 选择一个人员。
显示该人员的信息(ID是为一个吧)。同名不同名 没关系

我觉得不是你那样设计.
姓名一样的很正常。你就把所有姓名一样的记录都显示出来呗。然后让用户自己选择。
用一个datagridview显示所有姓名一样的记录。

你做这个程序,姓名相同,那你就给他们相同的一个唯一标示,比如ID这类的,根据不同的弹不不同的资料

你只让read读了一行。需要用ExecuteReader()实例对象的read方法让指针下移。
之后用while循环读出来。
while(read.read())
{
textBox2.Text = read[1].ToString();
textBox3.Text = read[2].ToString();
textBox4.Text = read[3].ToString();
textBox5.Text = read[4].ToString();
textBox6.Text = read[5].ToString();
textBox7.Text = read[0].ToString();
}
之后就是你排版的问题了。

Panel pl_layout=new Panel();

SqlDataReader read;
SqlConnection conn = new SqlConnection();

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from majinqi where name='"+textBox1.Text+"'";

read = cmd.ExecuteReader();

while(read.read())
{
TextBox t