0

我对编程很陌生,并且在我正在开发的基本应用程序中遇到了问题。我有一个类似于这个的人类......

        Person
        {
              SqlConnection conn = new SqlConnection(@"Integrated Security=True; Data                       
              Source=ME\MyPRESS;Initial Catalog=TEST5");
              SqlCommand cmd = new SqlCommand("usp_employee", conn);
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.Add("@emp_id", SqlDbType.Int).Value = id;
              SqlDataReader dr = cmd.ExecuteReader();

         while (dr.Read())

        try
        {
            conn.Open();

            {
                department = dr["dept_name"].ToString();
                fname = dr["emp_first_name"].ToString();
                lname = dr["emp_last_name"].ToString();
                email = dr["emp_email"].ToString();
                phone = dr["emp_phone"].ToString();
                position = dr["emp_position"].ToString();
                address1 = dr["emp_address1"].ToString();
                address2 = dr["emp_address2"].ToString();
                city = dr["emp_city"].ToString();
                state = dr["emp_state"].ToString();
                postal_code = dr["emp_postal_code"].ToString();

                // department = txtFirst_name.Text;
            }
        }
        finally
        {
            // 3. close the reader
            if (dr != null)
            {
                dr.Close();
            }

            // close the connection
            if (conn != null)
            {
                conn.Close();
            }
        }
    }

受保护的字符串部门;

    public string Department
    {
         get { return department; }
         set { department = value; }
    }
    protected string fname;

    public string Fname
    {
        get { return fname; }
        set { fname = value;}
    }

    protected string lname;

    public string Lname
    {
        get { return lname; }
        set { lname = value; }
    }

    protected string email;

    public string Email
    {
        get { return email; }
        set { email = value; }
    }



    protected string position;

    public string Position
    {
      get { return position; }
      set { position = value; }
    }

    protected string address1;

    public string Address1
    {
        get { return address1; }
        set { address1 = value; }
    }

    protected string address2;

    public string Address2
    {
        get { return address2; }
        set { address2 = value; }

    }

    protected string phone;

    public string Phone
    {
        get { return phone; }
        set { phone = value; }

    }

    protected string city;

    public string City
    {
        get { return city; }
        set { city = value; }
    }

    protected string state;

    public string State
    {
        get { return state; }
        set { state = value; }
    }

    protected string postal_code;

    public string Postal_Code
    {
        get { return postal_code; }
        set { postal_code = value; }

    }

}

如上所示,我有一个表单和一个带有存储过程的数据库来检索数据......我迷失的地方是连接我的表单以显示存储过程中的记录。

4

2 回答 2

0

这有帮助!我现在已经启动并运行了......

公共人员(int id){

        SqlConnection conn = new SqlConnection(@"Integrated Security=True; Data Source=JODIEPC\XPRESS;Initial Catalog=TEST5");
        conn.Open();
        SqlCommand cmd = new SqlCommand("usp_EmployeeSelect", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@emp_id", SqlDbType.Int).Value = id;



        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            department = dr["dept_name"].ToString();
            fname = dr["emp_first_name"].ToString();
            lname = dr["emp_last_name"].ToString();
            email = dr["emp_email"].ToString();
            phone = dr["emp_phone"].ToString();
            position = dr["emp_position"].ToString();
            address1 = dr["emp_address1"].ToString();
            address2 = dr["emp_address2"].ToString();
            city = dr["emp_city"].ToString();
            state = dr["emp_state"].ToString();
            postal_code = dr["emp_postal_code"].ToString();

        }


    }

   protected int id;
   protected string fname;
   protected string lname;
   protected string department;
   protected string email;
   protected string position;
   protected string address1;
   protected string address2;
   protected string phone;
   protected string city;
   protected string state;
   protected string postal_code;





    public int Id
   {
       get { return id; }
       set { id = value; }
   }



    public string Department
    {
       get { return department; }
       set{ department = value; }
    }

    public string Fname
    {
        get { return fname; }
        set { fname = value; }
    }
    public string Lname
    {
        get { return lname; }
        set { lname = value; }
    }
    public string Email
    {
        get { return email; }
        set { email = value; }
    }

    public string Position
    {
      get { return position; }
      set { position = value; }
    }

    public string Address1
    {
        get { return address1; }
        set { address1 = value; }
    }

    public string Address2
    {
        get { return address2; }
        set { address2 = value; }

    }

    public string Phone
    {
        get { return phone; }
        set { phone = value; }

    }

    public string City
    {
        get { return city; }
        set { city = value; }
    }
     public string State
    {
        get { return state; }
        set { state = value; }
    }
    public string Postal_Code
    {
        get { return postal_code; }
        set { postal_code = value; }

    }
}

---------winform---------------- private void btnSearch_Click(object sender, EventArgs e) { Person Emp = new Person(Int32.Parse(txtId.Text) ); txtFirstName.Text = Emp.Fname; txtLastName.Text = Emp.Lname; txtEmail.Text = Emp.Fname; txtPhone.Text = Emp.Phone; txtDepartment.Text = Emp.Department; txtPosition.Text = Emp.Position; txtAddress1.Text = Emp.Address1; txtAddress2.Text =Emp.Address2; txtCity.Text = Emp.City; txtState.Text = Emp.State; txtPostalCode.Text = Emp.Postal_Code; 这不是最有创意的方式,但它确实有效....

非常感谢各位....

于 2013-01-08T03:21:33.530 回答
0

一种简单的方法是为您的 Person 类定义一个构造函数:

public class Person
{
    public Person(string Department, string fName /* etc */)
    {
        this.Department = Department;
        this.fName = fName;

        // and so on for all the fields
    }

    // the rest of the class definition goes here
}

然后在循环数据时,使用该构造函数创建一个 Person 对象。从您的函数中返回新的 Person 对象。

于 2013-01-07T02:08:45.747 回答