0

I have the following code, which generates insert queries

For Each f As String In Directory.GetFiles(d)
    objSQLStringBuilder.Append("insert into table1 (full_path, file_name) values ('" & f.Replace("'", "''") & "', '" & f.Remove(0, Len(d) + 1).Replace("'", "''") & "');")
Next

However, the paths which it finds are formatted as follows

c:\program files\microsoft office\winword.exe

I need to format the paths as follows

file:///c:/program%20files/microosoft%20office/winword.exe

How can I modify the above code to do this?

4
  • 3
    Its never a good idea to "generate insert queries" especially using user input Commented Jan 7, 2011 at 16:27
  • I know, but it's an intranet environment, so no real risk of anyone trying to hack into it. Commented Jan 7, 2011 at 16:28
  • 2
    Even in your intranet: just DON'T do it ! Use parametrized queries - ALWAYS! - no excuse accepted. Commented Jan 7, 2011 at 16:28
  • 1
    It's not just hacking protection - it's performance (the engine can cache the execution plan), robustness (the app won't break if you don't escape something perfectly). There's a reason everyone is suggesting it - if you've followed that pattern before it's almost unthinkable to use the string concatenation approach! Commented Jan 7, 2011 at 16:44

5 Answers 5

4

As m.edmondson pointed out, you're much better off using command parameters.

Here is the basic idea:

sql = "INSERT INTO TABLE1 (full_path, file_Name) values (@full_path, @file_name)";

param = new SqlParameter("@full_path", varchar, 255);
param.Value = fullPath;

//add param for file name

command.Parameters.Add("@full_path");

command.ExecuteNotQuery(sql);
Sign up to request clarification or add additional context in comments.

Comments

2

Don't write your SQL in this way if at all possible - try and use a SqlCommand object with parameters. That helps in two ways:

  • takes care of the quote / space escaping etc
  • helps guard against SQL injection attacks

Comments

1

I don't understand how the query is related to your question. Seems more like a distraction to me.

At any rate, you can use s.Replace(" ", "%20").

You can also use HttpUtility.UrlEncode(s) but that will encode characters other than just the spaces.

Comments

1

You can convert it to that format by writing new Uri(path).AbsoluteUri.

As everyone else mentioned, use parameters!

Comments

1

I think this is as simple as continuing what you were already doing with string.Replace(string, string) calls:

   For Each f As String In Directory.GetFiles(d)
        objSQLStringBuilder.Append("insert into intranet.dbo.noticeboard (full_path, file_name) values ('" & "file:///" + f.Replace("'", "''").Replace(" ", "%20").Replace("\", "/") & "', '" & f.Remove(0, Len(d) + 1).Replace("'", "''") & "');")
    Next

Also, that is a bad way to write SQL as others have mentioned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.