回線でエラーが発生し、そのaConnection.Open()
理由がわかりません。この正確なコードは機能していましたが、基本的に同じコードを別のクラスに追加して、値を Vehicle テーブルに挿入しようとしたところ、このエラーが発生し始めました。そのため、動作していたときに持っていたものをすべて削除しましたが、[保存] をクリックしてもエラーが発生します。何か案は?発見と解決にすべてのリソースを使い果たしました。ありがとう!
データレイヤー
public class Data
{
public static Business anApplicant;
static SqlConnection aConnection = null;
public static void initializeConnection(SqlConnection aDbConnection)
{
aConnection = aDbConnection;
aConnection.Open();
}
// Method for Inserting Applicant information into the database
public static void InsertApplicant()
{
try
{
SqlCommand myCommand = new SqlCommand("INSERT INTO Applicant (FirstName, LastName, Address, City, State, Zip, Phone, Gender, BirthDate)" +
"VALUES ('" + anApplicant.FName + "', '" + anApplicant.LName + "', '" + anApplicant.Address + "', '" + anApplicant.City + "', '" + anApplicant.State +
"', '" + anApplicant.Zip + "', '" + anApplicant.Phone + "', '" + anApplicant.Gender + "', '" + anApplicant.BirthDate + "')", aConnection);
if (aConnection.State == ConnectionState.Closed)
aConnection.Open();
myCommand.ExecuteNonQuery();
aConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
}
}
ビジネスレイヤー
public class Business
{
SqlConnection aConnection = new SqlConnection("Data Source=zac2424-HP; Initial Catalog=Final; Trusted_Connection=True;");
public void initializeConnection()
{
Data.initializeConnection(aConnection);
}
private string policyNumber;
private string fName;
private string lName;
private string address;
private string city;
private string state;
private string zip;
private string phone;
private string gender;
private string birthDate;
public Business(string fName, string lName, string address,
string city, string state, string zip, string phone, string gender, string birthDate)
{
FName = fName;
LName = lName;
Address = address;
City = city;
State = state;
Zip = zip;
Phone = phone;
Gender = gender;
BirthDate = birthDate;
}
public Business()
{
}
// Applicant Get and Set Method
public string PolicyNumber
{
get { return policyNumber; }
set { policyNumber = value; }
}
public string FName
{
get { return fName; }
set { fName = value; }
}
public string LName
{
get { return lName; }
set { lName = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public string City
{
get { return city; }
set { city = value; }
}
public string State
{
get { return state; }
set { state = value; }
}
public string Zip
{
get { return zip; }
set { zip = value; }
}
public string Phone
{
get { return phone; }
set { phone = value; }
}
public string Gender
{
get { return gender; }
set { gender = value; }
}
public string BirthDate
{
get { return birthDate; }
set { birthDate = value; }
}
string premium = "";
public string Premium
{
get { return premium; }
set { premium = value; }
}
}
プレゼンテーション層
public partial class PolicyHomeForm : Form
{
public PolicyHomeForm()
{
InitializeComponent();
}
private void PolicyHomeForm_Load(object sender, EventArgs e)
{
}
public void saveButton_Click(object sender, EventArgs e)
{
Data.anApplicant = new Business(txtFirstName.Text, txtLastName.Text, txtAddress.Text, txtCity.Text, comboState.Text, txtZip.Text, txtPhone.Text,
comboGender.Text, txtBirthDate.Text);
//Data.aVehicle = new Vehicle(comboMake.Text, txtModel.Text, txtYear.Text, txtDesc.Text);
Data.InsertApplicant();
//Data.InsertVehicle();
}
}