我正在學校項目中工作,並且無法將一段數據從Access數據庫轉換爲可傳遞給C#中第二個窗體的字符串。我知道與數據庫的連接正在工作,並且我在其中引用了正確的表來獲取信息,所以我不確定我在做什麼錯誤。它不會在代碼中顯示任何錯誤,但是每次運行應用程序時,都會崩潰,因爲它無法從string accountnumber = reader["Account_Number"].ToString();
行的字符串的數據庫中找到值。有什麼我做錯了嗎?C#無法將數據庫值轉換爲字符串
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * from User_Info where Username='" +txt_Username.Text+ "' and Password='" +txt_Password.Text+ "'";
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
string accountnumber = reader["Account_Number"].ToString();
while (reader.Read())
{
count = count+1;
}
if (count == 1)
{
MessageBox.Show("Login Successful!", "Success!");
connection.Close();
connection.Dispose();
this.Hide();
User_Account_Screen UAS = new User_Account_Screen();
UAS.Number = accountnumber;
UAS.ShowDialog();
你真的應該使用參數化查詢。做'X ='+ someTextBox.Text +'''總是一件壞事 –
什麼是數據庫中的帳號列?它必須是Account_Number。 –
你能分享三個確切的錯誤信息嗎?你需要從數據庫返回確切的一行,你應該把所有的代碼放在'if(reader.Read()){string accountNumber = ......; MessageBox.Show .....等等你不需要'count'變量。 –