简体   繁体   中英

How to store ImagePath in Mysql Database?

I am trying to store an image path in my mysql database but it does not store the backslash(/).

this is my path - string str = @"C:\\Users\\user\\Downloads\\99 Most Amazing Collection of Computer Desktop Wallpapers - 5522 [ECLiPSE]"

this is how it appears in mysql - C:UsersuserDownloads99 Most Amazing Collection of Computer Desktop Wallpapers - 5522 [ECLiPSE]

Because of this i am unable to retrive the image, so how do i store the imagepath with the backslash included.

Mysql Code

      string sqlstring = "server=; port= ; user id =;Password=;Database=;";

   string str = @"C:\Users\user\Downloads\99 Most Amazing Collection of 
       Computer Desktop Wallpapers - 5522 [ECLiPSE]"
            MySqlConnection conn = new MySqlConnection(sqlstring);
            try
            {
                conn.Open();
            }
            catch (MySqlException ex)
            {
                throw ex;
            }
            string Query = "INSERT INTO test.blogimagestable (ImagesId)values('" + str + "');";
            MySqlCommand cmd = new MySqlCommand(Query, conn);
            cmd.ExecuteReader();
            conn.Close();

You should use parametrized queries to prevent such problems from happen automatically. And other nasty things, like SQL injections.

And you should rather call ExecuteNonQuery() instead of ExecuteReader() for a statement, that doesn't return a result. Or at least take care of calling Dispose() on the useless reader or using using . (Same goes for your other disposable objects.)

...
string Query = "INSERT INTO test.blogimagestable (ImagesId)values(?str);";
MySqlCommand cmd = new MySqlCommand(Query, conn)
cmd.Parameters.Add("?str", MySqlDbType.VarString, 256).Value = str;
cmd.ExecuteNonQuery();
...

Change the type and length passed to Parameters.Add() according to the type of your column.

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