我正在創建一個登錄頁面,用戶名和密碼應該從數據庫中檢查。 我有一個表LoginTable與用戶名和密碼無法通過WCF服務訪問Silverlight中的數據庫?
我的代碼如下所示
WCF服務 - LoginService.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)
{
}
}
我沒有收到任何錯誤或答案。
您是否在測試客戶端嘗試了您的服務?它工作嗎? – Sajeetharan
@Sajeetharan我沒有WCF測試客戶端 – Dhinesh
什麼?它帶有Visual Studio。轉到Visual Studio命令提示符並鍵入WCFTestClient – Sajeetharan