Converting double slashes
-
Hi, Ive got a filename that i'm passing to an stored proc, but the string is a filename:
string filename = "c:\\temp\\LogFile.Txt";
my command is built:command.CommandText = "sp_UpdateLogFile '" + filename + "'";
however, sql doesnt accept the \\ directory seperators, they should only be one \. Is there a function already available to remove these double slashes? -
Hi, Ive got a filename that i'm passing to an stored proc, but the string is a filename:
string filename = "c:\\temp\\LogFile.Txt";
my command is built:command.CommandText = "sp_UpdateLogFile '" + filename + "'";
however, sql doesnt accept the \\ directory seperators, they should only be one \. Is there a function already available to remove these double slashes? -
Hi, Ive got a filename that i'm passing to an stored proc, but the string is a filename:
string filename = "c:\\temp\\LogFile.Txt";
my command is built:command.CommandText = "sp_UpdateLogFile '" + filename + "'";
however, sql doesnt accept the \\ directory seperators, they should only be one \. Is there a function already available to remove these double slashes? -
Hi, Ive got a filename that i'm passing to an stored proc, but the string is a filename:
string filename = "c:\\temp\\LogFile.Txt";
my command is built:command.CommandText = "sp_UpdateLogFile '" + filename + "'";
however, sql doesnt accept the \\ directory seperators, they should only be one \. Is there a function already available to remove these double slashes? -
Hi, Ive got a filename that i'm passing to an stored proc, but the string is a filename:
string filename = "c:\\temp\\LogFile.Txt";
my command is built:command.CommandText = "sp_UpdateLogFile '" + filename + "'";
however, sql doesnt accept the \\ directory seperators, they should only be one \. Is there a function already available to remove these double slashes?Mark06 wrote:
sp_UpdateLogFile
Don't put "sp_" in the name of your stored procedure. "sp_" stands for "system procedure", and they are handled differently from normal stored procedures.
Mark06 wrote:
sql doesnt accept the \\ directory seperators
First of all, your string doesn't contain any double backslashes. The backslash is the escape character in string literals in C#. When you put double backslashes in a string literal, the string will contain a single backslash. Second, the database has no problems with backslashes in string literals. In MS SQL the apostrophe is the escape character, the backslash has no special meaning at all. What has made you come to the conclusion that the database would have any problem with double backslashes, especially as your string doesn't even contain any?
--- single minded; short sighted; long gone;
-
Hi, Ive got a filename that i'm passing to an stored proc, but the string is a filename:
string filename = "c:\\temp\\LogFile.Txt";
my command is built:command.CommandText = "sp_UpdateLogFile '" + filename + "'";
however, sql doesnt accept the \\ directory seperators, they should only be one \. Is there a function already available to remove these double slashes?The backslash is an escape character within a string literal in C-based languages (C, C++, C#, Java), used to include non-printable characters within a string (for example, the newline character U+000A[^] is represented as
'\n'
). For that reason, to get an actual backslash in the string, you have to double it. This is processed by the compiler - in the compiled file, only a single backslash appears. Therefore you don't need to do anything to remove them at runtime - the compiler has already done it. C# also supports @-quoted strings where the backslash is a literal backslash, not an escape character. The only escape available for this type of literal is "", which indicates that a single " character should be included in the string.Stability. What an interesting concept. -- Chris Maunder
-
-
Hi, Ive got a filename that i'm passing to an stored proc, but the string is a filename:
string filename = "c:\\temp\\LogFile.Txt";
my command is built:command.CommandText = "sp_UpdateLogFile '" + filename + "'";
however, sql doesnt accept the \\ directory seperators, they should only be one \. Is there a function already available to remove these double slashes? -
Mark06 wrote:
sp_UpdateLogFile
Don't put "sp_" in the name of your stored procedure. "sp_" stands for "system procedure", and they are handled differently from normal stored procedures.
Mark06 wrote:
sql doesnt accept the \\ directory seperators
First of all, your string doesn't contain any double backslashes. The backslash is the escape character in string literals in C#. When you put double backslashes in a string literal, the string will contain a single backslash. Second, the database has no problems with backslashes in string literals. In MS SQL the apostrophe is the escape character, the backslash has no special meaning at all. What has made you come to the conclusion that the database would have any problem with double backslashes, especially as your string doesn't even contain any?
--- single minded; short sighted; long gone;
Guffa wrote:
Don't put "sp_" in the name of your stored procedure. "sp_" stands for "system procedure", and they are handled differently from normal stored procedures.
Specifically, SQL Server looks in the
master
database first, before looking in the database you're actually trying to use. This extra lookup doesn't cost a lot of time, but could break your application if Microsoft, or someone else, add a procedure with that name to themaster
database at a later date.Stability. What an interesting concept. -- Chris Maunder
-
Martin# wrote:
filename.Replace("\\","\");
That won't even compile. What you intended to write was:
filename.Replace("\\\\","\\");
orfilename.Replace(@"\\",@"\");
However, that will not do anything at all, as the string doesn't contain any double backslashes.--- single minded; short sighted; long gone;
-
appologies, I was trying to keep it simple. I'm actually calling the stored proc 'sp_attach_db' and I've tried command.CommandText.Replace("\\",@"\"); but it didnt do anything to the commandtext value.
That's not surprising, both literals end up as a single backslash. I suggest you post the actual problem you're encountering on the SQL/ADO/ADO.NET forum.
Stability. What an interesting concept. -- Chris Maunder
-
-
Martin# wrote:
filename.Replace("\\","\");
That won't even compile. What you intended to write was:
filename.Replace("\\\\","\\");
orfilename.Replace(@"\\",@"\");
However, that will not do anything at all, as the string doesn't contain any double backslashes.--- single minded; short sighted; long gone;
-
That's not surprising, both literals end up as a single backslash. I suggest you post the actual problem you're encountering on the SQL/ADO/ADO.NET forum.
Stability. What an interesting concept. -- Chris Maunder
-
appologies, I was trying to keep it simple. I'm actually calling the stored proc 'sp_attach_db' and I've tried command.CommandText.Replace("\\",@"\"); but it didnt do anything to the commandtext value.
Mark06 wrote:
I've tried command.CommandText.Replace("\\",@"\"); but it didnt do anything to the commandtext value.
Of course not. You are replacing each single backslash with a single backslash. Also, the Replace method doesn't change the string, it returns the new string, so you have to use the result of the method:
str = str.Replace("\\\\", "\\");
Still, the string literal that you showed doesn't contain any double backslashes, so that will not have any effect at all. What has made you come to the conclusion that the database has any problems with double backslashes, and why do you think that your string contains any?--- single minded; short sighted; long gone;
-
but this isnt an sql problem. the problem is, c# setting a commandtext to "sp_attach_db N'c:\\temp\\logfile.txt'" when it should be "sp_attach_db N'c:\temp\logfile.txt'" the actual sql command is irrelevant. its the double slashes thats the issue.
-
but this isnt an sql problem. the problem is, c# setting a commandtext to "sp_attach_db N'c:\\temp\\logfile.txt'" when it should be "sp_attach_db N'c:\temp\logfile.txt'" the actual sql command is irrelevant. its the double slashes thats the issue.
Visual Studio shows escapes in the C# debugger watch window and tooltips. I think that's a stupid idea, but there you are: that's what it does. If you output the string to the console (Console.WriteLine) you will see that the backslashes are not doubled. If you're having trouble attaching the database, please ask on the other forum (including all error messages) - this is not the problem.
Stability. What an interesting concept. -- Chris Maunder
-
Mark06 wrote:
the problem is, c# setting a commandtext to "sp_attach_db N'c:\\temp\\logfile.txt'" when it should be "sp_attach_db N'c:\temp\logfile.txt'"
It doesn't. Why do you think that it would?
--- single minded; short sighted; long gone;
Damn debugger watch window (and tooltips) shows it as doubled.
Stability. What an interesting concept. -- Chris Maunder
-
Damn debugger watch window (and tooltips) shows it as doubled.
Stability. What an interesting concept. -- Chris Maunder
-
Hi, Ive got a filename that i'm passing to an stored proc, but the string is a filename:
string filename = "c:\\temp\\LogFile.Txt";
my command is built:command.CommandText = "sp_UpdateLogFile '" + filename + "'";
however, sql doesnt accept the \\ directory seperators, they should only be one \. Is there a function already available to remove these double slashes?ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_String_Replace_1_d460c748.htm use filename.replace("\\","\")