【问题标题】:Count from SQL Rows into C# textbox从 SQL 行计数到 C# 文本框
【发布时间】:2013-03-08 22:30:01
【问题描述】:

大家好,这是第一次使用stackoverflow所以大家好L)

我是 C# 表单的初学者,我把它当作一种有趣的爱好。

SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM Members where sponser = "
        +textbox1.text+"'", connection);

 Int32 count = (Int32)comm.ExecuteScalar();
 textbox2.Text ="Found "+ count+" Members;

它只是我从谷歌 xD 获得的 2 个代码之间的混合 错误怎么会出现在这里 textbox2.Text ="Found "+ count+" Members;

【问题讨论】:

  • 这段代码容易被SQL注入。使用 SqlParameter。
  • 这不是你要问的,但第一行代码是一个非常的坏事。它让您对所谓的 SQL 注入攻击敞开心扉。如果您像这样使用 ADO.NET,则需要使用参数化查询:codinghorror.com/blog/2005/04/…
  • @ken2k 我认为大多数发布此类代码的人都不在乎。
  • @Romoku:我认为大多数发布此类代码的人知道理解,这值得修复。
  • 除了易受 SQL 注入攻击之外,它还可能是错误的根源——等号后缺少单引号。

标签: c# count


【解决方案1】:

这行代码有几个问题:

textbox2.Text ="Found "+ count+" Members;

首先,存在语法错误。你永远不会关闭第二组报价。你会这样做:

textbox2.Text ="Found "+ count+" Members";

但是,像这样的字符串连接仍然有点混乱。您有两个文字字符串,并且您试图将它们添加到一个整数中,这并不完全直观(并且可能比需要的要慢)。相反,请考虑使用格式化字符串:

textbox2.Text = string.Format("Found {0} Members", count);

这将从count(它是一个整数)中获取值,并在string.Format() 函数内部识别其字符串表示并将其插入到格式化字符串的占位符中。

更新: 负责处理编译时错误。现在你会得到一个运行时错误:

SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM Members where sponser = "
    +textbox1.text+"'", connection);

一旦您尝试执行该 SQL 语句,您就会从数据库中得到一个错误,因为生成的查询有语法错误:

SELECT COUNT(*) FROM Members where sponser = some text'

您缺少参数的开头单引号。像这样的:

SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM Members where sponser = '"
    +textbox1.text+"'", connection);

然而,这是重要的,你还没有完成。这行代码对一个非常常见且易于利用的漏洞SQL Injection 开放。您将希望摆脱直接的字符串连接并为 SQL 查询使用参数。像这样的:

SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Members where sponser = @sponser");
cmd.Parameters.Add("@sponser", textbox1.text);
Int32 count = (Int32)comm.ExecuteScalar();

要知道,您仍然可以做很多事情来改进这一点,这些都值得随着时间的推移而学习。您可以查看的内容有:

  • 在您尝试在 SQL 查询中使用之前检查和验证用户输入 (textbox1.text)。
  • 在尝试将其直接转换为 Int32 之前检查 comm.ExecuteScalar() 的输出(如果由于某种原因它返回整数以外的任何值,则会出现运行时错误)。
  • 考虑使用 Linq to Sql 之类的东西来代替 ADO.NET 组件,因为它可以用更少的代码为您做更多的事情。

【讨论】:

  • 你现在会得到一个不同的错误。你有两个文字字符串,你试图将它们添加到一个整数!如果我没记错的话,计数将自动转换为字符串,其中使用 (+) 为字符串
  • @user2148395:我已经用更多信息更新了答案,因为您还会遇到其他问题。
  • @Akrem:有趣,看起来你是对的。我会更新答案。但是,string.Format() 仍然是这里的路。
【解决方案2】:
protected void Page_Load(object sender, EventArgs e)
{
    lb1.Text = GetRecordCount(textbox2.Text).ToString();
}

private int GetRecordCount(string myParameter)
{
    string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ToString();
    Int32 count = 0;
    string sql = "SELECT COUNT(*) FROM members WHERE sponsor = @Sponsor";
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@Sponsor", SqlDbType.VarChar);
        cmd.Parameters["@Sponsor"].Value = myParameter;
        try
        {
            conn.Open();
            count = (Int32)cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {

        }
    }
    return (int)count;
}

【讨论】:

    【解决方案3】:

    最后你缺少一个结束符:

    textbox2.Text ="Found "+ count+" Members";
    

    【讨论】:

      【解决方案4】:

      您的代码容易受到SQL Injections 的攻击。请考虑使用Parameters

      private int GetMemberCount(string connectionString, string sponsor)
      {
          using(var connection = new SqlConnection(connectionString))
          using(var command = connection.CreateCommand())
          {
              command.CommandText = "SELECT COUNT(*) FROM members WHERE sponsor = @Sponsor";
              command.Parameters.AddWithValue("@Sponsor", sponsor);
      
              return Convert.ToInt32(command.ExecuteScalar());
          }
      }
      
      //Usage
      var sponsor = textbox1.text;
      var count = GetMemberCount(connectionString, sponsor);
      
      textbox2.Text = string.Format("Found {0} Members", count);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多