日期:2014-05-18 浏览次数:20483 次
[color=#FF6600]EmployeeUI窗体:[/color] EmployeeInfo[] empInfo; private void Form1_Load(object sender, EventArgs e) { ShowAllRecords(); } private void ShowAllRecords() { this.Cursor = Cursors.WaitCursor; EmployeeDB emp = new EmployeeDB(); empInfo = emp.GetAllEmployee(); int recPos = this.BindingContext[empInfo].Position; if (empInfo != null) { txtEmployeeId.Text = empInfo[recPos].EmployeeId.ToString(); txtFirstName.Text = empInfo[recPos].FirstName; txtLastName.Text = empInfo[recPos].LastName; } [color=#FF6600]EmployeeDAL类库下的EmployeeDB类:[/color] public EmployeeInfo[] GetAllEmployee() { SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "GetAllEmployee"; ArrayList arrEmp = new ArrayList(); try { conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); EmployeeInfo emp; if (dr.HasRows) { while (dr.Read()) { emp = new EmployeeInfo((int)dr["EmployeeId"], (string)dr["FirstName"], (string)dr["LastName"]); arrEmp.Add(emp); } } else arrEmp = null; dr.Close(); conn.Close(); return (EmployeeInfo[])arrEmp.ToArray(typeof(EmployeeInfo)); } [color=#FF6600]EmployeeModel类库下的EmployeeInfo类:[/color] public class EmployeeInfo { public EmployeeInfo() { //构造函数 } private int employeeId; private string firstName; private string lastName; public int EmployeeId { get{return employeeId;} set{employeeId = value;} } public string FirstName { get{return firstName;} set{firstName = value;} } public string LastName { get{return lastName;} set{lastName = value;} } public EmployeeInfo(int employeeId, string firstName, string lastName) { this.employeeId = employeeId; this.firstName = firstName; this.lastName = lastName; } }