删除数据库 mdb 表单中的表
Posted
技术标签:
【中文标题】删除数据库 mdb 表单中的表【英文标题】:Delete a table in the database mdb form 【发布时间】:2015-06-25 17:06:13 【问题描述】:我的项目到期目标是让讲师从 MS Access 数据库表 Instructor
中删除,其中 ID = get 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);
【问题讨论】:
您要删除哪个讲师?这个 Instructor 的 ID 应该来自哪里。用户是从下拉列表还是列表视图中选择? 它来自数据库。比如说讲师 ID:1,我只想删除讲师“1”的所有信息的行。在数据库中称为ID。 【参考方案1】:错误很明显。您尚未为变量 iD 赋值。您需要在删除方法中使用它之前对其进行设置。
private void Delete_Click(object sender, EventArgs e)
int iD = 1;
Instructor s = new Instructor(iD);
s.deleteDB(iD);
我在这里以“1”为例。它可以从控件或用户所做的选择中获取,基本上是从用户那里接收到的一些输入。
【讨论】:
【参考方案2】:这个错误是绝对正确的。在调用它之前,您没有为变量 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);
【讨论】:
以上是关于删除数据库 mdb 表单中的表的主要内容,如果未能解决你的问题,请参考以下文章