MySQL Command not executing

MySQL Command not executing

Postby rkrell » Fri 16 Sep 2011 16:03

I am writing a application in C#.NET and have ran into a small problem. When running the program the following lines of code setup the query:

MySqlCommand cmd = connection.CreateCommand();
DateTime cDate = DateTime.Now;
char quote = (char)34;

cmd.CommandText = String.Format("UPDATE config_data_test SET old_name = name, test=2, modifyDt={1}{0:yyyy/MM/dd HH:MM:ss}{1} WHERE old_name IS NULL;", cDate, quote);

I have verified the connection to the database by performing a simple select statement but when I run the query above nothing is happening. the table is not being updated or effected in any way. But I can take the output query generated above and copy and paste it in a query window and it works fine so I have to believe it is not the way I am coding the query.

If anyone can see my error or know of something that I may be overlooking I would appriciate the assistance. I have posted the entire script below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using Devart.Data.MySql;

namespace MetadataUpdate_Csharp
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

void ModifyData(MySqlConnection connection)
{
MySqlCommand cmd = connection.CreateCommand();
DateTime cDate = DateTime.Now;
char quote = (char)34;

cmd.CommandText = String.Format("UPDATE config_data_test SET old_name = name, test=2, modifyDt={1}{0:yyyy/MM/dd HH:MM:ss}{1} WHERE old_name IS NULL;", cDate, quote);

lblComment.Text = cmd.CommandText;
}

protected void butCopyNames_Click(object sender, EventArgs e)
{
using (MySqlConnection oConn = new MySqlConnection("User Id=mdupdate;Password=mdupdate2011;Host=Lake;Port=3306;Database=weave;"))
{
try
{
oConn.Open();
ModifyData(oConn);
}
catch (MySqlException ex)
{
lblError.Text = "Exception occurs: " + ex.Message;
}
finally
{
oConn.Close();
}
}
}
}
}
rkrell
 
Posts: 1
Joined: Fri 16 Sep 2011 15:58

Postby Shalex » Tue 20 Sep 2011 13:49

This code does not send any query to the database. Try using
Code: Select all
lblComment.Text = cmd.ExecuteNonQuery();

instead of
Code: Select all
lblComment.Text = cmd.CommandText;


In general, we recommend debugging database interoperation of your application with the help of the dbMonitor tool that performs per-component tracing of database events such as commit, rollback, SQL statement execute etc.
Download link: http://www.devart.com/dbmonitor/dbmon3.exe
Documentation: http://www.devart.com/dotconnect/mysql/ ... nitor.html
Shalex
Devart Team
 
Posts: 7538
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for MySQL