在ASP.net网站中使用我的数据访问层时出错(需要专家) [英] Error using my Data Access Layer in ASP.net WebSite (experts needed )

查看:146
本文介绍了在ASP.net网站中使用我的数据访问层时出错(需要专家)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有2个表Employee和Department的SQL数据库
尝试使用DAL来获取和设置数据库数据容易且具有更高的安全性
这是我的工作:
我做了2个类来反映数据库EX中的表:Emp_object.cs<<这是课程
在此类内部,我使用表中的列定义了公共属性,例如

i am Using SQL Database with 2 tables Employee and Department
trying to use DAL to get and set data with database Easy and more security
here what i did :
i made 2 classes reflect the tables in database EX: Emp_object.cs << this is the class
inside this class i defined public properties with the columns inside the table like this

public class Emp_object
{
    public int ID { set; get; }
    public string EmpName { set; get; }
    public string EmpDept { set; get; }
    public float EmpSalary { set; get; }
    public float EmpBounce { set; get; }
    public float EmpCut { set; get; }
    public int EmpCell { set; get; }
    
    public Emp_object()
	{
		//
		// TODO: Add constructor logic here
		//
	}
// i use this method to select * from the table employee
public List<Emp_object> SelectAll()
    {
       List<Emp_object> itm_lst = new List<Emp_object>();
       Emp_object itm;
       DataTable dt= new BaseLayer().GetDataTable("GetAllEmp", CommandType.StoredProcedure);
       foreach (DataRow drow in dt.Rows)
       {
           itm = new Emp_object();
           itm.EmpName = drow["EmpName"].ToString();
           itm.EmpDept = drow["EmpDept"].ToString();
           itm.EmpSalary = (float)drow["EmpSalary"];
           itm.EmpBounce = (float)drow["EmpBounce"];
           itm.EmpCut =(float)drow["Emp_Cut"];
           itm.EmpCell = (int)drow["Emp_Cell"];

           itm_lst.Add(itm);
       }
       return itm_lst;
    }

}


我有另一个花药类BaseLayer,它获取连接字符串,并且它具有与emp_object类进行交互的方法


and i have anther class BaseLayer that is get the connection string and it has the methods that interact with the emp_object class

public class BaseLayer
{
    SqlConnection con;
    #region constarctor 
    public BaseLayer()
    {
        con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HrEmpDatabase.mdf;Integrated Security=True;User Instance=True");
    }
   
    #endregion

public int ModifyTable(string CommandText, CommandType comType,List<Emp_object> EmpList)
    {
        SqlCommand com = new SqlCommand(CommandText, con);
        com.CommandType = comType;
        for (int i = 0; i < EmpList.Count; i++)
        {
            com.Parameters.Add(EmpList[i]);
        }
        con.Open();
        int x = com.ExecuteNonQuery();
        con.Close();
        return x;
    }


现在我正在尝试使用,我需要执行将数据插入数据库或进行update的方法,使用emp_object类进行删除,可以将对象传递给该方法,并使用emp对象的列表来传递参数吗?多数民众赞成在

推荐答案

需要帮助的情况下,可以将列表用作已定义的ModifyTable的参数.您可能会遇到的问题是,如果单个Emp处于什么状态?它是添加,修改,删除还是保持不变.您必须知道这一点才能决定SQL语句.

我注意到您使用了一个数据表,从中可以拉出列表.数据表具有跟踪行状态的机制(实际上每个数据行都具有此状态).因此,为什么不使用数据表并直接对其进行修改并使用 SqlDataAdapter [ ^ ]进行修改.
Yes, you can use the list as a parameter to the ModifyTable as you have already defined. The problem you would have is, what is the state if a single Emp? Is it added, modified, deleted or unchanged. You have to know this in order to decide the SQL statement.

I noticed that you have used a data table from which you pull out the list. Data table has a mechanism to track the state of a row (well actually each data row has this). So why not use the data table and make the modifications directly to it and use a SqlDataAdapter[^] to actually carry out the modifications.


这篇关于在ASP.net网站中使用我的数据访问层时出错(需要专家)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆