背景:我真的很新,刚开始在学校学习德尔福。我试图在Microsoft Access中创建一个数据库,所以我不知道很多。我正在关注我的老师示例,但在我这样做时遇到了访问冲突错误。我在学校的电脑上,所以我不认为这与电脑有什么关系。
老师给出的例子:
$('<td>').text(parseFloat(item.Price).toFixed(4)),
我知道上面的内容不是一个正确的例子,如果它没用,我会删除它,我只是展示了我正在工作的例子。字幕只与表格上的其他标签等有关。
我的工作:
unit uDatabaseDDL;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, DB, ADODB, ComObj, jpeg; {Need to add ComObj to Uses}
type
TfrmDatabase = class(TForm)
lblHeading: TLabel;
pnlExit: TPanel;
adoConDatabase: TADOConnection;
adoCommDatabase: TADOCommand;
adotblStudent: TADOTable;
lblInfo: TLabel;
edtDatabaseName: TEdit;
lblDatabaseName: TLabel;
btnCreateDatabase: TButton;
lblDatabaseLocation: TLabel;
btnCreateTables: TButton;
imgERDiagram: TImage;
lblTablesMessage: TLabel;
procedure pnlExitClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure btnCreateDatabaseClick(Sender: TObject);
procedure btnCreateTablesClick(Sender: TObject);
private
public
end;
var
frmDatabase: TfrmDatabase;
implementation
{$R *.dfm}
procedure TfrmDatabase.btnCreateDatabaseClick(Sender: TObject);
var dbase : OLEVariant;
nameDatabase : string;
begin
nameDatabase := edtDatabaseName.Text + '.accdb';
if not fileexists(nameDatabase)then
begin //Database stored in Debug folder of the program.
dbase := CreateOLEObject('ADOX.Catalog');
dbase.create('Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+nameDatabase+';');
dbase := null; //Deletes created object as not required any more.
lblDatabaseLocation.Caption := 'Database saved in ' + ExtractFilePath(ParamStr(0));
end
else
ShowMessage('The database ' + nameDatabase + ' already exists');
adoCommDatabase.ConnectionString := 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+
nameDatabase+';';
btnCreateTables.Visible := true; //Makes create table button visisble.
lblTablesMessage.Caption := 'Tables to be created are based on the diagram below';
end;
procedure TfrmDatabase.btnCreateTablesClick(Sender: TObject);
begin
//Fields that are Yes/No is MS Access are defined in DDL as YesNo, e.g. SPWord YesNo,
adoCommDatabase.CommandText := 'CREATE TABLE tblStudent (StudentID real, '+
'StudentName char(30), SPWord char(20), primary key(StudentID))';
adoCommDatabase.Execute; //Creates the student table.
adoCommDatabase.CommandText := 'CREATE TABLE tblTeacher (TeacherID short, '+
'TeacherName char(30), TPWord char(20), primary key(TeacherID))';
adoCommDatabase.Execute; //Creates the teacher table.
adoCommDatabase.CommandText := 'CREATE TABLE tblCourse (CourseID short, '+
'CourseTitle char(30), LengthCourse char(20), LevelOfCourse char(20), '+
'primary key(CourseID))';
adoCommDatabase.Execute; //Creates the course table.
adoCommDatabase.CommandText := 'CREATE TABLE tblEnrolment (StudentID real, '+
'TeacherID short, CourseID short, DateEnrolment Date, '+
'Foreign Key(StudentID) references tblStudent(StudentID), '+
'Foreign Key(TeacherID) references tblTeacher(TeacherID), '+
'Foreign Key(CourseID) references tblCourse(CourseID), '+
'Primary Key(CourseID, StudentID, TeacherID))'; {Creates Compound Key}
adoCommDatabase.Execute; //Creates the enrolment table.
end;`
错误:无论我在ADOCommand.ConnectionString行上得到什么错误。 无论数据库是否存在,都会发生这种情况。 $ 76ABAA12的第一次机会例外。异常类EAccessViolation,消息'模块'Project1.exe'中地址0051FA1E的访问冲突。读取地址00000390'。处理Project1.exe(1928)。
我的代码说明:我已经查看了其他教程,但我不确定它是否适用于我的问题。我已经看到访问冲突通常与未正确创建的对象有关,我认为这是问题,因为零的数量通常表明这一点。但是,我已将ADOQuery,ADOConnection,ADOCommand放在表单上,并且它位于类的已发布部分中,因此我相信它已经被正确创建和销毁。表单也是项目的一部分,并自动处理项目设置。或者那是错的?我不想从我的老师示例中更改代码,只是想知道它为什么不起作用。
我想知道为什么我会遇到访问冲突以及如何解决访问冲突问题。