【问题标题】:Make condition right using SqlDataReader?使用 SqlDataReader 使条件正确?
【发布时间】:2020-04-13 14:45:44
【问题描述】:
var a = "1";
var b = "2";
var c = "3";

var name = authResult.ExtraData["email"];

string connectionString = null;

SqlConnection cnn;
SqlCommand cmd;
string sql = null;
SqlDataReader reader;

connectionString = "Data Source = dj0043\\sqlexpress; Initial Catalog = XXXX; Integrated Security = True";
sql = "Select EmployeeRoles.RoleId From EmployeeList Inner Join EmployeeRoles on EmployeeList.EmployeeId = EmployeeRoles.EmployeeId Where EmailId = name";

cnn = new SqlConnection(connectionString);

try
{
    cnn.Open();
    cmd = new SqlCommand(sql, cnn);
    reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        if (reader == a)
        {
            return Redirect(Url.Action("Employee", "Home"));
        }
        else if (sql == b)
        {
            return Redirect(Url.Action("Index", "Home"));
        }
        else if (sql == c)
        {
            return Redirect(Url.Action("Index", "Home"));
        }
    }

    reader.Close();
    cmd.Dispose();
    cnn.Close();
}

在这种情况下,如果任何用户拥有 1 id 将被重定向到该页面,反之亦然。

谁能告诉我如何让SqlDataReader 在这种情况下工作?

【问题讨论】:

  • 此条件无效,显示错误

标签: c# sql model-view-controller ado.net ado


【解决方案1】:

首先,您的 SQL 查询不正确。您应该将@name 参数发送到查询中。

我更喜欢这样做,因为它更干净

var a = "1";
var b = "2";
var c = "3";

var name = authResult.ExtraData["email"];
var connectionString = "Data Source = dj0043\\sqlexpress; Initial Catalog = XXXX; Integrated Security = True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
           connection.Open();
           using (SqlCommand cmd = connection.CreateCommand())
           {
               cmd.CommandText = "Select EmployeeRoles.RoleId From EmployeeList Inner Join EmployeeRoles on EmployeeList.EmployeeId = EmployeeRoles.EmployeeId Where EmailId = @name";
               cmd.CommandType = CommandType.Text;
               cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;


               using (SqlDataReader reader = cmd.ExecuteReader())
               {
                    while (reader.Read())
                    {
                       if (reader["RoleId"].ToString() == a)
                       {
                          return Redirect(Url.Action("Employee", "Home"));
                       }
                       else if (reader["RoleId"].ToString() == b)
                       {
                          return Redirect(Url.Action("Index", "Home"));
                       }
                       else if (reader["RoleId"].ToString() == c)
                       {
                          return Redirect(Url.Action("Index", "Home"));
                       }
                    }
               }
          }
}

【讨论】:

  • 此条件不起作用,它显示错误
  • 直到在 if 条件下显示 int 错误。我认为因为我正在初始化的 var 是字符串类型并且无法将数据读取器值转换为 intonint 并且无法比较....如果有任何其他原因建议和
  • 我已经对 int 进行了更改,但它显示了无法绑定空引用异常的错误
  • @RizwanWani 我已通过将数据类型更改为字符串来更新我的答案。空引用问题可能来自代码中的空对象。检查您的连接字符串是否有效。然后确保 'authResult.ExtraData["email"]' 返回正确的值。您的 roleId 是否有可能为空?
  • No role id is not null ...在 authResult.Extradata[''email''] 我收到用户的电子邮件但是...我不知道为什么会显示这个问题因为阅读器没有被执行调试器没有调试阅读器。
猜你喜欢
  • 2013-08-10
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 2014-10-05
  • 1970-01-01
  • 2023-03-20
  • 2016-08-14
  • 2019-07-01
相关资源
最近更新 更多