Today, we discussed about "Get DataBase Exists or not using DataContext in C#.net"
DataContext context = new DataContext(GetConnectionString("YourServerName"));
bool dbExists = context.DatabaseExists();
if (dbExists)
{
MessageBox.Show("Database Exists");
}
else
{
MessageBox.Show("Database doesn't Exist");
}
Get Connection string Method
bool dbExists = context.DatabaseExists();
if (dbExists)
{
MessageBox.Show("Database Exists");
}
else
{
MessageBox.Show("Database doesn't Exist");
}
static string GetConnectionString(string serverName)
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Data Source"] = serverName; builder["integrated Security"] = true; builder["Initial Catalog"] = "Sample2";
Console.WriteLine(builder.ConnectionString);
Console.ReadKey(true);
return builder.ConnectionString;
}
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Data Source"] = serverName; builder["integrated Security"] = true; builder["Initial Catalog"] = "Sample2";
Console.WriteLine(builder.ConnectionString);
Console.ReadKey(true);
return builder.ConnectionString;
}