Sql server 不同职位的用户登录

Sql server 不同职位的用户登录,sql-server,visual-studio-2013,Sql Server,Visual Studio 2013,我对项目的登录功能有点陌生,我正在尝试为我的组登录,该组由3个用户组成,即护士、患者和药剂师。我想我即将完成loin过程,但我的LoginDAO.cs中的一个方法getPosition()有问题。到目前为止,我还没有为病人和药剂师做任何登录代码,因为我需要我的队友的零件才能工作,但下面显示的是我所做的。不知何故,login(stringnric,stringpw)起作用,但getPosition(stringnric)不起作用。这是我从错误日志中得到的错误: 异常:必须声明标量变量“@paraN

我对项目的登录功能有点陌生,我正在尝试为我的组登录,该组由3个用户组成,即护士、患者和药剂师。我想我即将完成loin过程,但我的LoginDAO.cs中的一个方法getPosition()有问题。到目前为止,我还没有为病人和药剂师做任何登录代码,因为我需要我的队友的零件才能工作,但下面显示的是我所做的。不知何故,login(stringnric,stringpw)起作用,但getPosition(stringnric)不起作用。这是我从错误日志中得到的错误: 异常:必须声明标量变量“@paraNRIC”。来源:LoginDAO.getPosition 提前感谢:D

protected void btnLogin_Click(object sender, EventArgs e)
{
    login login = new login();
    login.nric = tbLoginID.Text;
    login.pw = tbPassword.Text;
    if (login.userLogin(login.nric, login.pw))
    {
        if (login.getPosition(login.nric) == "Nurse")
        {
            Response.Redirect("Nurse.aspx");
        }
        else if (login.getPosition(login.nric) == "Patient")
        {
            Response.Redirect("Patient.aspx");
        }
        else if (login.getPosition(login.nric) == "Pharmacist")
        {
            Response.Redirect("PharmacistDisplay.aspx");
        }
    }
    else
    {
        lblErr.Text = "Invalid account.";
    }
}

public bool login(string nric, string pw)
{
    bool flag = false;
    SqlCommand cmd = new SqlCommand();
    StringBuilder sqlStr = new StringBuilder();

    sqlStr.AppendLine("SELECT Password from Position");
    sqlStr.AppendLine("Where NRIC = @paraNRIC");
    try
    {
        SqlConnection myconn = new SqlConnection(DBConnect);
        cmd = new SqlCommand(sqlStr.ToString(), myconn);

        cmd.Parameters.AddWithValue("@paraNRIC", nric);

        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if (dt == null)
        {
            flag = false;
        }
        else
        {
            string dbhashedpw = dt.Rows[0]["Password"].ToString();
            flag = Helper.VerifyHash(pw, "SHA512", dbhashedpw);
        }
    }
    catch (Exception exc)
    {
        logManager log = new logManager();
        log.addLog("NurseDAO.login", sqlStr.ToString(), exc);
    }


    return flag;
}

public string getPosition(string nric)
{
    string dbPosition = "";
    int result = 0;
    SqlCommand cmd = new SqlCommand();

    StringBuilder sqlStr = new StringBuilder();
    sqlStr.AppendLine("SELECT Position from Position ");
    sqlStr.AppendLine("where NRIC = @paraNRIC");

    cmd.Parameters.AddWithValue("@paraNRIC", nric);
    try
    {
        SqlConnection myconn = new SqlConnection(DBConnect);
        cmd = new SqlCommand(sqlStr.ToString(), myconn);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);

        myconn.Open();
        result = cmd.ExecuteNonQuery();
        dbPosition = dt.Rows[0]["Position"].ToString();
        myconn.Close();
    }
    catch (Exception exc)
    {
        logManager log = new logManager();
        log.addLog("LoginDAO.getPosition", sqlStr.ToString(), exc);
    }


    return dbPosition;
`}

你可以换这条线

sqlStr.AppendLine(“其中NRIC=@paranic”)

对此

sqlStr.AppendLine(“其中NRIC='”+NRIC+'”)

并完全避免使用参数。

您的错误如下:

SqlCommand cmd = new SqlCommand();
// lines omitted
cmd.Parameters.AddWithValue("@paraNRIC", nric);
try
{
    SqlConnection myconn = new SqlConnection(DBConnect);
    cmd = new SqlCommand(sqlStr.ToString(), myconn);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
请注意,您正在实例化
cmd
两次。代码将参数添加到第一个
SqlCommand
实例,但执行第二个实例

要解决此问题,请确保在调用的SqlCommand实例上声明参数:

public string getPosition(string nric)
{
    string dbPosition = "";
    int result = 0;
    // remove this line: SqlCommand cmd = new SqlCommand();

    StringBuilder sqlStr = new StringBuilder();
    sqlStr.AppendLine("SELECT Position from Position ");
    sqlStr.AppendLine("where NRIC = @paraNRIC");
    // move parameter declaration until after you declare cmd
    try
    {
        SqlConnection myconn = new SqlConnection(DBConnect);
        SqlCommand cmd = new SqlCommand(sqlStr.ToString(), myconn);
        // add the parameters here:
        cmd.Parameters.AddWithValue("@paraNRIC", nric);
        // code continues

我正在使用visual Studio 2013提供的sql server数据库@一匹没有名字的马嗯。。。刚刚尝试了该方法,但错误日志显示“异常:无效列名'S123455A'。其中S123455A是我输入的nric