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?

- 47,018
- 22
- 121
- 208

- 2,729
- 12
- 43
- 66
-
You need to read up on what mysql give's you to understand what you can do with a database. Once you know what can be done then you can easily verify it's been created correctly. – Preet Sangha Jun 05 '12 at 08:48
3 Answers
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.

- 10,464
- 4
- 26
- 36
-
1Not sure how that helps answer the question? And even so, the OP says that in this case the return value is 0. – eggyal Jun 05 '12 at 08:50
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'

- 24,648
- 6
- 68
- 78
-
Whilst this might suffice insofar as one can discover whether the desired database exists, it doesn't actually answer the question insofar as discovering whether the command succeeded or failed. – eggyal Jun 05 '12 at 08:57
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

- 627
- 6
- 13