tee
tee

Reputation: 155

SQL Won't Update Database

This sql statement I check and it works:

UPDATE faxcomplete SET DATE= curdate() WHERE DATE='0000-00-00'

When I run the code it not update database. I am a beginner and I don't know how to check if something is wrong:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      string MyConString = "SERVER=localhost;" +
                           "DATABASE=webboard;" +
                           "UID=root;" +
                           "PASSWORD='';";
      MySqlConnection connection = new MySqlConnection(MyConString);
      MySqlCommand command = connection.CreateCommand();
      MySqlDataReader Reader;
      command.CommandText = "UPDATE `faxcomplete` SET `DATE`= curdate() WHERE `DATE`='0000-00-00'";
    }
  }

}

Upvotes: 0

Views: 494

Answers (3)

Chris Haines
Chris Haines

Reputation: 6555

Execute the SQL:

command.ExecuteNonQuery();

You also need to close the connection once you're done.

connection.Close();

You do not need the line

MySqlDataReader Reader;

Upvotes: 3

Manatherin
Manatherin

Reputation: 4187

well one thing i noticed is that your not executing the command or binding it to the connection. I tend to use something like

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand("name", connection))
        {
            command.CommandText = "UPDATE `faxcomplete` SET `DATE`= curdate() WHERE `DATE`='0000-00-00'";
            command.ExecuteNonQuery();
        }
    }

Upvotes: 1

Pontus Gagge
Pontus Gagge

Reputation: 17278

Try calling ExecuteNonQuery on your command.

Upvotes: 4

Related Questions