|
||
| Inside Technique : Sokoban Level Designer : Client Data Access with HTML Applications : FileNew function In this section, we will be looking at the FileNew function. As in the previous section, because of the size of the function, we will be chopping it into steps, each followed by a detailed explanation of the code within these steps. Step 1 : Open the Common Dialog
function FileNew(){
//Decide which directory in which to open CommDlg
DP=TDC1.dataURL;
if(DP=='')
DataPath=location.pathname.substr(1)
else
DataPath=DP.substring(0,DP.lastIndexOf('\\')
//Initialize CommDlg properties
CommonDialog1.DialogTitle="Create Data File";
CommonDialog1.Filter="Data File (*.dat)|*.dat";
CommonDialog1.DefaultExt="dat";
CommonDialog1.InitDir=DataPath;
//Open CommDlg
try{
CommonDialog1.ShowSave();
}
//An Error has occured
catch (exception){
//Strip high word from error number
errnum=exception.number & 0xFFFF;
//Check if user has pressed Cancel
if(errnum==32755)
return
//Some other error, display Error Message
//and return
else{
alert('ERROR:\n\n'+exception.description);
return;
}
}
...continued...
The first step of this function is almost exactly the same as the first step for the FileOpen function, but for 2 important differences. The first is that the DefaultExt property of the Common Dialog is set to "dat", which means that if the user enters a file name without an extension, the ".dat" extension is automatically added by the Common Dialog. The other important difference is that instead of the ShowOpen method, we call the ShowSave method of the Common Dialog. This method displays the default Save File As dialog. Step 2 : Open the ADO Connection
//Set DataFile and DataFilePath
DataFile=CommonDialog1.FileTitle;
if(DataFile=="")
return;
DataFilePath=CommonDialog1.FileName;
//Set DSN-less Connection String
DSNStr=
"DRIVER={Microsoft Text Driver (*.txt; *.csv)}; DBQ="+
DataFilePath.substring(0,DataFilePath.lastIndexOf('\\'));
//If Previous Connection is Open, Close it
if(ADOConn.State==1)
ADOConn.Close();
//Open ADO Connection
try{
ADOConn.Open(DSNStr);
}
catch(exception){
errnum=exception.number & 0xFFFF;
//ODBC Error - Path Not Found
if(errnum==16389){
alert('The path you have specified does not exist');
return;
}
else{
//Some other error
alert('ERROR:\n\n'+exception.description);
return;
}
}
...continued...
This is exactly the same as Step 2 in the FileOpen function Step 3 : Write new Data File to Disk
//Build SQL CREATE TABLE string
CreateStr='CREATE TABLE '+DataFile+
'(FLDA INTEGER,FLDB INTEGER,FLDC TEXT)';
//Execute CREATE TABLE command
try{
ADOConn.Execute(CreateStr)
}
catch(exception){
errnum=exception.number & 0xFFFF;
//File Already Exists error
if(errnum==3604){
alert('Data File already exists!');
return;
}
else{
alert('ERROR:\n\n'+exception.description);
return;
}
}
TDC1.DataURL=DataFilePath;
TDC1.Reset();
The code then builds an SQL Create Table string, using the file name previously obtained as the table name. The file name is followed by a sample schema consisting of 3 fields, you would replace this structure with your custom schema. The SQL statement is then executed using the Execute method of the connection Object. The error handler for this statement first checks to see if the File Already Exists error occurred, in which case it displays a custom error message. Finally, if everything went smoothly, the TDC is reset to point to the newly created data file. When the file is created, another file 'SCHEMA.INI' is also created along with it in the same directory. This file holds information about the structure of the data file, and is used internally by the Text Driver. However, you need not worry about it, because CSV tables don't require a schema file, and even if the user deletes this file, nothing will happen. In the next section, we will look at the FileSave function. Page 1:Sokoban Level Designer : Client Data Access with HTML Applications © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |