我創建了一個WCF的「登錄」Windows窗體應用程序, 如何發送輸入用戶名和密碼到WCF並檢查用戶名是否在SQL?c#WCF服務 - 如何獲得文本框輸入值
private void loginbt_Click(object sender, EventArgs e)
{
string username = textBox1.Text;
string password = textBox2.Text;
//check username and password are not empty
if (username.Trim().Length != 0 && password.Trim().Length != 0)
{
checkPassword.CustomerServiceClient service= new checkPassword.CustomerServiceClient();
var enterPassword = service.checkPassword(username, password);
//check input value here with WCF?
}
}
我得到Index was outside the bounds of the array.
例外,當我添加string getUsername = enterPassword[0].name;
。它看起來像WCF沒有從文本框中獲得輸入值,換句話說checkPassword(null,null)。
public Customer[] checkPassword(string name, string password){
List<Customer> customers = new List<Customer>();
SqlConnection connection = new SqlConnection(
"Data Source = 11111; Initial Catalog = 1111;" +
"User ID = 1111; Password = 11111");
connection.Open();
SqlCommand command = new SqlCommand("select customerId, customerName, address, password, phone from Customer where customerName='"+name+"'", connection);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read()){
Customer newCustomer = new Customer(reader.GetString(1), reader.GetString(3));
string correctPW = reader.GetString(3);
if (correctPW == password)
{
customers.Add(newCustomer);
}
}
reader.Close();
connection.Close();
return customers.ToArray();
}
對不起,我真的很困惑這個問題,希望你能理解我的問題,謝謝你的幫助。
我不太清楚你的問題是什麼。你到目前爲止取得了什麼成就,以及你錯過了什麼。它看起來像你的代碼已經做了你想要的:「service.checkPassword(username,password)」。 –
我得到了「索引超出了數組的界限」。當我添加「string getUsername = enterPassword [0] .name;」時出現異常。它看起來像WCF沒有從文本框中獲得輸入值,換句話說checkPassword(null,null)。 – user2893761
在service.checkPassword(用戶名,密碼)處設置一個斷點,然後找出這些值是否正確,如果它們是正確的,那麼它們應該傳遞給WCF服務,並且存在服務問題。現在您必須調試WCF客戶端和服務。 –