【问题标题】:Issue with query in ASP.NET MVCASP.NET MVC 中的查询问题
【发布时间】:2020-08-15 10:04:38
【问题描述】:

我正在使用 Web API 和 ASP.NET MVC。

我在地址栏中传递了错误的用户名和密码,然后也给出了问题所在的成功消息

我的表中没有这条记录:

我正在创建 2 个项目,一个用于 Web API 框架脚手架,第二个是一个普通的空 ASP.NET MVC 项目。

普通的空 ASP.NET MVC 项目(不使用实体框架)

globalvariable.cs:

namespace Mvc
{
    public static class globalvariable
    {
        public static HttpClient webapiclient = new HttpClient();

        static globalvariable()
        {
            webapiclient.BaseAddress = new Uri("https://localhost:0000/api/");
            webapiclient.DefaultRequestHeaders.Clear();
            webapiclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
    }
}

empsController.cs:

namespace Mvc.Controllers
{
    public class empsController : Controller
    {
        public JsonResult Index()
        {
            IEnumerable<mvcempmodel> empList;
            HttpResponseMessage response = globalvariable.webapiclient.GetAsync("emps").Result;
            empList = response.Content.ReadAsAsync<IEnumerable<mvcempmodel>>().Result;
            //return View(empList);
            return Json(empList, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult Login(string username, string password)
        {
                if (username == "" || username == null)
                {
                    var data = new
                    {
                        message = "Enter Username ",
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (password == "" || password == null)
                {
                    var data = new
                    {
                        message = "Enter Password",
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var userdata = "select * from emp where username='" + username + "'and password='" + password + "'".First();
                    if (userdata != null)
                    {
                        var data = new
                        {
                            message = "Success",
                            data = new { username }
                        };
                        return Json(data, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        var data = new
                        {
                            message = "Username and Password incorrect ",
                        };
                        return Json(data, JsonRequestBehavior.AllowGet);
                    }
                }
            }
        }
    }
}

Web API 项目(使用实体框架)

empsController.cs:

namespace WebApi.Controllers
{
    public class empsController : ApiController
    {
        private empdbEntities db = new empdbEntities();

        // GET: api/emps
        public IQueryable<emp> Getemps()
        {
            return db.emps;
        }

        // GET: api/emps/5
        [ResponseType(typeof(emp))]
        public IHttpActionResult Getemp(int id)
        {
            emp emp = db.emps.Find(id);

            if (emp == null)
            {
                return NotFound();
            }

            return Ok(emp);
        }
    }
}

我想要什么如果用户输入真实凭据然后给出成功消息,当用户输入错误凭据然后给出用户名和密码不正确的消息

这里我认为我的查询是错误的

var userdata = "select * from emp where username='" + username + "'and password='" + password + "'".First();

请帮忙

【问题讨论】:

  • 你不是在查询数据库,你所做的只是定义一个字符串。此外,您通过不安全的查询字符串传递用户名和密码。您还应该使用参数化查询而不是替换
  • @OleEHDufour 我是初学者如何查询数据库,请帮助

标签: c# asp.net-mvc asp.net-web-api


【解决方案1】:

你可以检查这个到 url (正如你提到的你是一个初学者,想知道如何查询数据库)

在 asp.net 中,您可以使用查询数据库

  1. 实体框架(第二个链接显示如何使用实体框架查询本地数据库)
  2. Dapper(微型,快速)
  3. ADO.net(老派风格,需要编写大量样板代码)

https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started

https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string

要在远程或本地服务器上查询Sql server数据库,只需更改连接字符串即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多