Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Transactions property during create database command (in C#)

  • I need to create a database
  • Run a SQL Script on the created database
  • I need to enter some data into created Db Programmitically

If any of the steps fail, the whole transaction should rollback even the database creation. But when I am using transaction property on Create database, it is throwing an exception

CREATE DATABASE statement not allowed within multi-statement transaction.

Is there any method to achieve the same instead of delete database command.(i.e programmitically delete the database)

like image 731
Veer Avatar asked Oct 23 '25 12:10

Veer


2 Answers

I'm afraid you can't.

From the MSDN documentation:

The CREATE DATABASE statement must run in autocommit mode (the default transaction management mode) and is not allowed in an explicit or implicit transaction.

like image 69
ken2k Avatar answered Oct 25 '25 04:10

ken2k


No, it's not possible. CREATE DATABASE:

The CREATE DATABASE statement must run in autocommit mode (the default transaction management mode) and is not allowed in an explicit or implicit transaction.

So you'll have to explicitly check at the end and issue a DROP DATABASE to clean up.

like image 44
Damien_The_Unbeliever Avatar answered Oct 25 '25 02:10

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!