当MS SQL SERVER允许远程连接时,如何上传文件并将其存储在服务器本地路径中?
我正在使用 Delphi 和 MS SQL Server 开发 win32 Windows 应用程序。它在 LAN 中工作正常,但我正在尝试添加对 SQL Server 远程连接的支持(= 使用可以通过外部 IP 访问的数据库,如本文所述:http://support.microsoft.com/default.aspx?scid=kb;EN-US;914277)。
基本上,我在数据库中有一个表,其中保存了 DocumentID、文档描述和文档路径(如 \\FILESERVER\MyApplicationDocuments\45.zip
)。
当然 \\FILESERVER
是服务器的本地(LAN)路径,但不是客户端的路径(因为我现在正在尝试添加对远程连接的支持)。
所以我需要一种方法来访问 \\FILESERVER
即使我在局域网中看不到它。
我发现以下 T-SQL 代码片段非常适合“下载技巧”:
SELECT BulkColumn as MyFile FROM OPENROWSET(BULK '\FILESERVER\MyApplicationDocuments\45.zip' , SINGLE_BLOB) AS X
通过上面的代码,我可以下载客户端上的文件。
但如何上传呢?我需要一个“上传技巧”才能插入新文件,而且还可以删除或替换现有文件。
有人可以建议吗?如果没有可用的技巧,你能建议一个替代方案吗?就像扩展存储过程或从服务器调用某些 .net 程序集一样。
I am developing a win32 windows application with Delphi and MS SQL Server. it works fine in LAN but I am trying to add the support for SQL Server remote connections (= working with a DB that can be accessed with an external IP, as described in this article: http://support.microsoft.com/default.aspx?scid=kb;EN-US;914277).
Basically I have a Table in DB where I keep the DocumentID, the document description and the Document path (like \\FILESERVER\MyApplicationDocuments\45.zip
).
Of course \\FILESERVER
is a local (LAN) path for the server but not for the client (as I am now trying to add the support for remote connections).
So I need a way to access \\FILESERVER
even if of course I cannot see it in LAN.
I found the following T-SQL code snippet that is perfect for the "download trick":
SELECT BulkColumn as MyFile FROM OPENROWSET(BULK '\FILESERVER\MyApplicationDocuments\45.zip' , SINGLE_BLOB) AS X
With the code above I can download a file on the client.
But how to upload it? I need an "Uppload trick" to be able to insert new files, but also to delete or replace existing files.
Can anyone suggest? If a trick is not available could you suggest an alternative? Like an extended stored procedure or calling some .net assembly from the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有 sql 2008,那么您可以使用 FILESTREAM,然后 sql服务器会自动将其扔到磁盘上。
如果您有 sql 2005,我会考虑将数据移动到 varbinary( max) 列并以这种方式处理它(也很简单)。
如果这些都不适用或者您无法将其推入 varbinary 列,那么我不会使用 sql server 来处理实际的文件内容,而只是使用一个将文件存储在文件系统或 SAN 上的 Web 服务Web服务可以轻松访问。 (与恕我直言相同)
更新:
我的脑海中闪过另一个想法。如果您使用的是 SQL 2005/08,那么您可以编写 CLR 存储过程在.Net中。这可以处理将 blob 数据传输到本地文件系统或从本地文件系统传输 blob 数据。
If you have sql 2008, then you can use FILESTREAM, then sql server will automatically throw it out to disk.
If you have sql 2005, I'd consider just moving the data into a varbinary(max) column and deal with it that way (also pretty simple).
If neither of those apply OR you can't shove it into a varbinary column, then I would not use sql server to handle the actual file contents and instead just have a web service which stored the file on the file system or a SAN that the web service can easily access. (same as IMHO)
UPDATE:
One other idea that crossed my mind. If you are using SQL 2005/08 then you can write a CLR Stored procedure in .Net. This could handle transferring the blob data to / from the local file system.
在理想的世界中,我会创建简单的:
- ASP.NET 网络服务
- 或 .Net Remoting Service(比 Web 服务更快)
- 或新的.Net 4.0 RIA 服务。
将其部署到 SQL Server 上的自定义 TCP/IP 端口
该服务将侦听该端口,客户端将通过该服务请求文件。该服务将通过本地 LAN 获取文件并通过本地 OLE DB 连接与 DB 进行通信。
我不会使用任何 SQl Server“Web 服务”支持 - 这是安全和性能问题。
更新:
由于这是 Delphi 应用程序 - 您可以使用 Delphi 执行相同的操作,即使上述解决方案仍然有效,但需要更多工作来集成不同的技术。 Delphi 有自己的工具来构建远程应用程序
In ideal world I would create simple:
- ASP.NET Web Service
- or .Net Remoting Service (faster than web service)
- or new .Net 4.0 RIA service.
Deploy it to the SQL Server on custom TCP/IP port
This service would listen to the port and client would request the file via the service. The service would get the file via local LAN and communicate with the DB via local OLE DB connection.
I would not use any SQl Server "web service" support - this is security and performance issues.
UPDATE:
Since this is Delphi app - you can do the same using Delphi, even though above solution still valid, but more work to integrate different technologies. Delphi has its own tools to build remote applications
如果您使用的是 2005,您可以尝试将文件存储在某个临时表的临时 blob 字段中,然后调用存储过程,该存储过程应该将文件放在您想要的位置,并根据需要更新路径字段。
在该存储过程中,您必须使用扩展存储过程 (xp_something),它允许访问文件系统。这意味着应该为 sql server 启用这些。
顺便说一句,您正在尝试使用关系数据库作为文档数据库。这迟早会适得其反。
If you are on 2005, you could try to store file in temp blob field of some temp table, and then call stored procedure which should put the file where you want it, and update path field as you want it.
In that stored procedure you must use extended stored procedures (xp_something), which allow access to file system. That means that those should be enabled for sql server.
BTW You are trying to use relational DB as Document database. That will, sooner or later, backfire.