1

I'm adding a record into remote mysql database and after that I want to check if it's added - So i want to try to select this particular record.

My code:

    public void Select(string filename)
    {
        string query = "SELECT * FROM banners WHERE file = '"+filename+"'";

        //open connection
        if (this.OpenConnection() == true)
        {
            //create command and assign the query and connection from the constructor
            MySqlCommand cmd = new MySqlCommand(query, connection);

            //Execute command
            cmd.ExecuteNonQuery();

            //close connection
            this.CloseConnection();
        }
    }

How to check the response from the server? Or selected records? It's c# windows forms app.

2
  • You may use datatable to be filled with the data either by SqlDataReader or SqlDataAdapter. Now just check rowcount over datatable object. Commented Jul 19, 2012 at 5:00
  • filename is new added record? Commented Aug 1, 2017 at 6:46

3 Answers 3

2

Instead of making another round-trip (with additional select) you can expand you insert statement by

SELECT LAST_INSERT_ID();

execute your insert command

command.ExecureQuery();

and get the ID of the last inserted record.

Have a look at MySQL Information Functions

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

public void Select(string filename)
{
        string query = "SELECT * FROM banners WHERE file = '"+filename+"'";

        //open connection
        if (this.OpenConnection() == true)
        {
                //create command and assign the query and connection from the constructor
                MySqlCommand cmd = new MySqlCommand(query, connection);

                //Get the DataReader from the comment using ExecuteReader
                            MySqlDataReader reader = cmd.ExecuteReader(); 
                while (myReader.Read())  
                { 
                    //Use GetString etc depending on the column datatypes.
                    Console.WriteLine(myReader.GetInt32(0)); 
                } 

                //close connection
                this.CloseConnection();
        }
}

Check this URL for more info: http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.100).aspx

Comments

0

You can do it when you run command.ExecuteNonQuery() for INSERT statement itself. command.ExecuteNonQuery() returns the number of rows affetced (inserted in your case). So

if(command.ExecuteNonQuery()>0)
{
 //inserted
}
else
{
 //Not inserted
}

http://dev.mysql.com/doc/refman/5.1/en/connector-net-ref-mysqlclient.html#connector-net-ref-mysqlclient-mysqlcommand-executenonquery

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.