Windows form using DAL BLL
我的EmployeeDB类
使用系统;使用system.collections.generic;使用system.linq;使用system.text;使用system.data.sqlclient;使用系统数据;
命名空间测试{公共类员工数据库{私有字符串连接字符串;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | public EmployeeDB() { //my connectionstring info } public int CountEmployees() { SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("CountEmployees", con); cmd.CommandType = CommandType.StoredProcedure; try { con.Open(); return (int)cmd.ExecuteScalar(); } catch (SqlException err) { // Replace the error with something less specific. // You could also log the error now. throw new ApplicationException("Data error."); } finally { con.Close(); } } public List<EmployeeDetails> GetEmployees() { SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("GetAllEmployees", con); cmd.CommandType = CommandType.StoredProcedure; // Create a collection for all the employee records. List<EmployeeDetails> employees = new List<EmployeeDetails>(); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { EmployeeDetails emp = new EmployeeDetails( (int)reader["EmployeeID"], (string)reader["FirstName"], (string)reader["LastName"], (string)reader["TitleOfCourtesy"]); employees.Add(emp); } reader.Close(); return employees; } catch (SqlException err) { // Replace the error with something less specific. // You could also log the error now. throw new ApplicationException("Data error."); } finally { con.Close(); } } } |
}
我的EmployeeDetails类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace Test { public class EmployeeDetails { private int employeeID; private string firstName; private string lastName; private string titleOfCourtesy; 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 string TitleOfCourtesy { get {return titleOfCourtesy;} set {titleOfCourtesy = value;} } public EmployeeDetails(int employeeID, string firstName, string lastName, string titleOfCourtesy) { this.employeeID = employeeID; this.firstName = firstName; this.lastName = lastName; this.titleOfCourtesy = titleOfCourtesy; } public EmployeeDetails(){} } } |
然后我构建类库并将引用添加到我的Windows窗体项目中。
下面是我的主窗体的屏幕截图,该类显示出来,但是没有方法。
要绑定到数据报,请执行以下操作:
如果您的库具有以下实现,例如:
1 2 3 4 5 6 7 8 9 | public interface IDataOperation { List<Employee> GetEmployees(); } public class DataOperation : IDataOperation { public List<Employee> GetEmployees(){} } |
您的实现应该如下所示:
1 2 3 4 5 |
或者,您可以简单地将DataGrid的DataSource属性设置为getEmployees()的结果,而不使用bindingSource:
1 | dataGrid.DataSource = dataOperation.GetEmployees(); |
编辑:在屏幕截图中,在使用EmployeeDB之前,应先实例化它,如下面的代码所示:
1 2 | Test.EmployeeDB employeeDB = new Test.EmployeeDB(); dataGrid.DataSource = employeeDB.GetEmployees(); // assuming you have a dataGrid control |