0

I am developing a c# program that deals with an excel file and a sql server table. For now I am using a conneciton string I declare in my program but eventually want to let the user choose the file with an openfiledialog.

My question is when I add the "filepath" variable to the connection string, will it throw an error as there are no escape sequences ? If i add "@" to the start of the string can I still add the string variable to the connection string ?

Thanks in advanced. ☮ ✌

Edit: Example

For now just to get other functionality right I am using this string

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hbudhran\Desktop\Copy of EUR SVC EUR PRICE LIST.xls; Extended Properties='Excel 8.0;HDR=YES';"

But eventually when I'm polishing my program to look and act pretty I'll want the user to browse for the excel file that he wants to insert into the DB. So the string would look like:

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath +"; Extended Properties='Excel 8.0;HDR=YES';"

  • 1
    It's not really clear what you mean. It would be much easier to help if you'd give your sample code - and tell us what happened when you tried it. Fundamentally though, the idea of an escape sequence is one which applies to string literals... not strings themselves. – Jon Skeet Sep 25 '14 at 14:46
  • Sadly the Stack Overflow community though amazing, lacks the skill to read your mind and code telepathically. Please post your code that is causing the trouble for more assistance. – Greg Sep 25 '14 at 14:46
  • With the way this question is worded it seams like you are asking "what will happen ...". Why not code it and see what happens? – Gridly Sep 25 '14 at 14:47
  • If you are asking what I think you are asking, the answer is no. The @ is for you to not have to escape it, the compiler will still throw in the escapes accordingly. Look at this example ` string a = "c:\\test\\one.txt"; string b = string.Format(@"add \ some \ text \ before \ it and {0}", a);` If you view b in immidiate window it will be "add \\ some \\ text \\ before \\ it and c:\\test\\one.txt" Edit: What I mean is you can just pass it in like a string format or something- it will be escaped correctly. – Nyra Sep 25 '14 at 14:49

1 Answers1

0

you should add "@" for the variables and use parameter property to add it to your SQL command. here is an example: Must Declare Scalar Variable

hope it helps!

Community
  • 1
  • 1
Kris
  • 9
  • 1