-
-
Save mahizsas/ca9474d2c86cdd36a4d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace LocalDBSample | |
{ | |
using System; | |
using System.Data.SqlClient; | |
using System.IO; | |
using System.Reflection; | |
public class LocalDBHelper | |
{ | |
public static string DataDirectory | |
{ | |
get | |
{ | |
return (string)AppDomain.CurrentDomain.GetData("DataDirectory") ?? | |
(DataDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "App_Data")); | |
} | |
set | |
{ | |
AppDomain.CurrentDomain.SetData("DataDirectory", value); | |
if (!Directory.Exists(value)) | |
{ | |
Directory.CreateDirectory(value); | |
} | |
} | |
} | |
public LocalDBHelper(string databaseName) : this(11, databaseName) | |
{ | |
} | |
public LocalDBHelper(int version, string databaseName) | |
{ | |
if (version != 11 && version != 12) | |
{ | |
version = 11; | |
} | |
this.Version = version; | |
this.DatabaseName = databaseName; | |
if (DataDirectory == null) | |
{ | |
} | |
} | |
public int Version | |
{ | |
get; | |
protected set; | |
} | |
public string DatabaseName | |
{ | |
get; | |
protected set; | |
} | |
public string DatabaseConnectionString | |
{ | |
get | |
{ | |
return string.Format(@"Server=(localdb)\v{0}.0; AttachDbFilename=|DataDirectory|\{1}.mdf; Database={1}; Integrated Security=True", this.Version, this.DatabaseName); | |
} | |
} | |
public string MasterConnectionString | |
{ | |
get | |
{ | |
return string.Format(@"Server=(localdb)\v{0}.0; Database=master; Integrated Security=True", this.Version); | |
} | |
} | |
public void CreateDatabase() | |
{ | |
using (var connection = new SqlConnection(this.MasterConnectionString)) | |
{ | |
connection.Open(); | |
using (SqlCommand cmd = connection.CreateCommand()) | |
{ | |
cmd.CommandText = String.Format(@"CREATE DATABASE {0} ON (NAME = N'{0}', FILENAME = '{1}\{0}.mdf')", this.DatabaseName, DataDirectory); | |
cmd.ExecuteNonQuery(); | |
} | |
} | |
} | |
public void DropDatabase() | |
{ | |
using (var connection = new SqlConnection(this.MasterConnectionString)) | |
{ | |
connection.Open(); | |
using (SqlCommand cmd = connection.CreateCommand()) | |
{ | |
cmd.CommandText = String.Format(@"ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE", this.DatabaseName); | |
cmd.ExecuteNonQuery(); | |
cmd.CommandText = String.Format(@"DROP DATABASE {0}", this.DatabaseName); | |
cmd.ExecuteNonQuery(); | |
} | |
} | |
} | |
public bool CheckDatabase() | |
{ | |
try | |
{ | |
using (var connection = new SqlConnection(this.DatabaseConnectionString)) | |
{ | |
connection.Open(); | |
return true; | |
} | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment