在数据库中搜索数据 [英] Search data in database

查看:176
本文介绍了在数据库中搜索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上使得使用VS2008和SQL Server 2005,其与登录页面启动网站。现在我想验证为LoginID 和用户输入的密码。该认证将于一旦系统发现,从数据库表中的ID和密码。一旦发现,我想检查是否有什么样的用户是即管理​​客户。如果用户是管理员,则该页面应该被重定向到 abc.aspx ,否则 cde.aspx

我的前端LoginPage是:

 < TR>
< TD类=style11>登录< / TD>
&所述; TD>
< ASP:文本框ID =txtUserName=服务器WIDTH =300像素的CssClass =Textbox1的>< / ASP:文本框>
< / TD>
< / TR>
&所述; TR>
< TD类=style11>密码< / TD>
&所述; TD>
< ASP:文本框ID =txtPassword=服务器的TextMode =密码WIDTH =300像素的CssClass =Textbox1的>< / ASP:文本框>
< / TD>
< / TR>
&所述; TR>
&所述; TD列跨度=2>
< ASP:按钮的ID =btnSubmit按钮=服务器的OnClick =btnSubmit_Click的CssClass =BTN1
                    文本=提交/>
< ASP:按钮的ID =btnCancel=服务器的OnClick =btnCancel_Click的CssClass =BTN1
                    文本=取消/>
< / TD>
< / TR>

和我的后端code是:

  // code 1
SqlDataSource的SDS =新的SqlDataSource();
sds.ConnectionString = ConfigurationManager.ConnectionStrings [Gen_LicConnectionString3]的ToString()。
sds.SelectParameters.Add(为LoginID,输入code.String,this.txtUserName.Text);
sds.SelectParameters.Add(密码,输入code.String,this.txtPassword.Text);
sds.SelectCommand =SELECT FROM USER_TYPE [User_Details],其中[为LoginID] = @为LoginID [密码] = @密码;    如果(//一些条件)//< - 在这里,我要检查的条件USER_TYPE是否为管理员或客户
    {
        的Response.Redirect(Lic_Gen.aspx); //< - 如果管理员
    }
    其他
    {
        的Response.Redirect(Cust_Page.aspx); //< - 如果客户
    }
// code 2
//字符串的connectionString = WebConfigurationManager.ConnectionStrings [Gen_LicConnectionString3]的ConnectionString。
    //字符串selectSQL =SELECT FROM USER_TYPE WHERE User_Details [为LoginID] = @为LoginID [密码] = @Password;
    // SqlConnection的CON =新的SqlConnection(的connectionString);
    //的SqlCommand CMD =新的SqlCommand(selectSQL,CON);
    // SqlDataAdapter的适配器=新SqlDataAdapter的(CMD);
    // DataSet的DS =新的DataSet();    //如果(cmd.Equals(1))
    // {
    // Response.Redirect的(Lic_Gen.aspx);
    //}
    //其他
    // {
    // Response.Redirect的(Cust_Page.aspx);
    //}


解决方案

这是无需使用会员车型最简单的方法。这是使用的DataReader的简单方法。

  SqlDataReader的sdrDatanew = NULL;
    串strnew;
    字符串的connectionString = WebConfigurationManager.ConnectionStrings [Gen_LicConnectionString]的ConnectionString。
    SqlConnection的connew =新的SqlConnection(的connectionString);
    connew.Open();
    strnew ='+ ddlUserSel.SelectedItem.Value +和为LoginID ='+ txtUserName.Text +'和密码='+ txtPassword.Text +'从哪里User_Details USER_TYPE USER_TYPE =;
    的SqlCommand sqlCommnew =新的SqlCommand(strnew,connew);
    sdrDatanew = sqlCommnew.ExecuteReader();    INT用户等级和积分= 0;    如果(sdrDatanew.HasRows)
    {
        如果(sdrDatanew.Read())
        {
            用户等级和积分= Convert.ToInt32(sdrDatanew [USER_TYPE]的ToString());
        }
    }    开关(用户类型)
    {
        情况下0:
            的Response.Redirect(Lic_Gen.aspx);
            打破;
        情况1:
            的Response.Redirect(Cust_Page.aspx);
            打破;
        默认:
            Console.WriteLine(无效的用户名/密码);
            到Console.ReadLine();
            打破;
    }    connew.Close();

I'm basically making a web site using VS2008 and SQL Server 2005 which initiates with a login page. Now I want to authenticate the LoginID and the Password entered by the user. This authentication will take place once the system has found the ID and Password from the database table. Once found, I want to check whether what kind of user it is i.e. Admin or Customer. If the user is admin, then the page should be redirected to abc.aspx otherwise cde.aspx.

My front-end for LoginPage is:

<tr>
<td class="style11"> Login </td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Width="300px" CssClass="Textbox1"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style11"> Password </td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="300px" CssClass="Textbox1"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" CssClass="btn1"
                    Text="Submit" />
<asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" CssClass="btn1"
                    Text="Cancel" />
</td>
</tr>

And my backend code is:

//CODE 1
SqlDataSource sds = new SqlDataSource();
sds.ConnectionString = ConfigurationManager.ConnectionStrings["Gen_LicConnectionString3"].ToString();
sds.SelectParameters.Add("LoginID", TypeCode.String, this.txtUserName.Text);
sds.SelectParameters.Add("Password", TypeCode.String, this.txtPassword.Text);
sds.SelectCommand = "SELECT User_Type FROM [User_Details] WHERE [LoginID]=@LoginID AND [Password]=@Password";

    if (//Some Condition) //<-- Here I want to check the condition whether the User_Type is 'Admin' or 'Customer'
    {
        Response.Redirect("Lic_Gen.aspx"); //<-- If Admin
    }
    else
    {
        Response.Redirect("Cust_Page.aspx"); //<-- If Customer
    }


//CODE 2
//string connectionString = WebConfigurationManager.ConnectionStrings["Gen_LicConnectionString3"].ConnectionString;
    //string selectSQL = "SELECT User_Type FROM User_Details WHERE [LoginID]=@LoginID AND [Password] = @Password";
    //SqlConnection con = new SqlConnection(connectionString);
    //SqlCommand cmd = new SqlCommand(selectSQL, con);
    //SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    //DataSet ds = new DataSet();

    //if (cmd.Equals(1))
    //{
    //    Response.Redirect("Lic_Gen.aspx");
    //}
    //else
    //{
    //    Response.Redirect("Cust_Page.aspx");
    //}

解决方案

This is the simplest method without the use membership models. This is a simple method which uses datareader.

    SqlDataReader sdrDatanew = null;
    string strnew;
    string connectionString = WebConfigurationManager.ConnectionStrings["Gen_LicConnectionString"].ConnectionString;
    SqlConnection connew = new SqlConnection(connectionString);
    connew.Open();
    strnew = "select User_Type from User_Details where User_Type='" + ddlUserSel.SelectedItem.Value + "' AND LoginID = '" + txtUserName.Text + "' AND Password = '" + txtPassword.Text + "'";
    SqlCommand sqlCommnew = new SqlCommand(strnew, connew);
    sdrDatanew = sqlCommnew.ExecuteReader();

    int userType = 0;

    if (sdrDatanew.HasRows)
    {
        if (sdrDatanew.Read())
        {
            userType = Convert.ToInt32(sdrDatanew["User_Type"].ToString());
        }
    }

    switch (userType)
    {
        case 0:
            Response.Redirect("Lic_Gen.aspx");
            break;
        case 1:
            Response.Redirect("Cust_Page.aspx");
            break;
        default:
            Console.WriteLine("Invalid User/Password");
            Console.ReadLine();
            break;
    }

    connew.Close();

这篇关于在数据库中搜索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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