努力使用WCF验证和验证用户登录

时间:2017-04-30 20:38:42

标签: c# wcf webforms

我正在尝试从用户检索数据(登录详细信息),将其与存储在SQL Server数据库中的数据进行比较,使用WebForms和C#with WCF。

继承我的代码: Service1.cs

 public int LoginMember(Member m)
    {
        SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
        string sqlStr = "select Username, Password from Members where Username =@Username and PassWord=@Password";
        con.Open();
        SqlCommand cmd = new SqlCommand(sqlStr, con);
        cmd.Connection = con;

        System.Data.SqlClient.SqlDataReader dataReader;


        dataReader = cmd.ExecuteReader();
        if (dataReader.Read())
        {
            return 1;
        }
            return 0;

Logincs.cs

    private void button1_Click(object sender, EventArgs e)
    {
        Member m = new Member();
        Service1Client service = new Service1Client();

        int result = service.LoginMember(m);

        if (result > 0)
        {
            this.Hide();
            Shop sh = new Shop();
            sh.ShowDialog();
            MessageBox.Show("Login Successful!");
        }
        else
        {
            MessageBox.Show("Invalid Login");
        }

我已经添加了cmd参数:

 public int LoginMember(Member m)
    {
        SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
        string sqlStr = "select Username, Password from Members where Username =@Username and PassWord=@Password";
        SqlCommand cmd = new SqlCommand(sqlStr, con);
        cmd.Parameters.AddWithValue("@Username", m.Username);
        cmd.Parameters.AddWithValue("@PassWord", m.Password);
        con.Open();

        cmd.Connection = con;

        System.Data.SqlClient.SqlDataReader dataReader;


        dataReader = cmd.ExecuteReader();
        if (dataReader.Read())
        {
            return 1;
        }
        return 0;
    }

但我收到此错误: enter image description here

有谁能告诉我我的代码有什么问题? 提前谢谢!

更新: 我现在收到此错误: New Error

0 个答案:

没有答案