Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UPDATE in ado net

Tags:

c#

.net

sql

ado.net

I need to perform an update in a table(Homework). But it is not just replacing an old value with a new one; to the already existing value in the column i have to add(SUM) the new value(the column is of type int). This is what i did so far but i am stuck:

protected void subscribeButton_Click(object sender, EventArgs e)
    {
        string txtStudent = (selectedStudentLabel.Text.Split(' '))[0];
        int studentIndex = 0;
        studentIndex = Convert.ToInt32(txtStudent.Trim());        

        SqlConnection conn = new SqlConnection("Server=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Trusted_Connection=True;User Instance=yes");
        conn.Open();
        string sql2 = "UPDATE student SET moneyspent = " + ?????? + " WHERE id=" + studentIndex + ";";
        SqlCommand myCommand2 = new SqlCommand(sql2, conn);

        try
        {
            conn.Open();
            myCommand2.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex);
        }
        finally
        {
            conn.Close();
        }
    }

What should i add intead of ??? to achieve my goal?

Is it possible to do it this way? I want to avoid using to many queries.

like image 461
javing Avatar asked Apr 24 '11 17:04

javing


People also ask

What is the use of update clause?

In SQL, the UPDATE statement is used to modify or update existing records in a table. You can use it to update everything all at once, or you can specify a subset of records to modify using the WHERE clause. The UPDATE statement is considered a SQL data manipulation command.

What can the SqlDataAdapter update () method do?

The Update method of the DataAdapter is called to resolve changes from a DataSet back to the data source. The Update method, like the Fill method, takes as arguments an instance of a DataSet , and an optional DataTable object or DataTable name.

How do you update a DataSet?

On the Datasets page, choose the dataset that you want to update, and then choose Edit dataset. On the data preparation page that opens, choose the drop-down list for the file that you want to update, and then choose Update file. On the Update file page that opens, choose Upload file, and then navigate to a file.

Can we use WHERE with update?

You can use the WHERE clause with the UPDATE query to update the selected rows, otherwise all the rows would be affected.


1 Answers

If i understand you correctly (i'm not sure i do) you want something like this:

string sql2 = "UPDATE student SET moneyspent = moneyspent + @spent WHERE id=@id";
SqlCommand myCommand2 = new SqlCommand(sql2, conn);
myCommand2.Parameters.AddWithValue("@spent", 50 )
myCommand2.Parameters.AddWithValue("@id", 1 )

Notice how i've used parameters and not string concatenation, very important!!

like image 188
Paul Creasey Avatar answered Oct 28 '22 21:10

Paul Creasey