I\'m running SQL Server 2012 Management Studio Express in windows 7, i am having issues connecting to the local db. i tried all the above mentioned solutions, didnt work. pl
Try this code:
RegistryKey rKey;
string InstanceName;
//use this string for database connection
string ConString;
string regPath = @"Software\Microsoft\Microsoft SQL Server\UserInstances";
rKey = Registry.CurrentUser.OpenSubKey(regPath);
if (rKey.GetSubKeyNames().Length != 0)
{
regPath += @"\" + rKey.GetSubKeyNames()[0];
rKey = Registry.CurrentUser.OpenSubKey(regPath);
InstanceName = rKey.GetValue("InstanceName").ToString();
ConString = @"Server=np:\\.\pipe\" + InstanceName +
@"\tsql\query;Initial Catalog=RahBord;Trusted_Connection=True";
}
else
{
Process.Start(@"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SqlLocalDB.exe", "c amirLOCALDB -s");
regPath += @"\" + rKey.GetSubKeyNames()[0];
rKey = Registry.CurrentUser.OpenSubKey(regPath);
InstanceName = rKey.GetValue("InstanceName").ToString();
ConString = @"Server=np:\\.\pipe\" + InstanceName + @"\tsql\query;Initial Catalog=RahBord;Trusted_Connection=True";
}
If your instance stopped go to this address:
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\
and use this command
SqlLocalDB.exe s yourInstance