简体   繁体   中英

How to Save File Path in the MySQL Database Using string in C#

I have a picturebox control and also a imagepath textbox control.When I upload an image into the picturebox control then the full path of image in my computer is also pasted in the imagepath textbox control as

E:\\Engineer\\picture.jpg

but when I save this image and also its path in my database, then the path is saved without backslashes such as like this:

E:Engineerpicture.jpg

I am using string type for the path. This is the main problem which had stuck my developing process of application. If any other explaination is needed then kindly let me know. Thank You

Backslashes are escaped characters.

You can instead escape the backslashes as below:

string filename = "E:\\Engineer\\picture.jpg";

Or use a string literal:

string filename = @"E:\Engineer\picture.jpg";

Further reading from MSDN: https://msdn.microsoft.com/en-us/library/h21280bw.aspx

I did a bit research...but could not find escaping a string after it is initialized as suggested in the other answer. So it leaves us with this option:

string newFileName = fileName.Replace("\\","\\\\");

Doing this before the DB insert should help you, but I am not sure if it is the best way to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM