如何从数据库获取文件名.

如何从数据库获取文件名.

问题描述:

Helo Friedns,
今天有一个新问题...
我的应用程序运行正常.允许用户下载.txt或.xml格式的文件.

我正在用硬编码的应用程序中定义文件名格式.

但是我想从我的数据库中获取文件名.

如何从我的数据库中分配文件名.

例如

ABD_YYYY-MM-DD_TIME.txt
ABD_YYYY-MM-DD_TIME.xml

我想从数据库中获取名称ABD.

Helo friedns,
today with one new problem...
my application is working fine.it is permit the user to download its file in .txt or .xml format.

i am defining the file name format in my application hard coded.

but i want to papulate the file name from my DB.

how can assign the file name from my DB.

for example

ABD_YYYY-MM-DD_TIME.txt
ABD_YYYY-MM-DD_TIME.xml

i want to get name ABD from my DB.

您存储的信息不正确IMO,连接的文件名实际上是三个离散的信息,这会更好从这些中形成您在select语句中所拥有的.我建议:
You are storing the information incorrectly IMO, the concatenated filename is really three discrete peices of information, it would be better to form what you have in the select statement from these. I suggest:

  1. 创建一个新表FooAllowedFileNameExtension
  2. 在表中添加一列Extension,使其成为varchar类型,并根据您的要求限制其长度,可能是1 +
  3. 将该列设为在2个主键中

  1. Create a new table FooAllowedFileNameExtension
  2. In the table add a column Extension, make it varchar type and restict its length as per your requirements probably 1+
  3. Make the column in 2 its primary key



使用当前表将文件名拆分为三列:
OrininalFilename,Datestamp和扩展名(FK返回上表).

这似乎很费劲,但是:
a)您不会失去原名.
b)您可以更好地查询:一天中上传的所有文件等
c)您可以使用允许的扩展名表来确定是否可以添加文件.
d)如果要添加新文件类型,它将变得更加容易:只需将其添加到新表中即可.
e)可接受的文件名位于堆栈的下方,因此,如果连接了新系统,则开发人员不太可能允许上传不受支持的文件类型.

显然,您首先需要迁移现有数据,它并不能真正解决您的原始问题(这两个问题都需要拆分字符串的能力),并且您可能无法更改架构. br/> 您可以相对轻松地获得ABD部分,并轻松扩展文件扩展名:



With your current table split the filename you have into three columns:
OrininalFilename, Datestamp and extension (which FKs back to the table above).

This seems like a lot of effort, but:
a) You don''t lose the original name.
b) You can query better: All files uploaded on a day etc
c) You can used the allowed extensions table to determine whether a file can be added.
d) If you want to add new file types it becomes much easier: just add it to the new table.
e) The acceptable filnames are lower down the stack, so if a new system connects to it, it is less likely that the developer will allow the uploading of an unsupported file type.

Obviously you''ll need to migrate the existing data in the first place, and it doesn''t really answer your original problem (which both need the ability to split the string) and you may not be able to alter the schema.
You can get the ABD part relatively and file extensions pretty easily:

declare @foo varchar(max)
set @foo = 'ABD_YYYY-MM-DD_TIME.txt'
Select substring( @foo, 0, CHARINDEX ('_',@foo, 0))
Select substring( @foo, CHARINDEX ('.',@foo, 0)+ 1,LEN(@foo) -CHARINDEX ('.',@foo, 0) )






这看起来像是一个有用的讨论 [






This looks like a useful discussion[^] it discusses various custom split functions.


它"就像这个例子一样简单:
It''s simple as this example:
SELECT DB_NAME() + '_' + CONVERT(VARCHAR(10),GETDATE(),120) + '.txt' AS [FileName]



有关日期和时间转换的更多信息: CAST和CONVERT [



More about date and time convertion: CAST and CONVERT[^]