我的EmployeeDB类
使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 使用System.Data.SqlClient; 使用System.Data;
命名空间测试 { 公共类EmployeeDB { private string connectionString;
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();
}
}
}
}
我的员工详细课程
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窗体项目的引用。
这是我的主要表单的屏幕截图,该类显示但是没有方法。
答案 0 :(得分:1)
绑定到DataGrid:
如果您的库具有以下实现,例如:
public interface IDataOperation
{
List<Employee> GetEmployees();
}
public class DataOperation : IDataOperation
{
public List<Employee> GetEmployees(){}
}
您的实施应如下所示:
IDataOperation dataOperation = new DataOperation();
var bindingSource = new BindingSource();
dataGrid.DataSource = bindingSource;
bindingSource.DataSource = dataOperation.GetEmployees();
或者,你可以简单地将DataGrid的DataSource属性以编程方式设置为GetEmployees()的结果而不使用BindingSource:
dataGrid.DataSource = dataOperation.GetEmployees();
编辑:在截图中,您应该在使用之前实例化您的EmployeeDB,如下面的代码:
Test.EmployeeDB employeeDB = new Test.EmployeeDB();
dataGrid.DataSource = employeeDB.GetEmployees(); // assuming you have a dataGrid control