Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Converting double slashes

Converting double slashes

Scheduled Pinned Locked Moved C#
databasequestion
21 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mark06

    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?

    M Offline
    M Offline
    Mark06
    wrote on last edited by
    #7

    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.

    M G 2 Replies Last reply
    0
    • M Martin 0

      Replace[^]

      filename.Replace("\\","\");

      -- modified at 9:06 Friday 27th July, 2007 trash post, sorry!

      All the best, Martin

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #8

      That will not do anything at all, as the string doesn't contain any double backslashes.

      --- single minded; short sighted; long gone;

      1 Reply Last reply
      0
      • M Martin 0

        Replace[^]

        filename.Replace("\\","\");

        -- modified at 9:06 Friday 27th July, 2007 trash post, sorry!

        All the best, Martin

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #9

        Martin# wrote:

        filename.Replace("\\","\");

        That won't even compile. What you intended to write was: filename.Replace("\\\\","\\"); or filename.Replace(@"\\",@"\"); However, that will not do anything at all, as the string doesn't contain any double backslashes.

        --- single minded; short sighted; long gone;

        M 1 Reply Last reply
        0
        • G Guffa

          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;

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #10

          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 the master database at a later date.

          Stability. What an interesting concept. -- Chris Maunder

          1 Reply Last reply
          0
          • M Mark06

            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.

            M Offline
            M Offline
            Mike Dimmick
            wrote on last edited by
            #11

            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

            M 1 Reply Last reply
            0
            • M Martin 0

              Replace[^]

              filename.Replace("\\","\");

              -- modified at 9:06 Friday 27th July, 2007 trash post, sorry!

              All the best, Martin

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #12

              LOL, that wont compile :)

              1 Reply Last reply
              0
              • G Guffa

                Martin# wrote:

                filename.Replace("\\","\");

                That won't even compile. What you intended to write was: filename.Replace("\\\\","\\"); or filename.Replace(@"\\",@"\"); However, that will not do anything at all, as the string doesn't contain any double backslashes.

                --- single minded; short sighted; long gone;

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #13

                :doh: Sure!

                All the best, Martin

                1 Reply Last reply
                0
                • M Mike Dimmick

                  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

                  M Offline
                  M Offline
                  Mark06
                  wrote on last edited by
                  #14

                  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.

                  G M 2 Replies Last reply
                  0
                  • M Mark06

                    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.

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #15

                    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;

                    1 Reply Last reply
                    0
                    • M Mark06

                      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.

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #16

                      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;

                      M 1 Reply Last reply
                      0
                      • M Mark06

                        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.

                        M Offline
                        M Offline
                        Mike Dimmick
                        wrote on last edited by
                        #17

                        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

                        L 1 Reply Last reply
                        0
                        • G Guffa

                          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;

                          M Offline
                          M Offline
                          Mike Dimmick
                          wrote on last edited by
                          #18

                          Damn debugger watch window (and tooltips) shows it as doubled.

                          Stability. What an interesting concept. -- Chris Maunder

                          M 1 Reply Last reply
                          0
                          • M Mike Dimmick

                            Damn debugger watch window (and tooltips) shows it as doubled.

                            Stability. What an interesting concept. -- Chris Maunder

                            M Offline
                            M Offline
                            Mark06
                            wrote on last edited by
                            #19

                            Yup, that'll be it!! :mad:

                            1 Reply Last reply
                            0
                            • M Mark06

                              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?

                              N Offline
                              N Offline
                              Nouman Bhatti
                              wrote on last edited by
                              #20

                              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("\\","\")

                              1 Reply Last reply
                              0
                              • M Mike Dimmick

                                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

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #21

                                Never, never apply Console.WriteLine on a suspicious variable. You might find the cause of a problem...

                                Luc Pattyn


                                try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups