2

I have a local MS SQL Database, and I want to update one of it's bit field. I have the following code:

static void UpgradeVevo(string nev)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            SqlCommand command = new SqlCommand("UPDATE Vevok SET Torzsvendeg=True Where Nev=" + nev, connection);
            command.ExecuteNonQuery();
        }
        Console.WriteLine(nev+" mostmár törzsvendég");
    }

Torzsvendeg is a bit datatype(I have tried to set its value to 1 too), and Nev is varchar.

The connectionstring should be fine, since I have tried Select in another method and it works fine. The above code throws no exceptions, but the table does not get updated.

I have tried to find an answer for quite some time, with no success :/. Thank you for your help in advance!

3
  • Does a column exist where the database field Nev matches the variable nev? Also, use parameterised queries, your statement is vulnerable to SQL injection. Commented May 12, 2017 at 21:20
  • Thank you for your tip, I will, I just want to figure this out first. The Vevok table contains one record so far, where the Nev is vevo1. The variable nev is vevo1. Commented May 12, 2017 at 21:31
  • Obligatory xkcd comic Commented May 12, 2017 at 22:37

2 Answers 2

1

True should be in a single quote since it's a string literal like

UPDATE Vevok SET Torzsvendeg='True'

Well brother, you are messed up with quotes. Your query should look like

"UPDATE Vevok SET Torzsvendeg = 1 Where Nev = '" + nev + "'"

Again, use parametarized query and not this concatenated one to avoid SQL Injection

Sign up to request clarification or add additional context in comments.

4 Comments

There is only one record in my table so far(as I'm trying to figure this out first), where the Nev is vevo1. I pass that paramater, but now it gives an SqlException, "Invalid column name 'vevo1'.". I don't really get what I'm doing wrong still :D
@Hevesi, umm .. mind checking the update in answer again
Yes, I know about SQL injection, and I will definetely use parametarized query once I figure this out :D Although, there are no exceptions this time, but its still not updated in the database...
As I have mentioned I have a local db, and for some reason Visual Studio creates a copy of my db to bin/debug, and when I'm updating the db, its updating that copy of my db, not the original db.... The question now is how can I update the original db instead...
0

If the column is a boolean (bit in sql server) then you will have to write

Torzsvendeg=1

instead of

Torzsvendeg='True' or Torzsvendeg=True

Edit:

Please try this:

static void UpgradeVevo(string nev)
{
    var connection = new SqlConnection(connectionString))
    connection.Open(); // try doing this without a using
    SqlCommand command = new SqlCommand("UPDATE Vevok SET Torzsvendeg=@enabled Where Nev=@nev", connection);
    command.Parameters.AddWithValue(@"enabled", 1);
    command.Parameters.AddWithValue(@"nev", "vevo123");
    command.ExecuteNonQuery();
    command.Parameters.Clear(); // always clear after executed
    // close connection when you shut down your application
    connection.Close();
    connection.Dispose();

    Console.WriteLine(nev+" mostmár törzsvendég");
}

7 Comments

I have also tried that with no success, the database is still not updated.
Does the column has to be varchar? I usually use nvarchar(256) or nvarchar(512) for stuff like mails and ntext for text. Can you maybe try that?
Tried, and it still shows False. I have no idea what am i missing(Im not forgetting to refresh the table)...
Im using a local database, and I have found that VS is creating a copy of my db to bin/debug and when I'm updating the table it's updating the copy of my db and not the original. I have checked, and the copied db's field in bin/debug is True. Now to figure out how to update the original too/instead...
Why you dont work with that in you bin folder instead? When you will switch to a database on a server this can not happen anyway.
|

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.