【问题标题】:C# get sql table column value and put it in a variableC#获取sql表列值并将其放入变量中
【发布时间】:2021-06-26 21:52:40
【问题描述】:

我有一个用户登录菜单。我想根据用户的级别重定向用户。级别数据位于 SQL 表中。我想根据用户名从表中获取级别数据并将其分配给变量。

protected void btnDefault_Click(object sender, EventArgs e)
{
//filter entered text
string strUserName = Tools.checkSQLInjection(txtUserName.Text).Trim();
string strPassword = Tools.checkSQLInjection(txtPassword.Text);
string strError = "";

//Get Dealer Level Value
SqlCommand command = new SqlCommand("SELECT dealerLvl FROM Users where email='" + strUserName + "'");
string strDealerLvl = "dealerLvl".ToString();
int intDealerLvl;
bool isParsable = Int32.TryParse(strDealerLvl, out intDealerLvl);

if (strDealerLvl == "1")
  {  Response.Redirect("/dealers/dashboard"); }
else if (strDealerLvl == "2")
  {  Response.Redirect("/dealers/dashboard-2"); }

【问题讨论】:

  • 您的代码易受 SQL 注入攻击。请使用参数化查询。
  • 是的,除非您删除 sql 注入漏洞,否则很少有人能够帮助您,因为没有好的答案会包含上面的代码。
  • 不要依赖自制的sql注入预防工具。参数化查询是一种经过验证的系统,可以避免 sql 注入。 (而且你的代码会更清晰,更不容易出现语法错误,并且可以通过sql引擎进行优化)
  • 但是,您似乎需要阅读一些关于如何创建选择命令、执行它并阅读结果的教程。例如:dotnettutorials.net/lesson/ado-net-sqlcommand-class

标签: c# sql asp.net


【解决方案1】:
            using(SqlCommand command = new SqlCommand("SELECT dealerLvl FROM Users where email= @strUserName", connection))
            {
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@strUserName", strUserName);
                DataSet ds = new DataSet();
                using(SqlDataAdapter da = new SqlDataAdapter(command))
                    da.Fill(ds);
        
                //Get the result of the first row        
                DataRow dr = ds.Tables[0].Rows[0];
        
               //Get the value of the column in the first row        
               string strDealerLvl = dr["dealerLvl"].ToString();    
            }

【讨论】:

  • 参数化你的查询,并使用using处理对象
  • @Charlieface,感谢您编辑了我的答案。
  • 感谢您的帮助。欣赏它,我得到了它的工作。
【解决方案2】:

您似乎没有检查密码,但也许应该稍后再检查。

执行此操作的有效代码存根如下所示:

DataTable MyTable = new DataTable();
int intDealerLvl = 0;

using (SqlCommand cmdSQL = new SqlCommand("SELECT dealerLv1 FROM Users where email = @meail", 
          new SqlConnection(My.Settings.test3ConnectionString)))
{
    cmdSQL.Parameters.Add("@email", SqlDbType.NVarChar).Value = strUserName;
    cmdSQL.Connection.Open();
    MyTable.Load(cmdSQL.ExecuteReader);
}

if (MyTable.Rows.Count > 0)
    intDealerLvl = MyTable.Rows(0)(0);

switch (intDealerLvl)
{
    case 1:
        {
            Response.Redirect("/dealers/dashboard");
            break;
        }

    case 2:
        {
            Response.Redirect("/dealers/dashboard-2"); 
            break;
        }

    default:
        {
            // no level found - where to go??
            break;
        }
}

但是,不清楚您是否应该检查密码,如果是,那么我们当然会使用:

DataTable MyTable = new DataTable();
string strSQL;
strSQL = "SELECT dealerLv1 FROM Users where email = @Email and Password = @Pass";

using (SqlCommand cmdSQL = new SqlCommand(strSQL, 
          new SqlConnection(My.Settings.test3ConnectionString)))
{
cmdSQL.Parameters.Add("@email", SqlDbType.NVarChar).Value = strUserName;
cmdSQL.Parameters.Add("@Pass", SqlDbType.NVarChar).Value = strPassword;
cmdSQL.Connection.Open();
MyTable.Load(cmdSQL.ExecuteReader);

if (MyTable.Rows.Count > 0)
    intDealerLvl = MyTable.Rows(0)(0);

【讨论】:

  • 感谢您的帮助。我让它工作了。感谢您的指导。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2014-10-24
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多