In this article, I am going to explain how you can create and drop a database, using C#.
Creating database
This class is the control class, through which we are going to call the helper methods.
- class Program
- {
- private static ExecuteSql _executeMaster;
- private static string _connectionString = "Data Source=.;Integrated Security=SSPI;Initial Catalog=testme;Application Name=Manager.Test;Connection Timeout=300;
- User ID = sa;
- Password = password;
- ";
- private static string _masterDatabaseName = "master";
- static void Main(string[] args)
- {
- _executeMaster = new ExecuteSql(openConnectionToMaster);
- createDatabase("testme");
- Console.WriteLine("done");
- Console.ReadLine();
- }
- private static SqlConnection openConnectionToMaster()
- {
- var connectionStringBuilder = new SqlConnectionStringBuilder(_connectionString);
- connectionStringBuilder.InitialCatalog = _masterDatabaseName;
- var conn = new SqlConnection(connectionStringBuilder.ConnectionString);
- conn.Open();
- return conn;
- }
- private static void createDatabase(string databaseName)
- {
- //check if the DB exists or not
- if (!Exists(databaseName))
- {
- try
- {
- var script = File.ReadAllText("CreateDB.sql");
- script = ReplaceScriptTags(script, databaseName);
- _executeMaster.ExecuteTransactionlessNonQuery(script, 10800);
- } catch (Exception exception)
- {
- string msg = exception.StackTrace;
- }
- }
- }
- private static string ReplaceScriptTags(string script, string name)
- {
- script = script.Replace("$(DBNAME)", name);
- return script;
- }
- public static bool Exists(string databaseName)
- {
- return
- Convert.ToBoolean(_executeMaster.ExecuteScalar("SELECT database_id FROM sys.databases WHERE Name = @databaseName", parameters: new Dictionary & lt; string, object & gt; {
- {
- "@databaseName",
- databaseName
- }
- }));
- }
- public static void Drop(string databaseName)
- {
- _executeMaster.ExecuteTransactionlessNonQuery(string.Format("DROP DATABASE [{0}]", databaseName), 120);
- }
- public static void SetOnline(string databaseName)
- {
- _executeMaster.ExecuteTransactionlessNonQuery(string.Format("ALTER DATABASE [{0}] SET ONLINE", databaseName));
- }
- }
- public class ExecuteSql
- {
- private readonly Func & lt;
- SqlConnection & gt;
- _openConnection;
- public ExecuteSql(Func & lt; SqlConnection & gt; openConnection)
- {
- _openConnection = openConnection;
- }
- public void ExecuteCustom(Action & lt; SqlConnection & gt; action)
- {
- using(var connection = _openConnection())
- {
- action(connection);
- }
- }
- public int ExecuteScalar(string sql, int timeout = 30, IDictionary & lt; string, object & gt; parameters = null)
- {
- parameters = parameters ? ? new Dictionary & lt;
- string, object & gt;
- ();
- var result = 0;
- using(var connection = _openConnection())
- {
- using(var transaction = connection.BeginTransaction())
- {
- using(var command = connection.CreateCommand())
- {
- command.Transaction = transaction;
- foreach(var parameter in parameters)
- {
- command.Parameters.AddWithValue(parameter.Key, parameter.Value);
- }
- command.CommandTimeout = timeout;
- {
- command.CommandText = sql;
- command.CommandType = CommandType.Text;
- result = (int)(command.ExecuteScalar() ? ?
- default (int));
- }
- transaction.Commit();
- }
- }
- }
- return result;
- }
- public void ExecuteTransactionlessNonQuery(string sql, int timeout = 30)
- {
- using(var connection = _openConnection())
- {
- using(var command = connection.CreateCommand())
- {
- command.CommandType = CommandType.Text;
- command.CommandTimeout = timeout;
- command.CommandText = sql;
- command.ExecuteNonQuery();
- }
- }
- }
- public void ExecuteNonQuery(string sql, int timeout = 30, IDictionary & lt; string, object & gt; parameters = null)
- {
- parameters = parameters ? ? new Dictionary & lt;
- string, object & gt;
- ();
- using(var connection = _openConnection())
- {
- using(var transaction = connection.BeginTransaction())
- {
- using(var command = connection.CreateCommand())
- {
- command.Transaction = transaction;
- command.CommandTimeout = timeout;
- {
- command.Parameters.Clear();
- foreach(var parameter in parameters)
- {
- if (sql.Contains(parameter.Key))
- {
- command.Parameters.AddWithValue(parameter.Key, parameter.Value);
- }
- }
- command.CommandText = sql;
- command.CommandType = CommandType.Text;
- command.ExecuteNonQuery();
- }
- transaction.Commit();
- }
- }
- }
- }
- }
- BEGIN TRY
- /*==============================================
- Prepare statements according to server settings
- and given input
- ==============================================*/
- -- DECLARES
- DECLARE @rc int
- DECLARE @DataDir nvarchar(4000)
- DECLARE @LogDir nvarchar(4000)
- DECLARE @LogFileName nvarchar(4000)
- DECLARE @DataFileName nvarchar(4000)
- DECLARE @CMD nvarchar(4000)
- -- Read reg values to get default datapath
- EXEC @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultData', @DataDir output, 'no_output'
- -- Check if value is NULL. In that case no changes has been done to default setup. Read from SQLDataRoot instead
- IF (@DataDir is null)
- BEGIN
- EXEC @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\Setup',N'SQLDataRoot', @DataDir output, 'no_output'
- SELECT @DataDir = @DataDir + N'\Data'
- END
- -- Read reg values to get default logpath
- EXEC @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultLog', @LogDir output, 'no_output'
- -- Check if value is NULL. In that case no changes has been done to default setup. Read from SQLDataRoot instead
- IF (@LogDir is null)
- BEGIN
- EXEC @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\Setup',N'SQLDataRoot', @LogDir output, 'no_output'
- SELECT @LogDir = @LogDir + N'\Data'
- END
- -- Add filenames to the folders
- SET @DataFileName = @DataDir + '\$(DBNAME)_Data.mdf'
- SET @LogFileName = @LogDir + '\$(DBNAME)_Log.ldf'
- -- Prepare statement
- SELECT @CMD =
- 'CREATE DATABASE $(DBNAME)
- ON
- (
- NAME = $(DBNAME)_Data,
- FILENAME = ''' + @DataFileName + ''',
- SIZE = 50,
- FILEGROWTH = 10
- )
- LOG ON (
- NAME = $(DBNAME)_Log,
- FILENAME = ''' + @LogFileName + ''',
- SIZE = 100,
- FILEGROWTH = 20
- )
- '
- /*==============================================
- This is were the action starts
- ==============================================*/
- PRINT 'Adding database $(DBNAME). Working...'
- -- Create database
- EXEC (@CMD)
- PRINT 'Adding database $(DBNAME). Finished!'
- PRINT 'Adding settings and properties for $(DBNAME). Working...'
- -- Add DBTYPE as extended property
- EXEC $(DBNAME).sys.sp_addextendedproperty @name=N'DatabaseType', @value=N''
- --Disable fulltext
- IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
- BEGIN
- EXEC $(DBNAME).[dbo].[sp_fulltext_database] @action = 'disable'
- END
- -- Set recovery model
- ALTER DATABASE $(DBNAME) SET RECOVERY FULL
- ALTER DATABASE $(DBNAME) SET MULTI_USER
- --All other database settings are based on the "model" database in local instance
- --see: sp_configure
- -- Set owner = sa (cosmetics only)
- EXEC $(DBNAME).dbo.sp_changedbowner @loginame = N'sa', @map = false
- PRINT 'Adding settings and properties for $(DBNAME). Finished'
- END TRY
- BEGIN CATCH
- DECLARE @ErrorMessage NVARCHAR(4000)
- DECLARE @ErrorNumber INT
- DECLARE @ErrorSeverity INT
- DECLARE @ErrorState INT
- DECLARE @ErrorLine INT
- IF ERROR_NUMBER() IS NOT NULL
- BEGIN
- SET @ErrorNumber = ERROR_NUMBER()
- SET @ErrorSeverity = ERROR_SEVERITY()
- SET @ErrorState = ERROR_STATE()
- SET @ErrorLine = ERROR_LINE()
- -- Return an error with state 127 since it will abort SQLCMD
- SET @ErrorMessage = 'Error %d, Severity %d, State %d, Line %d, Message: '+ ERROR_MESSAGE()
- RAISERROR (@ErrorMessage, 16, 127, @ErrorNumber, @ErrorSeverity, @ErrorState, @ErrorLine)
- END
- END CATCH
- PRINT 'Finished'
- /*******************************************************************************/
Now this time, we will drop a DB. I have divided the code into two classes. Let's see them in detail.
The first class is the control class through which we will call the helpers. In the code, given below, first, we will try to connect to the master DB. In the drop database method, we need to check if the DB exists or not and if it does, we will need to set the DB online. The reason to set the DB online is when you drop the DB, the file on the disk will also be deleted.
- class Program
- {
- private static ExecuteSql _executeMaster;
- private static string _connectionString = "Data Source=.;Integrated Security=SSPI;Initial Catalog=testme;Application Name=Manager.Test;Connection Timeout=300;
- User ID = sa;
- Password = password;
- ";
- private static string _masterDatabaseName = "master";
- static void Main(string[] args)
- {
- _executeMaster = new ExecuteSql(openConnectionToMaster);
- dropDatabase("testme");
- Console.WriteLine("done");
- Console.ReadLine();
- }
- private static SqlConnection openConnectionToMaster()
- {
- var connectionStringBuilder = new SqlConnectionStringBuilder(_connectionString);
- connectionStringBuilder.InitialCatalog = _masterDatabaseName;
- var conn = new SqlConnection(connectionStringBuilder.ConnectionString);
- conn.Open();
- return conn;
- }
- private static void dropDatabase(string databaseName)
- {
- if (Exists(databaseName))
- {
- SetOnline(databaseName); // if dropping a database that is offline, the file on disk will remain!
- _executeMaster.ExecuteTransactionlessNonQuery(string.Format("ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;", databaseName));
- Drop(databaseName);
- }
- }
- public static bool Exists(string databaseName)
- {
- return
- Convert.ToBoolean(_executeMaster.ExecuteScalar("SELECT database_id FROM sys.databases WHERE Name = @databaseName", parameters: new Dictionary & lt; string, object & gt; {
- {
- "@databaseName",
- databaseName
- }
- }));
- }
- public static void Drop(string databaseName)
- {
- _executeMaster.ExecuteTransactionlessNonQuery(string.Format("DROP DATABASE [{0}]", databaseName), 120);
- }
- public static void SetOnline(string databaseName)
- {
- _executeMaster.ExecuteTransactionlessNonQuery(string.Format("ALTER DATABASE [{0}] SET ONLINE", databaseName));
- }
- }

kalu singh raoPosted Jul 8, 2016, 1:30 AM
Nice...