Create Datasource Name Dynamically?
I am writing an application where the user will be given an empty database to begin with. The application will require a connection to the database, which will be named however the users wishes.
I have created a form, which when submitted will write a plain text file with one single word and no formatting. That one single word will be used as the datasource name.
So these are the files I have.
<!---datasource.txt--->
FreshDb
<!---read_settings.cfm--->
<!----Get the full path to the file datasource.txt--->
<cfparam name="isname" default="datasource.txt">
<cfset thisPath = ExpandPath("*.*")>
<cfset thisDirectory = GetDirectoryFromPath(thisPath)>
<cfparam name="islocation" default="#thisDirectory##isname#">
<cfset isettingsfile = islocation>
<!----Does the file datasource.txt exist?--->
<cfif FileExists(isettingsfile)>
<!---The file datasource.txt has been found, read it and place the contents into a variable called dsn--->
<cffile action="read" file="#isettingsfile#" variable="dsn">
<!---Set a variable called datasource and fill it with the contents of datasource.text--->
<cfparam name="datasource" default="#dsn#">
<!---Application.cfm--->
<!---Call the file read_settings.cfm---->
<cfinclude template="includes/read_settings.cfm">
<!---DNS_Test.cfm--->
<!---Query a table with one record to see if the connection is established--->
<cfquery name="qry_DSNTest" datasource="#dsn#">
SELECT *
FROM tbl_Admin
</cfquery>
<!---Has the query returned any results? If yes, set the variable connection to connected, otherwise, set it to not_connected--->
<cfif qry_DSNTest.RecordCount>
<cfset tconnection="Connnected">
<cfelse>
<cfset tconnection="Not_connnected">
Now, when I attempt to run a simple test this is the error message I receive:
| Error Occurred While Processing Request | |||||||||||||||
| |||||||||||||||
If I remove the cfinclude template line in my Application.cfm file and replace it with
<cfparam name="datasource" default="FreshDb">
Then I can perform the query without any problem.
If I leave the cfinclude template in place, but do not attempt to perform a query using the datasource, I can do a variable dump and see these results:
| VARIABLES |
| ||||||||||||||||||||||
So it appears to me, that my files are being found, the contents are being read, and the variables are beiong populated. So I cant figure out why I cant call the files using cfinclude template. and have the variables available.
Any ideas would be greatly appreciated. I am going into my second full day trying to figure out what I am doing wrong.
P.S. What I am trying to acheive with this code is a method to allow the user to define a custom datasource name. This should be done without ever connecting to the database, as there will be no connection to the database until the datasource name is defined somewhere. Also I do not want the user to have to manually edit any files. The goal is to submit a form with the datasource name and then test the connection. If there is an easier way to go about this, than the way I am trying, please offer your suggestions.
