我已经从教师课程中添加了代码,如果相关,请告诉我 添加Person.cs也是如此。 `当我尝试将一个教师添加到数据库时,我正试图弄清楚如何访问该集并获取我的Address.cs类的方法。教师继承自Person.cs,其中person包含Address对象,因此可以使用Address.cs的set和get方法。但是,我不能在我的Instructor类中使用那些set和get方法,除了通过向Instructor添加Address对象,或者可能包括/覆盖set / get in Person。 我的问题是,在教师类的代码中设置地址(String street,String city,String state,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
}
}
`
答案 0 :(得分:0)
没有看到您的代码的硬诊断。但是,您是否制作了属性方法public
。
例如:
public class Person
{
public Address Address { get; set; }
...
}
public class Instructor : Person
{
public void DoSomething()
{
Address.City = "Wellington";
}
}
答案 1 :(得分:0)
如果要从子类访问这些属性,而不是其他属性。那怎么样呢
public class Person
{
protected Address Address {get;set;}
}
public class Instructor : Person
{
public void Instructor (int i, String f, String l, Address add, Schedule sc, String o, String e)
{
Address = add;
}
}
这里重点是来自protected
类的Person
修饰符。您可以从here