I am using coldfusion.
I have dedicated database server and shared webserver. Both are located on different server.
I do not have access of coldfusion administrator where I can create datasource.
I need help to create database connection in the code and needs to createDatasource Name.
I know this does not exactly answer your question, but may help a bit. Here I goes:
You can create a DSN less connection like example below. You can store the connection in Application.cfm/cfc:
<cfscript>
classLoader = createObject("java", "java.lang.Class");
classLoader.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dm = createObject("java","java.sql.DriverManager");
con = dm.getConnection("jdbc:odbc:DRIVER={MySQL ODBC 3.51 Driver}; SERVER=server; PORT=3306; DATABASE=database; USER=username; PASSWORD=password; OPTION=3;");
st = con.createStatement();
rs = st.ExecuteQuery("Select xyz FROM table");
q = createObject("java", "coldfusion.sql.QueryTable").init(rs);
//the query is stored in the variable q
</cfscript>
ps: reference http://www.hostmysite.com/support/mysql/coldfusionstring/
Related
I have a project of C++ windows form. Now, I want to store the data in my SQL server database.
So, how can I connect my database from inside my C++ code and how to write an insert query?
I used the following code but it throws an exception that cannot open or cannot found the database.
SqlConnection^ con = gcnew SqlConnection();
int i = 0;
con->ConnectionString = "Data Source=122.179.151.229\EIEXPRESS;Initial Catalog=ICAST_IMS;Uid=developer;pwd=dev#12345";
con->Open();
SqlCommand^ com = gcnew SqlCommand();
com->Connection = con;
com->CommandText = "INSERT INTO image_analysis (nodule_count, nodularity) VALUES (final_nodule_count, 'final_nodularity')";
com->ExecuteNonQuery();
I think your problem it's your connection string, you include an "\" which is a special character, you need to try doing "\\" so the system recognize you're inserting an "\".
Hope this helps you!
I have an .aspx page that calls the .asmx web method to extract data from the MS SQL DB. I have tested the result without the connectionstring and it works.
I think my problem is with my connection string within the web.config, but i am not sure how to set it up. When i try to run the search with the SQL command i either get 500 server error or just nothing. Can someone please provide some advise?
partial web.config
<connectionStrings>
<add name="myConn" connectionString="Data Source=D:\\root\\Database\\; Initial Catalog=myData.mdb;Integrated Security=sspi;" />
</connectionStrings>
Web Method
Dim customers As List(Of String) = New List(Of String)
Dim conn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("myConn").ConnectionString
Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand
cmd.CommandText = "SELECT col1, col2 from myTable where" &
" , like #SearchText + '%'"
cmd.Parameters.AddWithValue("#SearchText", prefixText)
cmd.Connection = conn
conn.Open()
Dim sdr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader
While sdr.Read
Dim item As String = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(sdr("col1").ToString, sdr("col2").ToString)
customers.Add(item)
End While
conn.Close()
Return customers
OK, since i was trying to connection to an Access database, I was suppose to use OleDB connection instead of SqlClient.
So I replaced all Sqlclient with OldDb and it promptly solved the problem. So it turns out, it is very important to know which database type you are connecting to before you try to make that connection.
I use the following to dynamically detect the host server. The importance for making it dynamic is that currently there are too many hard coded redirect such as:
http:s//mysite.com/hr/index.cfm
within my app.
When I'm moving from production site to development site and then back to production site, I have to manually change/comment out this http/https one by one and it is not only time consuming but also dangerous.
Here is the code I found that can detect the host server. Then I do the following:
<CFSET inet = CreateObject("java", "java.net.InetAddress")>
<CFSET inet = inet.getLocalHost()>
<CFSET HostServer = "#inet.getHostName()#">
<CFSET ThisHostServer = "#LEFT(HostServer,6)#">
<CFSWITCH expression="#Trim(ThisHostServer)#"><!--- Prod or Dev server --->
<CFCASE value="myprodsite.com">
<CFSET UseThisURL = "http://myprodsite.com">
</CFCASE>
<CFCASE value="mydevsite.com">
<CFSET UseThisURL = "http://myDevsite.com">
</CFCASE>
</CFSWITCH>
Then on each page where links or redirection exist, I just need to use:
#UseThisURL#/hr/index.cfm
My question is:
Where is the best way to set #UseThisURL# in the application?
I'm using ColdFusion 10 and Application.cfc in Linux env.
Should I set it as an application or a session scope?
Since everything will be in an application or session scope, when users are idle on a certain page and the application/session scope is expired, when user click on a link will it generate an error? How to prevent users from seeing error caused by using this technique? Please advice, thank you!
Best practice that I used is creating config.cfc which can contain function like getServerSpecificVariables() to return structure. this structure will be saved in your application scope since you don't want to create USEThisURL for every session start. When you need to reset simply clear your application scope. instantiate below config component inside onApplicationStart event in Application.cfc
Example
Config.cfc:
component{
public struct function getServerSpeceficVariables(){
var config = {};
var inet = CreateObject("java", "java.net.InetAddress");
inet = inet.getLocalHost();
HostServer = inet.getHostName();
ThisHostServer = LEFT(HostServer,6);
switch(Trim(ThisHostServer)){
case 'myprodsite.com':{
config.useThisURL = '';
break;
}
case 'mydevsite.com':{
config.useThisURL = '';
break;
}
}
return config;
}
}
I have a ColdFusion instance being run under enterprise, but for some reason it ignores the local data source. It will only connect if I put the data source at the enterprise level.
I've even tried the following code and it only returns the data sources that are declared at the instance manager, not the instance itself.
<cfset factory = createObject("java", "coldfusion.server.ServiceFactory")>
<cfset datasources = factory.getDataSourceService().getDatasources()>
<cfloop collection="#datasources#" item="dsnName">
#dsnName#<br>
</cfloop>
Any help would be greatly appreciated.
These should help you figure out which instance you are on:
<cfscript>
loc = {};
loc.machineName = createObject('java','java.net.InetAddress').localhost.getCanonicalHostName();
loc.machineName2 = createObject('java','java.net.InetAddress').localhost.getHostName();
loc.hostAddress = createObject('java','java.net.InetAddress').localhost.getHostAddress();
loc.instanceName = createObject('java','jrunx.kernel.JRun').getServerName();
writeDump( var: loc );
</cfscript>
If you are having problems getting the datasources you might need to authenticate first with your cf administrator password like so:
createObject('component','CFIDE.adminapi.administrator').login('your-password');
There is a datasourceExists(), verifyDatasource() and getDatasource() method on the data source service that you might find handy:
<cfscript>
loc = {};
loc.dss = createObject('java','coldfusion.server.ServiceFactory').getDataSourceService();
loc.datasources = loc.dss.getDatasources();
loc.exists = loc.dss.datasourceExists('your-dsn');
loc.verified = loc.dss.verifyDatasource('your-dsn');
loc.datasource = loc.dss.getDatasource('your-dsn');
writeDump( var: loc );
</cfscript>
I have an application which uses context sensitive datasources. Currently I keep the datasource information stored a such
reqeust.DB.Datasource = "DatasourceName";
request.DB.Username = "DatasourceUsername"
request.DB.Password = "DatasourcePassword"
I then overwrite the variables depending on the context, so each cfquery tag has the attributes datasource="#request.DB.Datesource#" ... etc ...
I want to start moving to more CFC centric frameworks like Coldbox, but I just don't see how this would work.
Do I need to pass in a datasource object into the init statement of the CFC? This seems like it would be a super PITA.
With CF9, you can this.datasource in Application.cfc as the default datasource. Unfortunately, it doesn't seem to have a way to set username/password
Either
A.) use an Dependency Injection framework such as ColdSpring (only suitable for singleton Services), Lightwire or Coldbox's own DI solution (Wirebox). and inject the datasource/username/password through the init constructor or setters.
B.) set <Datasources> in Coldbox.xml.cfm, see: http://wiki.coldbox.org/wiki/ConfigurationFile.cfm
<!--Datasource Setup, you can then retreive a datasourceBean
via the getDatasource("name") method: -->
<Datasources>
<Datasource alias="MyDSNAlias"
name="real_dsn_name"
dbtype="mysql"
username=""
password="" />
</Datasources>
Even if your objects only get initialized at request level, it seems like it should be less of a pain to work with in this fashion.
<cfscript>
request.DB.Datasource = "DatasourceName";
request.DB.Username = "DatasourceUsername";
request.DB.Password = "DatasourcePassword";
request.randomDAO = createObject('component','DAOStuff.randomDAO');
request.randomDAO.init(DBObject = request.DB);
request.someQuery = request.randomDAO.someGetter();
request.someOtherQuery = request.randomDAO.someOtherGetter();
request.aThirdQuery = request.randomDAO.aThirdGetter();
</cfscript>
As opposed to:
<cfscript>
request.DB.Datasource = "DatasourceName";
request.DB.Username = "DatasourceUsername";
request.DB.Password = "DatasourcePassword";
</cfscript>
<cfquery name="request.someQuery"
datasource=request.DB.Datasource
username=request.DB.Username
password=request.DB.Password>
--SOME SQL HERE
</cfquery>
<cfquery name="request.someOtherQuery"
datasource=request.DB.Datasource
username=request.DB.Username
password=request.DB.Password>
--SOME SQL HERE
</cfquery>
<cfquery name="request.aThirdQuery"
datasource=request.DB.Datasource
username=request.DB.Username
password=request.DB.Password>
--SOME SQL HERE
</cfquery>
If it is safe for your data objects to exist at an application level (assuming here that the data source for the object will not change at run-time and that you have written thread-safe CFCs) You can store and initialize DAOs at application level and then each request has wonderfully simple code like:
<cfscript>
request.someQuery = application.randomDAO.someGetter();
request.someOtherQuery = application.randomDAO.someOtherGetter();
request.aThirdQuery = application.randomDAO.aThirdGetter();
</cfscript>