我有一个Windows ATM应用程序,我被困在这一部分:
我将pin
和firstName
作为私有字符串,并将其作为私有双重余额。
但是,当我尝试在文本区域中显示余额时,它只会打印0
。是因为retrieveAccountInformation()
因返回而无效吗?如何更改我的代码,以便我可以在其他方法中使用pin
,firstName
和balance
?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLDll;
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
private Connection myConnection;
private Statement myStatement;
private ResultSet myResultSet;
String databaseURL = "http://www.boehnecamp.com/phpMyAdmin/razorsql_mysql_bridge.php";
public string pin, firstName, userAccountNumber;
public double balance;
public double withdraw = 0;
public double deposit = 0;
bool buttonClicked;
bool buttonClicked2;
public Form1()
{
InitializeComponent();
try
{
//connect to database
SQL sql = new SQL();
myConnection = sql.getConnection(databaseURL);
//create Statement for executing SQL
myStatement = myConnection.createStatement(databaseURL);
loadAccountNumbers();
updateBalance();
}
catch (Exception)
{
Console.WriteLine("Cannot connect to database server");
}
//close statement and database connection
myStatement.close();
myConnection.close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void setText(string text)
{
}
//load account numbers to ComboBox
private void loadAccountNumbers()
{
//get all account numbers from database
try
{
myResultSet = myStatement.executeQuery("SELECT accountNumber FROM accountInformation");
// add account numbers to ComboBox
while (myResultSet.next())
{
accountNumberComboBox.Items.Add(myResultSet.getString("accountNumber"));
}
myResultSet.close(); // close myResultSet
}//end try
catch (Exception)
{
Console.WriteLine("Error in loadAccountNumbers");
}
}//end method to loadAccountNumbers
private void retrieveAccountInformation()
{
//get account info
try
{
myResultSet = myStatement.executeQuery("SELECT pin, " +
"firstName, balanceAmount FROM accountInformation " +
"WHERE accountNumber = '" + userAccountNumber + "'");
//get next result
if (myResultSet.next())
{
pin = myResultSet.getString("pin");
firstName = myResultSet.getString("firstName");
balance = myResultSet.getDouble("balanceAmount");
}
myResultSet.close(); //close myResultSet
}//end try
catch (Exception)
{
Console.WriteLine("Error in retrieveAccountInformation");
}
}// end method retrieveAccountInformation
//update database after withdrawing
private void updateBalance()
{
//update balance in database
try
{
myStatement.executeUpdate("UPDATE accountInformation" +
" SET balanceAmount = " + balance + " WHERE " +
"accountNumber = '" + userAccountNumber + "'");
}
catch (Exception)
{
Console.WriteLine("Error in updateBalace");
}
}//end method updateBalance
private void accountNumberComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
accountNumberComboBox.Enabled = false;
numberTextField.Text = " ";
messageTextArea.Text = "Enter your PIN #.";
button0.Enabled = true;
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
buttonDone.Enabled = true;
retrieveAccountInformation();
}
private void button1_Click(object sender, EventArgs e)
{
setText("1");
numberTextField.Text += "1";
buttonEnter.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
setText("2");
numberTextField.Text += "2";
buttonEnter.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
setText("3");
numberTextField.Text += "3";
buttonEnter.Enabled = true;
}
private void button4_Click(object sender, EventArgs e)
{
setText("4");
numberTextField.Text += "4";
buttonEnter.Enabled = true;
}
private void button5_Click(object sender, EventArgs e)
{
setText("5");
numberTextField.Text += "5";
buttonEnter.Enabled = true;
}
private void button6_Click(object sender, EventArgs e)
{
setText("6");
numberTextField.Text += "6";
buttonEnter.Enabled = true;
}
private void button7_Click(object sender, EventArgs e)
{
setText("7");
numberTextField.Text += "7";
buttonEnter.Enabled = true;
}
private void button8_Click(object sender, EventArgs e)
{
setText("8");
numberTextField.Text += "8";
buttonEnter.Enabled = true;
}
private void button9_Click(object sender, EventArgs e)
{
setText("9");
numberTextField.Text += "9";
buttonEnter.Enabled = true;
}
private void button0_Click(object sender, EventArgs e)
{
setText("0");
numberTextField.Text += "0";
buttonEnter.Enabled = true;
}
private void buttonEnter_Click(object sender, EventArgs e)
{
numberTextField.Text = " ";
buttonEnter.Enabled = false;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
button0.Enabled = false;
buttonBalance.Enabled = true;
buttonWithdraw.Enabled = true;
buttonDeposit.Enabled = true;
messageTextArea.Text = "Welcome," + firstName;
updateBalance();
if (buttonClicked == true)
{
withdraw = Double.Parse(numberTextField.Text);
balance = balance - withdraw;
updateBalance();
}
}
private void buttonBalance_Click(object sender, EventArgs e)
{
retrieveAccountInformation();
updateBalance();
messageTextArea.Text=(balance.ToString());
//display balance to messageTextArea
}
private void buttonWithdraw_Click(object sender, EventArgs e)
{
buttonBalance.Enabled = false;
buttonWithdraw.Enabled = false;
button0.Enabled = true;
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
messageTextArea.Text = "Enter withdrawal amount:";
buttonClicked = true;
}
private void buttonDone_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
button0.Enabled = false;
buttonEnter.Enabled = false;
buttonBalance.Enabled = false;
buttonWithdraw.Enabled = false;
buttonDone.Enabled = false;
buttonDeposit.Enabled = false;
accountNumberComboBox.Enabled = true;
messageTextArea.Text = "Please select your account number.";
}
private void buttonDeposit_Click(object sender, EventArgs e)
{
buttonBalance.Enabled = false;
buttonWithdraw.Enabled = false;
button0.Enabled = true;
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
messageTextArea.Text = "Enter deposit amount:";
buttonClicked2 = true;
}
}
}
答案 0 :(得分:0)
因为您使用的是myResultSet.next()
。
如果您有两个SQL语句,例如
SELECT * FROM Users
Go
SELECT * FROM Accounts
Go
if (myResultSet.next())
- >这将读取从第二个SQL语句返回的数据。
我想这可能是你想要的:
string connString = AppConfigurationManager.GetAppSetting("ConnectionString",IsQA);
using (var conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand sqlCommand = new SqlCommand("SELECT pin, " +
"firstName, balanceAmount FROM accountInformation " +
"WHERE accountNumber = '" + userAccountNumber + "'", conn))
{
sqlCommand.CommandType = CommandType.Text;
SqlDataReader dr = null;
try
{
dr = sqlCommand.ExecuteReader();
while (dr.Read())
{
pin = dr.getString("pin");
firstName = dr.getString("firstName");
balance = dr.getDouble("balanceAmount");
}
}
catch (SqlException ex)
{
}
catch (Exception ex)
{
}
finally
{
//clean up resources that access Data
if (dr != null)
{
dr.Close();
}
}
}
答案 1 :(得分:0)
你可以在我看来使用“out”或“ref”
private void retrieveAccountInformation(out string pin, out string firstname, out double balance)
{
//get account info
try
{
myResultSet = myStatement.executeQuery("SELECT pin, " +
"firstName, balanceAmount FROM accountInformation " +
"WHERE accountNumber = '" + userAccountNumber + "'");
//get next result
if (myResultSet.next())
{
pin = myResultSet.getString("pin");
firstName = myResultSet.getString("firstName");
balance = myResultSet.getDouble("balanceAmount");
}
myResultSet.close(); //close myResultSet
}//end try
catch (Exception)
{
Console.WriteLine("Error in retrieveAccountInformation");
}
}// end method retrieveAccountInformation
private void buttonBalance_Click(object sender, EventArgs e)
{
string pin = "";
string firstname = "";
double balance = 0;
retrieveAccountInformation(out string pin, out string firstname, out double balance);
updateBalance();
messageTextArea.Text=(balance.ToString());
//display balance to messageTextArea
}
答案 2 :(得分:-1)
因为pin,firstName,balance是Local Variable,并且此方法是private void,所以如果你想从void方法获取值。使用in,out变量。这使得一个方法可以返回多个值
Morever,updateBalance()做什么?它声明了varial并调用了retrieveAccountInformation方法????
示例:
private void retrieveAccountInformation(out string pin,out string firstname,out double balance)
{
//get account info
try
{
myResultSet = myStatement.executeQuery("SELECT pin, " +
"firstName, balanceAmount FROM accountInformation " +
"WHERE accountNumber = '" + userAccountNumber + "'");
//get next result
if (myResultSet.next())
{
pin = myResultSet.getString("pin");
firstName = myResultSet.getString("firstName");
balance = myResultSet.getDouble("balanceAmount");
}
myResultSet.close(); //close myResultSet
}//end try
catch (Exception)
{
Console.WriteLine("Error in retrieveAccountInformation");
}
}// end method retrieveAccountInformation
private void buttonBalance_Click(object sender, EventArgs e)
{
string pin, firstname;
double balance;
retrieveAccountInformation(pin, firstname, balance);
messageTextArea.Text=(balance.ToString());
//display balance to messageTextArea
}