Solved

How to update a MySQL table using C#?

Posted on 2009-07-15
4
409 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

863 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

24 Experts available now in Live!

Get 1:1 Help Now