简体   繁体   English

C#中的ExecuteNonQuery结果

[英]ExecuteNonQuery Result in C#

How can i know if i create a database successfully? 我如何知道我是否成功创建了数据库? I am using "CREATE DATABASE DemoDB" as a SQL command. 我正在使用"CREATE DATABASE DemoDB"作为SQL命令。 ExecuteNonQuery() method returns 0. What should i use to understand if i created a database successfully? ExecuteNonQuery()方法返回0。如果成功创建数据库,我应该使用什么来理解?

As MSDN says: 正如MSDN所说:

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. 对于UPDATE,INSERT和DELETE语句,返回值是该命令影响的行数。 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. 对于所有其他类型的语句,返回值为-1。 If a rollback occurs, the return value is also -1. 如果发生回滚,则返回值也为-1。

ExecuteNonQuery will return 0 for a CREATE DATABASE command because it returns the rows changed by your query. ExecuteNonQuery将为CREATE DATABASE命令返回0,因为它返回查询更改的

This will return some rows if the DB exists: 如果数据库存在,这将返回一些行:

SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DemoDB'

ExecuteNonQuery throws a MySqlException exception if there is an error. 如果存在错误,ExecuteNonQuery会引发MySqlException异常。 It returns 0 if successful but you don't need to check the return value; 如果成功,它将返回0,但是您无需检查返回值;如果成功,则返回0。 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 数据库存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM