简体   繁体   English

如何在ASP.net MVC中获取数据

[英]How to get data in ASP.net MVC

I'm trying to get the IsAdmin Value with SQL query(This query return 1 line or 0). 我正在尝试使用SQL查询获取IsAdmin值(此查询返回1行或0)。 but i get this error {"invalide reading Tentative there are no any data present."} This is my code 但是我收到此错误{“无效,正在读取“暂定”,没有任何数据。”}这是我的代码

        public static bool Login (string iduser, string password, bool IsAdmin)
    {
        bool auth = false;
        string query = string.Format("Select IsAdmin from [user] where iduser = '{0}' AND mdp = '{1}' ;", iduser, password);
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        SqlDataReader re = cmd.ExecuteReader();
        auth = re.HasRows;
        if (auth) { IsAdmin = re.GetBoolean(0); } // the error is on this line (this line will alow me to get IsAdmin Value If the user exist)
        con.Close();
        return auth;

    }

You are open to horrible SQL injection . 您对可怕的SQL注入持开放态度。 Your site will be pwned by hackers the very same second you put it online if you don't use parametrized queries. 如果您不使用参数化查询,那么您的网站将在您在线上的同一秒内被黑客伪造。

Like this: 像这样:

public static bool IsAdmin(string iduser, string password)
{
    using (var conn = new SqlConnection(ConnectionString))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = @"
            SELECT IsAdmin 
            FROM [user] 
            WHERE iduser = @iduser AND mdp = @mdp;
        ";
        cmd.Parameters.AddWithValue("@iduser", iduser);
        cmd.Parameters.AddWithValue("@mdp", password);
        using (var reader = cmd.ExecuteReader())
        {
            return reader.Read() && reader.GetBoolean(reader.GetOrdinal("IsAdmin"));
        }
    }
}

You need to call 你需要打电话

re.Read();

to move the reader to the first record before attempting to access the data. 在尝试访问数据之前将阅读器移至第一条记录。 re.HasRows does not cause the reader to move to the first record. re.HasRows不会使读者移动到第一条记录。

Also, definitely use parameterized queries. 另外, 一定要使用参数化查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net mvc控制器中获取发布的表单数据 - how to get posted form data in asp.net mvc controller 如何在ASP.NET MVC控制器中获取发布JSON数据 - How to get the post json data in asp.net mvc controller 如何在ASP.NET MVC中使用API​​获取数据? - How to get data using API in ASP.NET MVC? 如何在ASP.NET MVC 4 Razor中获取/处理POST数据? - How to get/handle POST data in ASP.NET MVC 4 Razor? 如何在同一项目中的ASP.NET MVC控制器中从ASP.NET Web API获取数据 - How get data from asp.net web api in asp.net mvc controller in the same project 如何在 ASP.NET MVC 中使用 EntityFramework6.Npgsql 从 Postgres 数据库获取数据? - how to get data from Postgres data base with EntityFramework6.Npgsql in ASP.NET MVC? ASP.NET MVC 使用 WCF 从 SQL Server 获取数据:如何在控制器中放置临时数据存储 - ASP.NET MVC get data from SQL Server with WCF : how to put temporary data store in controller 如何从统计类型数据的数据库即时获取枚举中的数据? 在asp.net核心mvc中 - How to get data in enum from database instant of stastically typed data? in asp.net core mvc ASP.NET MVC在View中获取继承的类数据 - ASP.NET MVC Get inherited class data in View ASP.net mvc API获取请求和JSON数据 - ASP.net mvc API get request and JSON data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM