简体   繁体   中英

Update query returns wrong values

I have this function:

 virtual public bool EditVideoNumber(String oldnumber, String channelnumber)
    {
        using (var con = GetConnection())
        {
            con.Open();
            var cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText += "update Videos set number=REPLACE(number,@oldnumber,@channelnumber)";
            cmd.Parameters.Add(new SqlParameter("@channelnumber", channelnumber));
            cmd.Parameters.Add(new SqlParameter("@oldnumber", oldnumber));
            try
            {
                cmd.ExecuteScalar();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }

The problem is that, in the moment that i update the value oldnumber, from 102 to 1020 (just an example), it returns as a value 10200. i have tried to change the query several times, but nothing seems to make it work

You can try this. This will replace your current number value, with an other value with 0 appended at the end:

cmd.CommandText += "update Videos set number=REPLACE(number,number,CONTACT(number,'0'))";

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