I want to use Dapper
into my WebApi Project. I Have a method for creating database from query:
public async Task<bool> CreateDatabase()
{
var connection = _connection.GetOpenConnection();
bool created = await connection.ExecuteAsync(SqlQuery.CreateDatabase);
}
My question is, how to write query for creating database and how to ensure, that query completed succesfuly and which method from dapper to use?
My SqlQuery.CreateDatabase
looks like this:
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'DataBase')
BEGIN
CREATE DATABASE DataBase
END
Do i need to return something from query?
If a problem occurs, it should already present as an exception - probably a SqlException
- telling you about it, so: what you have should already be fine. Note, though, that if your connection string tries to connect to the database that doesn't yet exist, then Open[Async]
is likely to fail. Ultimately, you could just try connecting to the specified database afterwards to be sure, but again, what you have should already be fine.
Note that it is often the case that app accounts do not have permissions to create databases. In that scenario, you'll need to get a DBA to create the database for you, and tell you the connection details.