等于运算符中不兼容的数据类型text和nvarchar

时间:2019-04-25 07:33:03

标签: c# sql-server

  

text和nvarchar的数据类型在等于运算符中不兼容。

我在登录页面上收到此消息的次数。该如何处理?

class loginDAL
{
    static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;
    public bool loginCheck(loginBLL l)
    {
        bool isSuccess = true;
        SqlConnection conn = new SqlConnection(myconnstrng);

        try
        {
            string sql = "SELECT *  FROM tbl_users WHERE username=@username AND password=@password AND user_type=@user_type";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@username", l.username);
            cmd.Parameters.AddWithValue("@password", l.password);
            //I got error on this next line
            **cmd.Parameters.AddWithValue("@user_type", l.user_type);**

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                isSuccess = true;
            }
            else
            {
                isSuccess = false;
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
        }
        return isSuccess;
    }
  }
}

0 个答案:

没有答案