从TOpenDialog在Delphi中将文件路径作为字符串传递 [英] Passing file path in Delphi from TOpenDialog as a string
问题描述
我正在尝试使用TOpenDialog,以便将指向所选文件的路径传递到AdoConection并将Excel文件的内容加载到表中.我目前正在尝试下面的代码,但是代码的最后部分未连接到Excel并返回错误: [dcc32错误] sample_map.pas(80):E2010不兼容的类型:字符串"和"TOpenDialog"
I'm trying to make use of the TOpenDialog in order to pass the path to selected file to the AdoConection and load the content of the Excel file to the table. I'm currently attempting the code below but the last part of the code does not connect to the Excel returning an error: [dcc32 Error] sample_map.pas(80): E2010 Incompatible types: 'string' and 'TOpenDialog'
procedure TForm1.Button1Click(Sender: TObject);
var
openDialog : TOpenDialog; // Open dialog variable
strConn : WideString; // Declare wide string for the connection
begin
// Create the open dialog object - assign to our open dialog variable
openDialog := TOpenDialog.Create(self);
// Set up the starting directory to be the current one
openDialog.InitialDir := GetCurrentDir;
// Only allow existing files to be selected
openDialog.Options := [ofFileMustExist];
// Allow only .dpr and .pas files to be selected
openDialog.Filter :=
'Excel 2003 and older|*.xls|Excel 2007 and older|*.xlsx';
// Select pascal files as the starting filter type
openDialog.FilterIndex := 2;
// Display the open file dialog
if openDialog.Execute
then ShowMessage('File : '+openDialog.FileName)
else ShowMessage('Open file was cancelled');
// Free up the dialog
openDialog.Free;
// Connect the Excel file
strConn:='Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + openDialog + ';' +
'Extended Properties=Excel 8.0;';
AdoConnection1.Connected:=False;
AdoConnection1.ConnectionString:=strConn;
end;
推荐答案
openDialog
是文件对话框的实例.它不是字符串.您需要像下面这样读取文件对话框对象的 FileName
属性:
openDialog
is an instance of a file dialog. It is not a string. You need to read the FileName
property of the file dialog object like this:
openDialog.FileName
实际上,您已经在您的 ShowMessage
调用中使用了它.
In fact you already use that in one of your ShowMessage
calls.
请确保在调用 Free
(问题代码中存在的错误)之前先阅读此属性.
Do make sure that you read this property before calling Free
, a mistake present in the code in the question.
实际上,您确实需要养成使用 try/finally
保护资源的习惯.每当创建类的实例时,都需要确保即使面对异常也将其销毁.在您的代码中,您需要这样编写:
In fact you do need to get in to the habit of using try/finally
to protect resources. Any time you create an instance of a class you need to make sure that it will be destroyed even in the face of an exception. In your code you need to write it like this:
openDialog := TOpenDialog.Create(self);
try
.... // use openDialog to let user choose file:
strConn := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + openDialog.FileName + ';' +
'Extended Properties=Excel 8.0;';
finally
openDialog.Free; // executes no matter what, even if exception raised, etc.
end;
我也不认为您需要在此处使用 WideString
.如果使用Unicode Delphi,则可以使用本机 string
类型,它是 UnicodeString
的别名.如果您的Delphi是Unicode之前的版本,那么在这种情况下,您也可以安全地使用 string
,这是 AnsiString
的别名.您使用的文字是ASCII.文件对话框是ANSI控件,因此 openDialog.FileName
也是ANSI.使用 WideString
无法获得任何好处.
I also don't think you need to use WideString
here. If you use a Unicode Delphi then you can use the native string
type which is an alias for UnicodeString
. If your Delphi is pre-Unicode, then you can also safely use string
, an alias for AnsiString
in that case. The literals you use are ASCII. The file dialog is an ANSI control and so openDialog.FileName
is also ANSI. Nothing to be gained using WideString
.
最后,您要混合使用一个功能,选择文件名的代码和用于数据库连接的代码.最好将关注点分开.创建一个仅返回文件名的方法,该方法是通过让用户通过对话框进行选择而获得的.并添加一种用于数据库连接的方法,该方法将文件名作为参数传递.
Finally, you are mixing up, all in one function, code to select a filename, and code to work on a database connection. It is better to separate concerns. Create a method that simply returns a filename, obtained by letting the user choose through a dialog. And add a method to work on the database connection, that is passed a filename as a parameter.
这篇关于从TOpenDialog在Delphi中将文件路径作为字符串传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!