SQL server management studio local database connection error in windows 7

后端 未结 7 2143
长发绾君心
长发绾君心 2021-01-02 00:30

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

7条回答
  •  滥情空心
    2021-01-02 00:47

    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
    

提交回复
热议问题