简体   繁体   English

没有DSN的情况下如何连接到ColdFusion 7中的SQL Server?

[英]How can I connect to a SQL Server in ColdFusion 7 without a DSN?

I'm writing a CFC on a server running Coldfusion MX 7. The CFC needs to be able to query a database the location of which will be determined at runtime. 我正在运行Coldfusion MX 7的服务器上编写CFC。CFC需要能够查询数据库的位置,该数据库的位置将在运行时确定。 The database is SQL Server 2005. The CFC takes the IP address of the server and the name of the database as arguments, and the built-in Windows Authentication should allow connections from the machine. 数据库为SQL Server2005。CFC将服务器的IP地址和数据库名称作为参数,并且内置的Windows身份验证应允许来自计算机的连接。 However, I can't figure out how to code the connection. 但是,我不知道如何编码连接。 The <cfquery> tag hasn't supported the connectstring attribute since CF5. 自CF5以来,<cfquery>标记不支持connectstring属性。 Setting up a System DSN isn't dynamic enough. 设置系统DSN不够动态。 I know there's probably some way to connect to SQL Server without using a DSN, but the method escapes me at the moment. 我知道可能有某种方法可以在不使用DSN的情况下连接到SQL Server,但是此方法此刻使我无所适从。 Does anyone know how to do this? 有谁知道如何做到这一点?

You can modify the datasource on CF7 using the datasourceService. 您可以使用datasourceService在CF7上修改数据源。 Something along these lines might work: 遵循以下思路可能会起作用:

<cfset service = createobject("java","coldfusion.server.ServiceFactory").getDatasourceService()>
<cfset uname = "yourUserName">
<cfset pw = service.encryptPassword("yourPassword")>
<cfdump var="#createobject("java","coldfusion.server.ServiceFactory").getDatasourceService()#">
<cfset ds = service.getDatasources()>
<cfset dsn = "MyDSN">
<cfset thisDS = service.getDatasource(dsn)>
<cfset thisDS.getDatasourceDef().setPassword(pw)><!--- if you need to set the password programmatically --->
<!--- do stuff to set the URL... you can get the appropriate functions from the cfdump of the datasourceservice, above --->
Verifying datasource: #service.verifyDatasource(dsn)# <br>

Google search proposes this solution: http://cfsilence.com/blog/client/index.cfm/2007/11/8/More-cfquery--Now-With-cfqueryparam Google搜索提出了以下解决方案: http : //cfsilence.com/blog/client/index.cfm/2007/11/8/More-cfquery--Now-With-cfqueryparam

Have you tried it? 你试过了吗?

Don't have CF7 so can't say that all Java stuff from that tag works. 没有CF7,所以不能说该标签中的所有Java东西都可以使用。 At least it does not use ServiceFactory (aka solution for CF8). 至少它不使用ServiceFactory(也就是CF8的解决方案)。

After looking at everyone's answers and doing some more Google searches, I got a simple solution worked out. 在查看了每个人的答案并进行了更多的Google搜索之后,我找到了一个简单的解决方案。 This solution uses some Java code, as Henry suggested, but doesn't require compiling a jar. 正如亨利建议的那样,该解决方案使用了一些Java代码,但不需要编译jar。 It can be done inline using <cfscript>. 可以使用<cfscript>内联完成。

<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={SQL Server};Database=""DATABASENAME"";Server=SERVERNAME;","USERNAME","PASSWORD");
    st = con.createStatement();
    rs = st.ExecuteQuery("SELECT * FROM TABLE");
    q = createObject("java", "coldfusion.sql.QueryTable").init(rs);
</cfscript>

I think this is easier than programatically adding datasources to ColdFusion Administrator, although that seems like a good solution too. 我认为这比以编程方式向ColdFusion Administrator中添加数据源要容易,尽管这似乎也是一个不错的解决方案。 I'm guessing the custom tag Sergii linked works similarly, although I didn't see the link until after I tried this solution, so I haven't examined the code in detail. 我猜自定义标签Sergii链接的工作方式相似,尽管直到尝试该解决方案后才看到链接,所以我没有详细检查代码。

不在CF7中,但是您可以通过管理员API在CF8中添加,修改和删除ColdFusion数据源

使用Web服务访问数据库。

您可以绕过cfquery,而仅在Java中使用JDBC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM