こんにちは、私はC#を使用していて、データベースへのアクセスと変数の割り当てなどに成功しましたが、もう一度アクセスしたいのですが、何らかの理由でこの時点で失敗しています。
OleDbDataReader reader = command.ExecuteReader();
これは、皆さんがよりよく理解するのに役立つ可能性のあるコードスニペットです。誰かが私が間違っているかもしれないことを指摘することができれば、私は深く感謝するでしょう。
//#########################
// DATABASE OPERATIONS
//#########################
// Create the database connections
string usersConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kronix\Documents\theitguy.accdb");
OleDbConnection theitguyDBConn = new OleDbConnection(usersConnString);
//==============================
// Populate Customers Table
//==============================
try
{
// Open theitguy database connection
theitguyDBConn.Open();
// Select the fields you want to retrieve from in the database
string selectString = "SELECT ID, Name, Surname, Address, Town, County, Postcode, HomeNumber, MobileNumber, Email FROM Customers";
OleDbCommand command = new OleDbCommand(selectString, theitguyDBConn);
//Send the CommandText to the connection, and then build an OleDbDataReader.
//Note: The OleDbDataReader is forward-only.
OleDbDataReader reader = command.ExecuteReader();
// PROCESS THE DATABASE AND ADD THEM TO THE LISTS FOR USE LATER IN THE PROGRAM
while (reader.Read())
{
custID.Add(reader["ID"].ToString());
custName.Add(reader["Name"].ToString());
custSurname.Add(reader["Surname"].ToString());
custAddress.Add(reader["Address"].ToString());
custTown.Add(reader["Town"].ToString());
custCounty.Add(reader["County"].ToString());
custPostcode.Add(reader["Postcode"].ToString());
custHomeNumber.Add(reader["HomeNumber"].ToString());
custMobileNumber.Add(reader["MobileNumber"].ToString());
custEmail.Add(reader["Email"].ToString());
}
// Dispose of the data once used
reader.Dispose();
reader.Close();
// Close the database connection
theitguyDBConn.Close();
}
catch (Exception ex)
{
Console.Write("ERROR 201 (Form2): Error reading Customers table in theitguy Database\n");
}
//==============================
// Populate Repairs Table
//==============================
try
{
// Open theitguy database connection
theitguyDBConn.Open();
// Select the fields you want to retrieve from in the database
string selectString = "SELECT ID, CustID, Name, Surname, DateIn, Device, Colour, ContactNumber1, ContactNumber2, EstimatedCost, ReportedProblem, Diagnostics, EngineerRemarks, WorkCompleted, PartsUsed, PartsCost, PartsID, Engineer, TotalCost, DateCompleted FROM Repairs";
OleDbCommand command = new OleDbCommand(selectString, theitguyDBConn);
//Send the CommandText to the connection, and then build an OleDbDataReader.
//Note: The OleDbDataReader is forward-only.
OleDbDataReader reader = command.ExecuteReader(); //###IT'S FAILING HERE!!!###
// PROCESS THE DATABASE AND ADD THEM TO THE LISTS FOR USE LATER IN THE PROGRAM
while (reader.Read())
{
repID.Add(reader["ID"].ToString());
repCustID.Add(reader["ID"].ToString());
repName.Add(reader["ID"].ToString());
repSurname.Add(reader["ID"].ToString());
repDateIn.Add(reader["ID"].ToString());
repDevice.Add(reader["ID"].ToString());
repColour.Add(reader["ID"].ToString());
repContactNumber1.Add(reader["ID"].ToString());
repContactNumber2.Add(reader["ID"].ToString());
repEstimatedCost.Add(reader["ID"].ToString());
repReportedProblem.Add(reader["ID"].ToString());
repDiagnostics.Add(reader["ID"].ToString());
repEngineerRemarks.Add(reader["ID"].ToString());
repWorkCompleted.Add(reader["ID"].ToString());
repPartsUsed.Add(reader["ID"].ToString());
repPartsCost.Add(reader["ID"].ToString());
repPartsID.Add(reader["ID"].ToString());
repEngineer.Add(reader["ID"].ToString());
repTotalCost.Add(reader["ID"].ToString());
repDateCompleted.Add(reader["ID"].ToString());
}
// Dispose of the data once used
reader.Dispose();
reader.Close();
// Close the database connection
theitguyDBConn.Close();
}
catch (Exception ex)
{
Console.Write("ERROR 202 (Form2): Error reading Repairs table in theitguy Database\n");
}