我对一个项目的目标是让教师从 MS Access 数据库表中删除 Instructor
其中 ID = 获取 ID
现在我在表格上收到一个错误,上面写着
使用未赋值的局部变量 'iD' C:\Users\Tina\documents\visual studio 2013\Projects\Students\Students\DeleteInstructor.cs 29 24 学生
讲师班:
class Instructor : Person
{
private int iD;
private String office;
private String eMail;
private String message;
public Instructor() : base()
{
this.iD = 0;
this.office = "";
this.eMail = "";
}
public Instructor(int i, String off, String eM) : base()
{
this.iD = i;
this.office = off;
this.eMail = eM;
InsertDB();
}
public Instructor(int iD)
{
SelectDB(iD);
}
//++++++++++++++++ DATABASE Data Elements +++++++++++++++++
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;
public string cmd;
public void DBSetup(){
// +++++++++++++++++++++++++++ DBSetup function +++++++++++++++++++++++++++
// This DBSetup() method instantiates all the DB objects needed to access a DB,
// including OleDbDataAdapter, which contains 4 other objects(OlsDbSelectCommand,
// oleDbInsertCommand, oleDbUpdateCommand, oleDbDeleteCommand.) And each
// Command object contains a Connection object and an SQL string object.
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();
OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand;
OleDbDataAdapter.InsertCommand = OleDbInsertCommand;
OleDbDataAdapter.SelectCommand = OleDbSelectCommand;
OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand;
OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Reg"+
"istry Path=;Jet OLEDB:Database L" +
"ocking Mode=1;Data Source=c:\RegistrationMDB.accdb;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";
}
public void SelectDB(int id)
{ //++++++++++++++++++++++++++ SELECT +++++++++++++++++++++++++
DBSetup();
cmd = "Select * from Instructors where ID = " + iD;
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=iD;
setOffice(dr.GetValue(1)+"");
setEMail(dr.GetValue(2)+"");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
OleDbConnection.Close();
}
} //end SelectDB()
public void InsertDB() {
// +++++++++++++++++++++++++++ INSERT +++++++++++++++++++++++++++++++
DBSetup();
cmd = "INSERT into Instructors values(" + getID() + "," +
"'" + getOffice() + "'," +
"'" + getEMail() + ")";
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();
}
}
public void updateDB()
{
//++++++++++++++++++++++++++ UPDATE +++++++++++++++++++++++++
cmd = "Update Instructors set ID = '" + getID() + "'," +
"Office = '" + getOffice() + "', " +
"EMail = '" + getEMail() +
" where ID = " + getID();
OleDbDataAdapter.UpdateCommand.CommandText = cmd;
OleDbDataAdapter.UpdateCommand.Connection = OleDbConnection;
Console.WriteLine(cmd);
try
{
OleDbConnection.Open();
int n = OleDbDataAdapter.UpdateCommand.ExecuteNonQuery();
if (n==1)
Console.WriteLine("Data Updated");
else
Console.WriteLine("ERROR: Updating Data");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
OleDbConnection.Close();
}
} //end UpdateDB()
public void deleteDB(int iD)
{
//++++++++++++++++++++++++++ DELETE +++++++++++++++++++++++++
cmd = "Delete from Instructors where ID = " + getID();
OleDbDataAdapter.DeleteCommand.CommandText = cmd;
OleDbDataAdapter.DeleteCommand.Connection = OleDbConnection;
Console.WriteLine(cmd);
try
{
OleDbConnection.Open();
int n = OleDbDataAdapter.DeleteCommand.ExecuteNonQuery();
if (n==1)
Console.WriteLine("Data Deleted");
else
Console.WriteLine("ERROR: Deleting Data");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
OleDbConnection.Close();
}
}
public void setID(int iD)
{
this.iD = iD;
}
public void setOffice(String office)
{
this.office = office;
}
public void setEMail(String eMail)
{
this.eMail = eMail;
}
public int getID()
{
return iD;
}
public String getOffice()
{
return office;
}
public String getEMail()
{
return eMail;
}
public String getMessage()
{
return this.message;
}
public void displays(){
System.Console.WriteLine("ID = "+ getID());
System.Console.WriteLine("Office = "+ getOffice());
System.Console.WriteLine("Email = " + getEMail());
}
}
形式:
namespace Students
{
public partial class DeleteInstructor : Form
{
public DeleteInstructor()
{
InitializeComponent();
}
private void InstructorIDText_TextChanged(object sender, EventArgs e)
{
}
private void Delete_Click(object sender, EventArgs e)
{
int iD;
Instructor s = new Instructor(iD);
s.deleteDB(iD);
}
}
}
错误很明显。尚未为变量 iD 赋值。您需要先设置它,然后再在删除方法中使用它。
private void Delete_Click(object sender, EventArgs e)
{
int iD = 1;
Instructor s = new Instructor(iD);
s.deleteDB(iD);
}
我在这里以"1"为例。它可以从控件或用户所做的选择中获取,基本上是从用户那里收到的一些输入。
错误是绝对正确的。在调用变量 iD 之前,您不会为它分配任何值。
private void Delete_Click(object sender, EventArgs e)
{
int iD; // -> here it is unassigned
Instructor s = new Instructor(iD);
s.deleteDB(iD);
}
你应该分配一个值,比如
int iD = 0;
或者从 DataGrid 或 TextBox 或 Combobox 等位置获取值
int iD = Convert.ToInt32(textBox1.Text);