当前位置: 首页 > 知识库问答 >
问题:

使用DAL BLL的Windows窗体

丰景同
2023-03-14

我的EmployeeDB类

使用系统;使用System.Collections.Generic;使用System.LINQ;使用System.Text;使用System.Data.SQLClient;使用System.Data;

命名空间测试{公共类employeeedb{私有字符串连接字符串;

    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窗体项目中。

共有1个答案

皇甫德庸
2023-03-14

要绑定到数据网格:

  1. 向表单添加BindingSource(BindingSource)
  2. 将DataGrid的DataSource属性设置为BindingSource
  3. 将bindingSource的DataSource属性设置为GetEmployees()的结果

如果您的库有以下实现,比如:

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 = dataOperation.GetEmployees();
Test.EmployeeDB employeeDB = new Test.EmployeeDB();
dataGrid.DataSource = employeeDB.GetEmployees(); // assuming you have a dataGrid control
 类似资料:
  • 最近,我(主要出于好奇)制作了一个无边界表单。在制作了我自己的标题栏后,它包括标题和三个按钮(最小化、最大化和关闭),就像每个普通的Windows程序一样。我还为这些按钮编写了代码(只要问一下你是否想看到代码)。 但是,我注意到没有动画。我的意思是,例如,如果我单击最小化按钮,没有动画,程序立即消失(它不关闭,按钮工作,但没有动画)。这种情况在所有情况下都会发生:当我打开程序时,它会立即出现,当我

  • 问题内容: 我收到应用程序异常 每次当我尝试单击DataGridView时。 我收到错误消息 {“索引-1没有值。”}(SystemIndexOutOfaRange异常)。 在行上 而且我无法调试它。请帮助我找出导致此问题的原因以及如何对其进行调试? 问题答案: 我猜想您已经将一个最初为空的List(或其他不生成列表已更改事件的集合)绑定到了您的,然后将项目添加到了此List中。 您添加的项目将正

  • 我有一个简单的申请与2个表格。一个表单(form1)允许用户在列表框中选择记录。一旦进行了选择,第二个表单(form2)就应该用第一个表单中的数据更新。 用户应该能够选择一个不同的记录,第二个表单应该用新的数据更新。 我上面展示的只是创建更多的form2版本,它没有更新form2的当前迭代。

  • 求一个在windows上 可以自动关闭长时间不使用的窗体的软件, 如果可以 希望也说出一个linux平台的同作用的软件?

  • 我一直在尝试创建一个代码来模拟学校的队列(目前还没有),并尝试创建多个图片框并将它们存储在一个列表中。由于某种原因,他们没有出现。。。有人有什么建议吗?

  • 我正在构建一个具有以下目标的 Flink 应用程序: 将事件收集到键控的非活动触发会话窗口中 尽早发出输入事件的副本,并通过会话引用进行增强 打开和关闭会话时发出会话更新以及收集的会话统计信息(在会话关闭时) 通过滚动窗口,我已经能够实现上述目标,但在会话窗口中我没有成功。 我的窗口处理代码如下 来生成我正在使用的输入 和 当我使用会话窗口执行时,会出现以下异常: 希望我错过了一个技巧,因为无法使