C#SQLCommand不起作用

时间:2012-05-30 15:01:13

标签: c# sql sqlcommand

我有一个带有以下方法的WebService:

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public string Login(string passwort, string email, string firma)
    {
        return LoginHelper.Login(passwort, email, firma);
    }

我的LoginHelper代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace WebService1
{
    public class LoginHelper
    {
        public static string Login(string passwort, string email, string firma)
        {
            string userName = "";

            SqlConnection con = new SqlConnection(@"Data Source=Yeah-PC\SQLEXPRESS;Initial Catalog=works;Integrated Security=true;");

        SqlCommand cmd = new SqlCommand(@"SELECT firma FROM TestData 
                                  WHERE email = @email", con);
        cmd.Parameters.AddWithValue("@email", email);

        con.Open();

        SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
      {
   //userName += dr["email"].ToString();
   //userName += dr["passwort"].ToString();
   userName += dr["firma"].ToString();
     }
    dr.Close();
    con.Close();
    return userName;
        }



    }
}

感谢帮助伙伴们

我已编辑了我的问题。这个解决方案现在安全吗?我的意思是反对SQL注入。还有更多我能做得更好的事情吗?

5 个答案:

答案 0 :(得分:6)

您正在呼叫LoginHelper.Login(passwort, email, firma);

但是在你的方法中

public static string Login(string email, string passwort, string firma)

电子邮件是拳头参数。

实际上在电子邮件参数中你有密码,这就是为什么它没有返回任何结果

更改login中的LoginHelper方法,如下所示

public static string Login(string passwort, string email, string firma)
{
    string userName = "";

    using (SqlConnection con = new SqlConnection(@"Data Source=Yeah-PC\SQLEXPRESS;Initial Catalog=works;Integrated Security=true;"))
    using(SqlCommand cmd = new SqlCommand(@"SELECT firma FROM TestData WHERE email = @email", con))
    {
        cmd.Parameters.AddWithValue("@email", email);
        con.Open();
        using (SqlDataReader rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {
                if (rdr["firma"]  != DBNull.Value)
                {
                    userName += rdr["firma"].ToString();
                }

            }
        }
    }

    return userName;
}

答案 1 :(得分:3)

如果您的电子邮件地址包含@字符,则可能是您的问题。 @是SQLCommand的参数标记。它会认为您的电子邮件地址的后半部分是一个sql参数。您需要使用参数传递电子邮件地址。这也可以保护您免受SQL注入。 Akatakritos的答案有一个如何将电子邮件作为参数传递的例子。

答案 2 :(得分:1)

另外,出于安全性和性能原因,您应该使用SqlParameters。阅读SQL注入攻击。

string userName = "";

SqlConnection con = new SqlConnection(@"Data Source=Yeah-PC\SQLEXPRESS;Initial Catalog=works;Integrated Security=true;");

SqlCommand cmd = new SqlCommand(@"SELECT firma FROM TestData 
                                  WHERE email = @email" con);
cmd.Parameters.AddWithValue("@email", email);

con.Open();

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
   //userName += dr["email"].ToString();
   //userName += dr["passwort"].ToString();
   userName += dr["firma"].ToString();
}
dr.Close();
con.Close();
return userName;

答案 3 :(得分:0)

根据其他答案&评价。

  

如果您不打算使用ORM(实体,则存在安全问题)   Framework / NHibernate / etc ...)请使用参数化查询

解决您的问题:

  • 数据库中有数据吗?
  • 你指的是正确的数据库吗?
  • 你的SQL是否正确?
  • 您的SQL是否正在运行?
  • 运行SQL事件探查器并查看正在运行的SQL,然后在SQL Management Studio中对其进行测试

答案 4 :(得分:0)

不要在代码中传递参数,而是尝试使用Sqlparameter添加参数。最佳做法是使用SQL参数添加参数。 您还可以通过调试来检查电子邮件的价值......您是否传递了正确的信息。

        SqlConnection conn = new SqlConnection(connectionString);
        conn.Open();
        SqlCommand cmd = new SqlCommand(@"SELECT firma FROM TestData 
                              WHERE email = @email" conn);
        cmd.Parameters.AddWithValue("@email", email);                     
        cmd.Prepare();
        cmd.ExecuteNonQuery();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
            {                  
               userName += dr["firma"].ToString();

            }
        dr.Close();
        conn.Close();
相关问题