How do I update records using data reader?
The procedure for updating records using INSERT commands is very similar to the one we presented in the previous example (of SELECT) except that here the command does not return anything and thus the method to call on the SqlCommand object is called ExecuteNonQuery().
C# Version
string connString = "server=FARAZ; database=programmersheaven;" +
"uid=sa; pwd=";
SqlConnection conn = new SqlConnection(connString);
// UPDATE Query
string cmdString = "UPDATE Author " +
"SET name = 'Grady Booch' " +
"WHERE authorId = 3";
SqlCommand cmd = new SqlCommand(cmdString, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
VB.Net Version
Dim connString As String = "server=FARAZ; database=programmersheaven;" + _
"uid=sa; pwd="
Dim conn As New SqlConnection(connString)
' UPDATE Query
Dim cmdString As String = "UPDATE Author " + _
"SET name = 'Grady Booch' " + _
"WHERE authorId = 3"
Dim cmd As New SqlCommand(cmdString, conn)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
Back