我已经添加了来自 Instructor 类的代码,如果它也与添加 Person.cs 相关,请告诉我。`在尝试将讲师添加到数据库时,我在试图弄清楚如何访问我的 Address.cs 类的 set 和 get 方法时遇到问题。讲师从 Person.cs 继承,其中 person 包含一个 Address 对象,因此可以使用 Address.cs 的 set 和 get 方法。但是,我不能在我的 Instructor 类中使用这些 set 和 get 方法,只能向 Instructor 添加一个 Address 对象,或者可能在 Person 中包含/覆盖 set/get。我的问题是,在 Instructor 类的代码中设置地址(字符串街道、字符串城市、字符串状态、int Zip)的正确/最佳方法是什么?这样我就可以使用数据库插入查询中的get方法
"INSERT * where Id=(" ...........get methods")"
/*********************************
* William Tolliver
* June 18, 2013
* C# lab #2 Gui
* instructor Ronald Enz
* *******************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrjLab2
{
class Instructor : Person
{
#region Fields
public System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter;
public System.Data.OleDb.OleDbCommand OleDbSelectCommand;
public System.Data.OleDb.OleDbCommand OleDbInsertCommand;
public System.Data.OleDb.OleDbCommand OleDbUpdateCommand;
public System.Data.OleDb.OleDbCommand OleDbDeleteCommand;
public System.Data.OleDb.OleDbConnection OleDbConnection;
private String office;
public String cmd;
private Address addr;
private Schedule sch;
#endregion
#region Constructors
public Instructor() { }
public Instructor(int i) { SelectDB(i); }
public Instructor(int i, String f, String l, Address add, Schedule sc, String o, String e)
: base(f, l, e, i, add, sc)
{
office = o;
addr = add;
sch = sc;
insertDB();
}
#endregion
#region Set/Get methods
public String Office
{
get { return office; }
set { office = value; }
}
#endregion
#region Display method
public override void display()
{
Console.WriteLine(" First Name: " + FName + "\n Last Name: " + LName + "\n ID: " + Id + "\n" + addr.ToString() + "\n Office: " + Office + "\n Email:" + Email);
}
#endregion
#region Database setup
//============================= BEHAVIORS =========================
//++++++++++++++++ DATABASE Data Elements +++++++++++++++++
public void DBSetup()
{
// +++++++++++++++++++++++++++ DBSetup function +++++++++++++++++++++++++++++++
OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
OleDbSelectCommand = new System.Data.OleDb.OleDbCommand();
OleDbInsertCommand = new System.Data.OleDb.OleDbCommand();
OleDbUpdateCommand = new System.Data.OleDb.OleDbCommand();
OleDbDeleteCommand = new System.Data.OleDb.OleDbCommand();
OleDbConnection = new System.Data.OleDb.OleDbConnection();
//OleDbDataAdapter1
OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand;
OleDbDataAdapter.InsertCommand = OleDbInsertCommand;
OleDbDataAdapter.SelectCommand = OleDbSelectCommand;
OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand;
OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" +
"ocking Mode=1;Data Source=C:\\Users\\William\\Documents\\Visual Studio 2012\\Lab3\\RegistrationDB.mdb;J" +
"et OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System datab" +
"ase=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=S" +
"hare Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet " +
"OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" +
"r=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";
} //end DBSetup()
#endregion
#region Database selection
public void SelectDB(int i)
{ //++++++++++++++++++++++++++ SELECT +++++++++++++++++++++++++
DBSetup();
cmd = "Select * from Instructors where ID = " + i;
OleDbDataAdapter.SelectCommand.CommandText = cmd;
OleDbDataAdapter.SelectCommand.Connection = OleDbConnection;
Console.WriteLine(cmd);
try
{
OleDbConnection.Open();
System.Data.OleDb.OleDbDataReader dr;
dr = OleDbDataAdapter.SelectCommand.ExecuteReader();
dr.Read();
Id = i;
FName = dr.GetValue(1).ToString();
LName = dr.GetValue(2).ToString();
addr = new Address(dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), Int32.Parse(dr.GetValue(6).ToString()));
Office = dr.GetValue(7).ToString();
Email = dr.GetValue(8).ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
OleDbConnection.Close();
}
} //end SelectDB()
#endregion
#region Database insertion
public void insertDB()
{
DBSetup();
cmd = "INSERT into Instructors values(" + Id + "," + "'" + FName + "'," + "'" + LName + "'," + "'" + addr.Street + "'," + "'" + addr.City + "'," + "'" + addr.State + "'," + "'" + addr.Zip + "'," + "'" + Office + "'," + "'" + Email + "'" + ")";// cmd = "INSERT into Instructors values( Id, FName, LName, addr.Street, addr.City, addr.State,addr.Zip,Office,Email )";
OleDbDataAdapter.InsertCommand.CommandText = cmd;
OleDbDataAdapter.InsertCommand.Connection = OleDbConnection;
Console.WriteLine(cmd);
try
{
OleDbConnection.Open();
int n = OleDbDataAdapter.InsertCommand.ExecuteNonQuery();
if (n == 1)
Console.WriteLine("Data Inserted");
else
Console.WriteLine("ERROR: Inserting Data");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
OleDbConnection.Close();
}
} //end InsertDB()
#endregion
}
}
`