无法通过WCF服务访问Silverlight中的数据库

本文关键字:Silverlight 数据库 访问 服务 WCF | 更新日期: 2023-09-27 17:58:46

我正在创建一个登录页面,其中应该从数据库中检查用户名和密码。我有一个登录表,其中包含用户名密码

我的代码显示在这里

WCF服务-LogiService.cvs.cs

public class LoginService
{
    [OperationContract]
    public int ValidateUsers(string username, string password)
    {
        int count;
        string connection = ("Data Source=(localdb);Initial Catalog=smsdb;
        Integrated Security=True;Connect Timeout=30;Encrypt=False;
        TrustServerCertificate=False");
        SqlConnection conn = new SqlConnection(connection);
        conn.Open();
        SqlCommand comm = new SqlCommand("[Login_Authentication]", conn);
        SqlParameter para = new SqlParameter("@username", username);
        comm.Parameters.Add(para);
        SqlParameter para1 = new SqlParameter("@password", password);
        comm.Parameters.Add(para1);
        comm.CommandType = CommandType.StoredProcedure;
        count = (int)comm.ExecuteScalar();
        return count;
        conn.Close();
    }
    // Add more operations here and mark them with [OperationContract]
}    

和我的XAML.CS

private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (uname.Text == "" && pass.Password == "")
        {
            MessageBox.Show("Enter Username and password");
        }
        else
        {
            var obj = new MyLoginService.LoginServiceClient();
            obj.ValidateUsersCompleted += new EventHandler
            <ValidateUsersCompletedEventArgs>(obj_ValidateUsersCompleted);
            obj.ValidateUsersAsync(uname.Text, pass.Password);
        }
    }
    public void obj_ValidateUsersCompleted
    (object sender, slwcftut.MyLoginService.ValidateUsersCompletedEventArgs e)
    {
        try
        {
            if (e.Result == 1)
            {
                MessageBox.Show("Logged in successfully");
            }
            else if (e.Result <= 0)
            {
                MessageBox.Show("Incorrect Username or Password");
            }
        }
        catch(Exception ex)
        {
        }
    }

我没有得到任何错误或答案。

无法通过WCF服务访问Silverlight中的数据库

我认为您的服务工作正常,

       MyLoginService.LoginServiceClient LoginClient=  new MyLoginService.LoginServiceClient();
           {
            LoginClient.ValidateUsersCompleted  += (a, ae) =>
            {
               if(ae.Error == null)
               {
               if(ae.Result != null)
               {
                if(ae.Result == 1)
                    {
                     MessageBox.Show("Logged in successfully");
                    }
                    else if (ae.Result <= 0)
                    {
                    MessageBox.Show("Incorrect Username or Password");
                    }
                }
              }
              else
              {
                    MessageBox.Show("Error occured from service");
              } 
            };
            LoginClient.ValidateUsers(uname.Text, pass.Password);
        }

您是否尝试在LogiService服务中使用断点,例如在中

   count = (int)comm.ExecuteScalar();

看看它是否返回null,看看它是否在连接到db时出错,所以我建议使用像这样的try-catch语句

   try
    {
        using (MySqlConnection conn = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand())
            {
                string sql = "SELECT Count(comment.Topic_Id) FROM comment inner join topic on(comment.Topic_Id=topic.id) group by topic.id limit " + start + ",10";
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                MySqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    numberOfcomments[i] = rdr.GetInt16(0);
                    i++;
                }
            }
        }
        return numberOfcomments;
    }
    catch(Exception ex)
    {
        return ex.Message;
    }

constr是我的字符串连接,在ex中。消息中,你可以看到一个字符串,说明可能发生的任何错误,希望这能有所帮助。