如何从SQL数据库中检索int值并将其与int值进行比较..

时间:2017-03-30 22:19:22

标签: mysql sql asp.net database

我创建了一个登录页面,并在其数据库中创建了一个包含int值(0或1)的列(角色),以确定用户(如果是管理员或普通用户)的角色。 所以,我想写一个查询,检索登录用户的int值(角色),并将其与1或0进行比较,将用户重定向到其特定的页面..

这是我的代码:

    using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class LogInPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(TextBox1.Text))
        { Label4.Text = "**Required**"; }
        else
        { Label4.Text = ""; }
        if (string.IsNullOrWhiteSpace(TextBox2.Text))
        { Label6.Text = "**Required**"; }
        else
        { Label6.Text = ""; }

        if (TextBox1.Text.Count() > 0 && TextBox2.Text.Count() > 0)
        {

            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Azooz_2\Documents\Visual Studio 2013\WebSites\Gulf_K\App_Data\DatabaseGulf.mdf;Integrated Security=True;");
            SqlDataAdapter sda = new SqlDataAdapter("Select count(*) From Users Where UserName ='" + TextBox1.Text + "' and Password ='" + TextBox2.Text + "'", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            if (dt.Rows[0][0].ToString() == "1")
            { 
                if (){
                  Response.Redirect("TheArrivalTime.aspx");
                }
               else{Response.Redirect("TheArrival.aspx")}



            }
            else
            {
                Label5.Text = "Wrong UserName or Password !!";
                Label6.Text = " ";
                Label4.Text = " ";
                TextBox1.Text = " ";
                TextBox2.Text = " ";
            }

        }


    }

    protected void Button2_Click(object sender, EventArgs e)
    {    
        Label4.Text = " ";
        Label5.Text = " ";
        Label6.Text = " ";
        TextBox1.Text = " ";
        TextBox2.Text = " ";
    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {this.resetLbls();}
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {this.resetLbls();}



    protected void resetLbls()
    {
        Label4.Text = " ";
        Label5.Text = " ";
        Label6.Text = " ";
    }
    }

我希望将查询放在if语句中:

 if (){
          Response.Redirect("TheArrivalTime.aspx");
        }
       else{ Response.Redirect("TheArrival.aspx");}

所以,有人可以帮我吗?

1 个答案:

答案 0 :(得分:-1)

  

为什么不从username = variable和的用户中选择user_type   密码   =变量然后将[用户类型字段]置于条件

中      

... count只会给你一个数字