SQL保存/加载文件地址 [英] SQL save/load file address

查看:136
本文介绍了SQL保存/加载文件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...

我正在尝试使用C#将文件地址保存在SQL Server数据库中,但我不知道我需要在数据库中使用什么数据类型",或者我是否需要一些我不需要的代码不知道...
就像这样:我创建了一个数据库,并创建了一个带有列的表,其中定义了nvarchar(100)类型,并用地址c:\ projects \ vids \ simpsonslidetector.flv填充",但是当我尝试时要从该列获取结果,我只能检索simpsonslidetector,并且我想检索包括文件扩展名的整个路径...
也有一种写绝对地址的方法,例如:机器地址" \ vids \ simpsonslidetector.flv
所以我不能在任何计算机上使用它?
谢谢人们

Hi...

I''m trying to get a file address saved on the SQL server database using c#, but i don''t know what ''data type'' i need to use in the database or if i need some code that i don''t know...
it''s like this: i have created a database and made a table with a column where i define a type nvarchar(100) and "fill it" with the address c:\projects\vids\simpsonslidetector.flv but when i try to get the result from that column i can only retrieve simpsonslidetector, and i want to retrieve the whole path including the file extension...
also is there a way of writing the absolute address, something like: "the machine address"\vids\simpsonslidetector.flv
so i cant use it on any computer???

thanks people

推荐答案

首先,不要使用nvarchar(100)-它太短了! MS说了260个字符,但随后继续讨论32K( MSDN [ ^ ]).无论您决定使用什么,100个字符都太短了!

其次,您在数据库中存储了什么?如果您要回读的只是没有路径或扩展名的文件名,那么可以断定您的问题是您保存的内容,而不是您回读的内容!
使用VS(通过服务器资源管理器)或使用SQL Server Management Studio对其进行检查.

如果您所拥有的是正确的,则发布您的代码以读取它-否则我们只是在猜测!
Firstly, don''t use nvarchar(100) - it is way too short! MS says 260 chars, but then goes on to talk about 32K (MSDN[^]). Whatever you decide to use, 100 characters is far too short!

Second, what have you stored in the database? If what you are reading back is just the filename with no path or extension, then it is a good bet that your problem is what you have saved, rather than what you have read back!
Examine it using VS (via the Server Explorer) or using SQL Server Management Studio.

If what you have in there is correct, then post your code to read it - otherwise we are just guessing!


i我在数据库中存储的字符串为"c:\ projects \ vids \ simpsonslidetector .flv",当我尝试检索时,它会忽略除"simpsonslidetector"之外的所有内容...这用作网络服务,网络服务会接收一个int值,以便它可以与表中的某个ID匹配,并在找到该ID时返回字符串(在这种情况下不是整个字符串,在其他情况下为更简单"的字符串,它返回的很好):

数据库上的表名是``info''并作为3列:id(int),name(nvarchar(100)),address(nvarchar(100))...该地址应使用哪种数据类型?还是应该增加尺寸?

我也有其他方法可以连接到数据库以返回一个值或值列表,并且运行正常,唯一的问题是地址字符串(我认为未得到我想要的结果的原因是,由于某种原因,阅读器未收到存储在数据库中的格式化字符串("c:\ projects \ vids \ simpsonslidetector.flv")).

这是我用来检索字符串的代码:

i stored in the database the string "c:\projects\vids\simpsonslidetector.flv" and when i try to retrive, it ignores everything except "simpsonslidetector"... this is used as webservice, the webservice receives an int so it can match with a certain id in the table and when finds that id, returns the string (in this case not the whole string, in other cases with "simplier" strings it return well):

the table name on the database is ''info'' and as 3 colums: id (int), name(nvarchar(100)), address(nvarchar(100))...what type of data should i use for the address? or should i just increase the size?

also i have other methods that connect to the database to return a value or list of values, and goes fine, the only problem is the address string (i think the cause of not getting the result i want, is because, for some reason the reader does not receive the formatted string stored in the database ("c:\projects\vids\simpsonslidetector.flv")).

this is the code i use to retrieve the string:

[WebMethod]
    public String getAddressByID(Int32 id)
    {
        String strReader = "";
        SqlConnection conn = new SqlConnection(strCon);
        conn.Open();
        String readCmd = "SELECT address FROM info WHERE id = " + id.ToString();
        SqlCommand cmd = new SqlCommand(readCmd, conn);
        strReader = (String)cmd.ExecuteScalar();
        conn.Close();
        if (strReader == null)
        {
            return "No Result";
        }
        else
        {
            return strReader;
        }
    }



PS:我等待回复时,将阅读OriginalGriff发布的 [ MSDN ]文档(谢谢). .



P.S.: while i wait for a response i will read the [MSDN] document OriginalGriff posted (thanks)...


发现"了问题,与数据库或c#代码无关,但与浏览器或服务器(我的计算机)无关……发生了什么!?我更改了数据库中的值后,浏览器显示了最后一条记录,例如:
1a-我第一次在列中使用字符串文件名获取信息;

1b-启动了Web服务,调用了服务,结果是正确的;

2a-,然后我将字符串更改为filenameXPTO;

2b-启动了Web服务,调用了服务并邀请了客人,结果是filena而不是filenameXPTO;

3a-因此我关闭了服务器(ASP.NET开发服务器)和Bowser,并使用CCleaner程序清理了cookie和其他信息;

3b-然后在不更改数据库任何内容的情况下再次启动了Web服务,结果是filenameXPTO;

3c-然后我执行了相同的过程,但是这将字符串更改为c:\ projects \ vids \ simpsonslidetector.flv,返回结果正常...

因此,如果有人遇到相同的问题,请尝试清理浏览器的cookie并重新启动服务器,然后再次进行测试(我使用的是firefox v4.0 bowser)...

感谢您的回答,您是否还知道如何插入字符串作为项目本身的相对路径,而不是使用c:\​​ projects \ vids \ simpsonslidetector.flv,而是使用诸如此类的东西(在这里,文件夹或项目,即系统确定的)\ vids \ simpsonslidetector.flv,因此可以将其传送到任何平台...谢谢

欢呼,PutchPT
"found" the problem, it as nothing to do with the database or the c# code, but with the browser or with the server (my computer)... what happened!? the browser showed the last record after i changed the value in the database, for example:
1a- in the first time i had the information in the column with the string filename;

1b- launched the webservice, invoked the service and the result was correct;

2a- and then i changed the string to filenameXPTO;

2b- launched the webservice, invoked the service and guest what, the result was filena instead of filenameXPTO;

3a- so i closed the server (ASP.NET Development Server) and the bowser, and cleaned the cookies and other information using CCleaner program;

3b- then launched the webservice again without changing anything in the database, and the result was filenameXPTO;

3c- then i did the same procedure but this changed the string to c:\projects\vids\simpsonslidetector.flv , and the return result is OK...

so, if anyone as the same problem try to clean the browsers cookies and restart the server, and test again (i was using firefox v4.0 bowser)...

thanks for your answer, and also do you know how to insert a string that as a relative path to the project itself, instead of using c:\projects\vids\simpsonslidetector.flv, using somthing like (IN HERE GOES THE FOLDER O THE PROJECT, THAT THE SYSTEM DETERMINES)\vids\simpsonslidetector.flv, so it can be teleported to any plataform...THANKS

cheers, PutchPT


这篇关于SQL保存/加载文件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆