Solved

How to update a MySQL table using C#?

Posted on 2009-07-15
4
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 24865730
Use

   int rowsUpdated;
   rowsUpdated = command.ExecuteNonQuery();

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

Author Comment

by:akohan
ID: 24865866


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
ID: 24865879

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 75

Expert Comment

by:käµfm³d 👽
ID: 24868884
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

623 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