更有效的运行SQL查询的方法

时间:2015-12-08 04:56:19

标签: c# sql asp.net .net entity-framework-6

我试图看看是否有更短的编写代码来运行SQL查询的方法。之前我使用的是Entity Framework,但似乎加载方式比使用SQL命令慢。任何建议都会很棒。提前谢谢!

以下是我的SQL命令的代码:

        string query = "Select Count(*) From Employee Where Email = @Email And Password = @Password";
        string queryEmployeeId = "Select EmployeeId From Employee Where Email =@Email and Password = @Password";
        string queryAdmin = "Select Admin From Employee Where Email =@Email and Password = @Password";
        string queryFirstName = "Select FirstName From Employee Where Email =@Email and Password = @Password";
        int result = 0;
        int employeeId = 0;
        int admin = 0;
        string employeeFirstName;

        using (SqlConnection connection = new SqlConnection(@"Data Source=198.71.227.2;Initial Catalog=TaskManager;Integrated Security=False;User ID=;Password=;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"))
        {
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                command.Parameters.AddWithValue("@Email", txtEmail.Text);
                command.Parameters.AddWithValue("@Password", txtPassword.Text);
                connection.Open();
                result = (int)command.ExecuteScalar();
            }

            using (SqlCommand command = new SqlCommand(queryEmployeeId, connection))
            {
                command.Parameters.AddWithValue("@Email", txtEmail.Text);
                command.Parameters.AddWithValue("@Password", txtPassword.Text);
                employeeId = (int)command.ExecuteScalar();
            }

            using (SqlCommand command = new SqlCommand(queryAdmin, connection))
            {
                command.Parameters.AddWithValue("@Email", txtEmail.Text);
                command.Parameters.AddWithValue("@Password", txtPassword.Text);
                admin = (int)command.ExecuteScalar();
            }

            using (SqlCommand command = new SqlCommand(queryFirstName, connection))
            {
                command.Parameters.AddWithValue("@Email", txtEmail.Text);
                command.Parameters.AddWithValue("@Password", txtPassword.Text);
                employeeFirstName = (string)command.ExecuteScalar();
            }
        }

        if (result > 0)
        {
            Session["EmployeeId"] = employeeId;
            Session["Admin"] = admin;
            Session["EmployeeFirstName"] = employeeFirstName;
            Response.Redirect("~/MyJobSheet.aspx");
        }

最初,这是我的实体框架代码:

        string username = txtEmail.Text;
        string password = txtPassword.Text;

        using (TaskManagerEntities myEntities = new TaskManagerEntities())
        {
            var employee = (from a in myEntities.Employees
                            where a.Email == username && a.Password == password
                            select new { a.EmployeeId, a.Admin, a.Email, a.Password, a.FirstName }).SingleOrDefault();

            if (employee != null)
            {
                Session["EmployeeId"] = employee.EmployeeId;
                Session["Admin"] = employee.Admin;
                Session["EmployeeFirstName"] = employee.FirstName;
                Response.Redirect("~/MyJobSheet.aspx");
            }

2 个答案:

答案 0 :(得分:2)

编写一个存储过程,该过程返回一个包含以下列EmployeeID,Admin,EmployeeFirstname的表。另外,检查员工是否存在可以在存储过程本身中完成(更好地存在用户IF而不是count(*)) 。

通过执行此操作,将只有一个数据库调用而不是4.也正如史蒂夫所提到的那样确保将电子邮件列编入索引

答案 1 :(得分:0)

ADO.NET将始终比任何ORM更高效,因为它更多"低级别",您可以做的是关闭实体框架提供的一些功能,当您执行只读查询时# 39; S。例如,您使用AsNoTracking()来获取实体,但不一定要让您的上下文跟踪它们。

var blogs2 = context.Blogs 
                    .Where(b => b.Name.Contains(".NET")) 
                    .AsNoTracking() 

或者您可以使用Dapper为每个实体创建只读查询的存储库,它使用ADO.Net方法,但工作效率高于纯ADO.Net

相关问题