Question

How can i know if i create a database successfully? I am using "CREATE DATABASE DemoDB" as a SQL command. ExecuteNonQuery() method returns 0. What should i use to understand if i created a database successfully?

Was it helpful?

Solution

ExecuteNonQuery throws a MySqlException exception if there is an error. It returns 0 if successful but you don't need to check the return value; if it returns normally it has succeeded. For example here is the exception I get when I try to create a database that already exists:

Can't create database 'name'; database exists

OTHER TIPS

As MSDN says:

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

ExecuteNonQuery will return 0 for a CREATE DATABASE command because it returns the rows changed by your query.

This will return some rows if the DB exists:

SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DemoDB'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top