Solved

How to update a MySQL table using C#?

Posted on 2009-07-15
4
406 Views
Last Modified: 2013-12-17

Hello group,

How can I update a record in MySQL table?
I can select and get the resuls but I cannot update any record.

Thanks.

strSQLStmt1 = "";

 strSQLStmt1 = "UPDATE table set  fld= '" + t.score.Trim().ToString() + "' WHERE Id = '" + t.applicant_id.Trim() + "' ";

                        Reader1 = command.ExecuteReader();

                        Reader1.Close();

Open in new window

0
Comment
Question by:akohan
  • 2
  • 2
4 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Use

   int rowsUpdated;
   rowsUpdated = command.ExecuteNonQuery();

instead of ExecuteReader(). rowsUpdated is just to show what is returned.
0
 

Author Comment

by:akohan
Comment Utility


Thanks I'm sure that is the answer but I'm getting

Cannot implicitly convert type 'int' to 'MySql.Data.MySqlClient.MySqlDataReader'
0
 

Author Comment

by:akohan
Comment Utility

OK I fixed that but my return value is -1 !

What could cause that? in msdn page says a rollback might cause it. Is this something I should take care of in db?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I don't see where in code you assigned the sql query to the command object. See notation below. Also, you can set a breakpoint at line 6 below, mouse over the strSQLStmtml variable and see the actual SQL statement that will be executed against the DB. You can try manually running that query against the DB to see if it generates any errors.
strSQLStmt1 = "";

strSQLStmt1 = "UPDATE table set  fld= '" + t.score.Trim().ToString() + "' WHERE Id = '" + t.applicant_id.Trim() + "' ";
 

command.CommandText = strSQLStmtl;
 

Reader1 = command.ExecuteReader();

Reader1.Close();

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now