C#代码错误与SQL Server的东西

时间:2015-04-17 19:33:54

标签: c#

我在我的应用程序上使用Access数据库并且我切换到SQL Server,但它无法正常运行...这是一个登录屏幕,Connect按钮单击事件。

当我连接它时,只需跳过并关闭登录窗口,其中包含我输入的任何信息。

这是我得到的错误。 它在cmd领域是红色的,我不知道为什么。

http://i.stack.imgur.com/gJ5hm.png

代码:

private void btnconectar_Click(object sender, EventArgs e)
    {
        if (!(empty(boxlogin.Text) && empty(boxsenha.Text)))
        {
        SqlCommand cmd = new SqlCommand("SELECT * from Usuarios where StrCmp(login, '" + boxlogin.Text + "')=0  and StrCmp(senha, '" + boxsenha.Text + "')=0",connection);
        cmd.CommandType = CommandType.Text;
        cmd.CommandTimeout = 15; 
        connection.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            nome =  reader["nome"].ToString();
            login = reader["login"].ToString();
            senha = reader["senha"].ToString();
            msg("Login realizado com sucesso!\nBem vindo(a), " + nome.Substring(0, nome.IndexOf(" ")),Color.Green, false);
                connection.Close();
                timer4.Start();
        }
        else
        {
            msg("Usuário e/ou senha incorretos!", Color.Red, false);
        }
        }
        else msg("Os campos não podem ficar em branco!", Color.Red, false);
        connection.Close();
    }

执行后输出:

  

类型' System.Data.SqlClient.SqlException'的第一次机会异常。   发生在System.Data.dll' WindowsFormsApplication1.vshost.exe' (CLR   v4.0.30319:WindowsFormsApplication1.vshost.exe):已加载   ' C:\的Windows \ Microsoft.Net \组件\ GAC_MSIL \ mscorlib.resources \ v4.0_4.0.0.0_pt-BR_b77a5c561934e089 \ mscorlib.resources.dll&#39 ;.   模块是在没有符号的情况下构建的。

2 个答案:

答案 0 :(得分:4)

当你得到SqlException时,问题不在于标记的行。它将与ExecuteReader一致,执行查询。

T-SQL中没有StrCmp函数,使用=运算符来比较字符串。

您应该使用数据参数,当您将值连接到查询中而不正确地转义它们时,查询对于SQL注入攻击是敞开的。

要求填写这两个字段,您应该在条件中使用||运算符,而不是&&运算符。

您没有关闭数据阅读器,而您正在关闭连接两次。您应该处置命令和数据读取器。安全处理它的最方便的方法是使用using块。

您不应在生产代码中使用select *。选择要从查询中获取的字段。

private void btnconectar_Click(object sender, EventArgs e) {
  if (!(empty(boxlogin.Text) || empty(boxsenha.Text))) {
    using (SqlCommand cmd = new SqlCommand("SELECT nome, login, senha from Usuarios where login = @Login and senha = @Senha", connection)) {
      cmp.Parameters.AddWithValue("@Login", boxlogin.Text);
      cmp.Parameters.AddWithValue("@Senha", boxsenha.Text);
      cmd.CommandType = CommandType.Text;
      cmd.CommandTimeout = 15; 
      connection.Open();
      using (SqlDataReader reader = cmd.ExecuteReader()) {
        if (reader.Read()) {
          nome =  reader["nome"].ToString();
          login = reader["login"].ToString();
          senha = reader["senha"].ToString();
          msg("Login realizado com sucesso!\nBem vindo(a), " + nome.Substring(0, nome.IndexOf(" ")),Color.Green, false);
          timer4.Start();
        } else {
          msg("Usuário e/ou senha incorretos!", Color.Red, false);
        }
      }
    } else {
      msg("Os campos não podem ficar em branco!", Color.Red, false);
    }
  }
  connection.Close();
}

答案 1 :(得分:0)

我认为问题可能是StrCmp无效sql。

在SQL Server中,您可以使用=比较两个varchar的相等性。